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
Open input streams
CODEvar inputStream = IInputStream.createFromFile(mako, fileThatNeedsACover); var insertStream = IInputStream.createFromFile(mako, coverPageFile);inputStreamis the PDF that will receive the inserted page(s).insertStreamis the PDF from which pages will be copied.
Create a page inserter for the target PDF
CODEIPDFPageInserter pageInserter = IPDFPageInserter.create(mako, inputStream);Insert the page(s)
CODEpageInserter.insert(insertStream, 0, 0, 1);Parameters (all zero-based):
insertStream: source PDF (the cover)0: destination index (insert at the very front of output pdf)0: source start page index (take from page 0 = first page of input pdf)1: number of pages to insert
Save the result
CODEpageInserter.save(IOutputStream.createToFile(mako, fileThatNowHasACover));Writes the combined PDF to the output file.
⌨️ Sample Code
Full code available at IPDFPageInserterExample.cs.
// 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.