Removing duplicate fonts after PDF merging

Q: We are currently developing an application where one of the steps
is to merge PDFs into a single file. By completion the result looks as
expected but with duplicated fonts are being embedded. Similar fonts
are used in two PDF files merged. Can you tell me if this behavior is
by design or there is a code (APIs) I need to use to filter/adjust the

The code used to merge is next:

private void Import( PDFDoc pdf, PDFDoc insert )
var pageIterator = insert.GetPageIterator();
var pages = new ArrayList();
while( true )
pages.Add( pageIterator.Current() );
if( !( pageIterator.HasNext() ) )

var importedPages = pdf.ImportPages( pages );
var mediaBox = pdf.GetPage( 1 ).GetMediaBox();

using( var builder = new ElementBuilder() )
using( var writer = new ElementWriter() )
using( var m2D = new Matrix2D() )

m2D.Translate( pdf.GetPage( 1 ).GetPageWidth() / 2 -
insert.GetPage( 1 ).GetPageWidth() / 2,

pdf.GetPage( 1 ).GetPageHeight() -
insert.GetPage( 1 ).GetPageHeight() );

for( var i = 0; i <
importedPages.Count - 1; ++i )
var newPage =
pdf.PageCreate( mediaBox );

writer.Begin( newPage );

var srcPage =
( Page )importedPages[ i ];
var element =
builder.CreateForm( srcPage );

element.GetGState().SetTransform( m2D );

writer.WritePlacedElement( element );

pdf.PagePushBack( newPage );

A: Since you are simply resizing the pages a more efficient solution
would be simply to change the dimensions of page media & crop box
(along the lines of PDFRect sample -

Also since you are merging multiple PDF documents into one you may
want to use ‘pdftron.PDF.Optimizer’ just after the merge and before
saving the final PDF to dist. Besides other things the optimizer
should identify and remove duplicate fonts, images, and other
resources. The only exception are subsetted fonts where different
document may have different subsets of the same font. In this case the
Optimizer will not try to merge different subsets into a single font
(because this is a fairly bug prone operation). For an example of how
to use Optimizer add-on, please see: