Skip to main content
Skip table of contents

Rendering with an alpha channel

📌 Overview

This example explains how to render a PDF to a PNG file with an alpha channel. The code example is C#.

📗 Instructions

The example code carries out the following steps:

🪜 Steps

  1. Opens a PDF then creates an IDOMFixedPage from the first page in the file

  2. Renders to separations + alpha channel

  3. Combines the result of recombining the RGB separations with the alpha channel

  4. Encodes the image as a PNG and write to disk

⌨️ Sample Code

Code can be found below or on our GitHub Gists page. See AlphaChannelRender.cs.

C#
using var inFixedPage = assembly.getDocument().getPage().getContent();

double dpi = 144;
var destWidth = (uint)(inFixedPage.getWidth() / 96.0 * dpi);
var destHeight = (uint)(inFixedPage.getHeight() / 96.0 * dpi);
var rgbSpace = IDOMColorSpaceDeviceRGB.create(mako);

var renderer = IJawsRenderer.create(mako);
var separations = renderer.renderSeparations(inFixedPage, 8, rgbSpace, 4,
    new FRect(), destWidth, destHeight, new CEDLVectWString(), IOptionalContent.Null(),
    eOptionalContentEvent.eOCEPrint, new CEDLVectWString(), true);

// Save the alpha channel
var alpha = separations[3];

// Just the RGB channels
separations.resize(3);

IDOMImage rgbImage = IDOMRecombineImage.create(mako, rgbSpace, separations);
IDOMImage image = IDOMRecombineAlphaImage.create(mako, rgbImage, alpha);

IDOMPNGImage.encode(mako, image, IOutputStream.createToFile(mako, "output.png"));

☑️ Conclusion

Rendering a PDF to a PNG file with an alpha channel in Mako involves several key steps, including opening the PDF, rendering separations with an alpha channel, and encoding the image as a PNG. The provided C# example demonstrates how to achieve this efficiently. By following these steps, users can ensure that their rendered images maintain transparency and high quality.

📚 Additional Resources

If you need additional help, see our API documentation for detailed information on class/method usage, or raise a support ticket via our customer portal.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.