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);
inputStream
is the PDF that will receive the inserted page(s).insertStream
is 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
Also available at IPDFPageInserterExample.cs.
/* -----------------------------------------------------------------------
* <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.