Skip to main content
Skip table of contents

Rendering with an alpha channel

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:

  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

๐Ÿ“— Code

Click here to expand...
C#
/* --------------------------------------------------------------------------------
 *  <copyright file="Program.cs" company="Global Graphics Software Ltd">
 *    Copyright (c) 2024 Global Graphics Software Ltd. All rights reserved.
 *  </copyright>
 *  <summary>
 *    This example is provided on an "as is" basis and without warranty of any kind.
 *    Global Graphics Software Ltd. does not warrant or make any representations
 *    regarding the use or results of use of this example.
 *  </summary>
 * ---------------------------------------------------------------------------------
 */

using JawsMako;

namespace RenderWithAlpha;

internal class Program
{
    static int Main()
    {
        try
        {
            var testFilepath = @"..\..\..\..\TestFiles\";

            var mako = IJawsMako.create();
            IJawsMako.enableAllFeatures(mako);

            // Input
            var pdfInput = IPDFInput.create(mako);
            using var assembly = pdfInput.open(testFilepath + "BeerLabels.pdf");
            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, "BeerLabels.png"));
        }
        catch (MakoException e)
        {
            Console.WriteLine($"Exception thrown: {e.m_errorCode}: {e.m_msg}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Exception thrown: {e}");
        }

        return 0;
    }
}

JavaScript errors detected

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

If this problem persists, please contact our support.