Q: We are trying to stamp some text on all pages in the document and
merge this document with another document using the code below.
For some reason the resulting PDF file is significantly larger. I know
that font embedding makes file larger, but in some cases we
significant increase in file size. Please advise.
------------Start Sample code----------------
PDFDoc doc=null;
PDFDoc tempDoc=null;
string sourceFile=@"F:\test\input.pdf";
string destFile=@"F:\test\output.pdf";
try
{
doc=new PDFDoc(sourceFile);
tempDoc=new PDFDoc();
int numPages=doc.GetPagesCount();
Element element=null;
ElementBuilder eb=new ElementBuilder();
ElementWriter writer = new ElementWriter();
Obj form=null;
for(int start=1;start<=numPages;start++)
{
Page page=doc.PageFind(start).Current();
writer.Begin(page);
element
=eb.CreateTextBegin(pdftron.PDF.Font.CreateTrueTypeFontdoc,"font.fft" ,true,false),
12);
element.SetTextMatrix(1, 0, 0, 1, 200,200);
writer.WriteElement(element);
element = eb.CreateTextRun("Hello World");
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextEnd());
form = writer.End();
form.Put("Subtype", Obj.CreateName("Form"));
form.Put("txt", Obj.CreateBool(true));
form.Put("single", Obj.CreateBool(true));
element=eb.CreateForm(form);
writer.WriteElement(element);
writer.End();
tempDoc.PagePushBack(page);
}
tempDoc.Save(destFile,0);
}
catch(PDFNetException pex)
{
MessageBox.Show(pex.ToString());
}
------
A: The file size is getting larger because you are creating a new font
for every PDF page you stamp. You can speed us the code and cut down
on the file size by creating the font only once (before the ‘for’
loop). For example:
...
pdftron.PDF.Font fnt =
pdftron.PDF.Font.CreateTrueTypeFontdoc,"font.fft" ,true,false);
for(int start=1;start<=numPages;start++) {
Page page=doc.PageFind(start).Current();
writer.Begin(page);
element =eb.CreateTextBegin(fnt,12);
element.SetTextMatrix(1, 0, 0, 1, 200,200);
...
}
...