Caching and reusing fonts to speed up PDF processing (PDF generation or editing).

Q: I have noticed a fair bit of disk activity on our server. Looking
into it I believe our application is doing a lot of opening up of font
files over and over again.
I am wondering if there is a way that I can cache the font files
within the application.

I did not see a method that takes some sort of stream or something.
Any suggestions would be appreciated.

Our current code looks like:

PDFDoc document = new PDFDoc();
mediaBox = new Rect(0, 0, 100, 100);
Page page = document.PageCreate(mediaBox); document.PagePushFront
(page);

// Font is created...
pdftron.PDF.Font font = pdftron.PDF.Font.
CreateTrueTypeFont(document, PDFUtilities.ServerMapPath("/fonts/" +
fontFilename));

ElementBuilder builder = new ElementBuilder();

builder.CreateTextBegin();
Element element = builder.CreateTextRun(text, font, fontSize);
----
A: Using PDFNet library you can share Fonts and other resources (e.g.
images, XObjects, ICC profiles, shadings, etc) throughout the
document.

Instead of creating a font multiple times (using
pdftron.PDF.Font.Create() methods) create a font only one time and
then keep on referencing the same font/resource throughout the
document. For example, in your class you could declare the following

variables:

private:
  pdftron.PDF.Font myfont = null;
  pdftron.PDF.ElementBuilder builder = null;
  pdftron.PDF.ElementWriter writer = null;

...
  MyClassInit() {
   builder = new ElementBuilder();
   writer = new ElementWriter();
   myfont = Font.CreateTrueTypeFont(doc, fontpath + "myfont.ttf");
  }

//
void AddSomeText(Page page)
{
// Begin writing to the page
writer.Begin(page);
builder.Reset();

// Begin writing a block of text
Element element = builder.CreateTextBegin(myfont, 12);
writer.WriteElement(element);

string txt = "Hello World!";
element = builder.CreateTextRun(txt);
// Scale-up text 5 times and shift it by (0,600)
element.SetTextMatrix(5, 0, 0, 5, 0, 600); writer.WriteElement
(element);

// Finish the block of text
writer.WriteElement(builder.CreateTextEnd());
writer.End();
}

All text created using AddSomeText() will reference the single font
instance "myfont". Reusing fonts will result in smaller PDF files and
faster execution.

Also you will notice that in the above code ElementBuilder &
ElementWriter are being reused similar to the font resource.

---
It is also possible to speed-up font reading and embedding across
multiple PDF documents. In this case you can create a global scrap PDF
document. Then create fonts that you will use across multiple
documents, and use pdfdoc.GetSDFDoc().ImportObj(font.GetSDFObj(),
true). Something ala:

PDFDoc cache_doc = new PDFDoc();
Obj cache_font1 = Font.Create...(cache_doc...).GetSDFObj();
...

PDFDoc doc1 = new PDFDoc();
...
// Import cached font in doc1.
Obj my_doc_font = doc1.GetSDFDoc().ImportObj(cache_font1.GetSDFObj(),
true);

// Create two instances of this font in doc1 (both instances will
refer to the same font).
Font my_font_1 = new Font(my_doc_font);
....

Font my_font_2 = new Font(my_doc_font);
....