Skip to main content
Skip table of contents

How to use the IPDFPageInserter class

📌 Overview

This simple example shows how to use the IPDFPageInserter class, to add a cover page to an existing PDF.

📗 Instructions

The IPDFPageInserter needs two streams for input and output to work. It takes input stream in its constructor and inserts pages to output stream using an insert() method. It then has a save() method for writing an output stream.

🪜 Steps

  1. Open input streams

    CODE
    var inputStream = IInputStream.createFromFile(mako, fileThatNeedsACover);
    var insertStream = IInputStream.createFromFile(mako, coverPageFile);
    
    1. inputStream is the PDF that will receive the inserted page(s).

    2. insertStream is the PDF from which pages will be copied.

  2. Create a page inserter for the target PDF

    CODE
    IPDFPageInserter pageInserter = IPDFPageInserter.create(mako, inputStream);
    
  3. Insert the page(s)

    CODE
    pageInserter.insert(insertStream, 0, 0, 1);
    

    Parameters (all zero-based):

    1. insertStream: source PDF (the cover)

    2. 0: destination index (insert at the very front of output pdf)

    3. 0: source start page index (take from page 0 = first page of input pdf)

    4. 1: number of pages to insert

  4. Save the result

    CODE
    pageInserter.save(IOutputStream.createToFile(mako, fileThatNowHasACover));
    

    Writes the combined PDF to the output file.

⌨️ Sample Code

Also available at IPDFPageInserterExample.cs.

C#
/* -----------------------------------------------------------------------
 *  <copyright file="IPDFPageInserterExample.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 System.Diagnostics;
using JawsMako;
using static JawsMako.jawsmakoIF_csharp;

namespace AddCoverPage;

internal class Program
{
    static int Main()
    {
        var testFilepath = @"..\..\..\..\..\TestFiles\";
        var fileThatNeedsACover = testFilepath + "DocumentBeforeCoverPage.pdf";
        var fileThatNowHasACover = testFilepath + "DocumentAfterCoverPage.pdf";
        var coverPageFile = testFilepath + "CoverPage.pdf";

        try
        {
            // Create Mako instance
            var mako = IJawsMako.create();
            IJawsMako.enableAllFeatures(mako);

            var stopWatch = new Stopwatch();
            stopWatch.Start();

            // File we want to insert into
            var inputStream = IInputStream.createFromFile(mako, fileThatNeedsACover);

            // File we want to get our cover page from 
            var insertStream = IInputStream.createFromFile(mako, coverPageFile);

            // Create inserter
            IPDFPageInserter pageInserter = IPDFPageInserter.create(mako, inputStream);

            // Do the insertion - page numbers are zero-based
            pageInserter.insert(insertStream, 0, 0, 1);

            // And save
            pageInserter.save(IOutputStream.createToFile(mako, fileThatNowHasACover));

            Console.WriteLine($"Processing time: {stopWatch.ElapsedMilliseconds / 1000.0:F4} seconds.");
            stopWatch.Stop();
        }
        catch (MakoException e)
        {
            Console.WriteLine($"Mako exception thrown: {getEDLErrorString(e.m_errorCode)}");
            return 1;
        }

        catch (Exception e)
        {
            Console.WriteLine($"Exception thrown: {e}");
            return 1;
        }

        return 0;
    }
}

☑️ Conclusion

The IPDFPageInserter class provides a straightforward method for adding pages to an existing PDF, such as inserting a cover page. By following the outlined steps, users can efficiently manage PDF content, ensuring seamless integration of additional pages.

📚 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.