How to use the IPDFPageInserter class
This simple example shows how to use the IPDFPageInserter
class, to add a cover page to an existing PDF.
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 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;
}
}