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

Full code available at IPDFPageInserterExample.cs.

C#
// 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));

☑️ 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.