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
Opens a PDF then creates an
IDOMFixedPagefrom the first page in the fileRenders to separations + alpha channel
Combines the result of recombining the RGB separations with the alpha channel
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.
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.