How do I produce small PDFs when embedding fonts?

Q: We are producing PDF’s using PDFNet SDK, but some files can get
very large because they contain Embedded Fonts. How can we produce PDF
files that are as slim as possible?
-----
A: Using PDFNet you should be able to create PDFs with optimal file
size. If the generated files are large due to fonts you should make
sure that you are not replicating the same font(s) throughout the
document. For example, if you are creating a multi-page document that
contains text in the same font you should create only a single font
instance (using pdftron.PDF.Font.Create...()) and perhaps storing it
is a member variable in your document generation class. Another thing
to consider when creating fonts are last two boolean parameters that
can be passed in Font.Create...() methods. The first flag specifies
whether the font should be embedded or not. The second flag specifies
whether the embedded font should be subsetted or not. For accurate PDF
viewing it is important to embed the font, but you can subset all
fonts in order to decrease the total file size.

Q: I have followed the last suggestion below by setting the two
Boolean parameters, but I am not sure about the result.

What I need to do is have the fonts embedded once at the beginning of
a PDF file ONLY, then Unembedded the rest of the way through the PDF
document to save size.

Can you please clarify if setting these two flags will achieve this,
and what setting I need? If not, is there another way to do this.
------
A: You need to use Font.Create???() method only once and keep on
reusing the same font object throughout your PDF generation code. For
example:

PDFNet.Initialize();
PDFNet.SetResourcesPath(...);
...

PDFDoc doc = new PDFDoc();

// Create a font that fill be used throughout the document
Font fnt = Font.Create(doc, Font.StandardType1Font.e_times_roman)

ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Element element;

// Start the first page -----------------
Page page = doc.PageCreate(new Rect(0, 0, 612, 794));
writer.Begin(page);

element = eb.CreateTextBegin(fnt, 12);
writer.WriteElement(element);

string data = "Hello World!";
element = eb.CreateTextRun(data);
element.SetTextMatrix(10, 0, 0, 10, 0, 600);
element.GetGState().SetLeading(15); // Set the spacing between lines
writer.WriteElement(element);

writer.WriteElement(eb.CreateTextNewLine()); // New line

// Draw Hello World once again on the next line
writer.WriteElement(eb.CreateTextRun(data));
writer.WriteElement(eb.CreateTextEnd());

writer.End();
doc.PagePushBack(page);

// Start the second page -----------------
page = doc.PageCreate();
writer.Begin(page);

// Note. we are reusing the font here!
element = eb.CreateTextBegin(fnt, 24);
writer.WriteElement(element);

string data = "Second Page!";
element = eb.CreateTextRun(data);
element.SetTextMatrix(1, 0, 0, 1, 0, 600);
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextEnd());

writer.End();
doc.PagePushBack(page);

// Save the document, dispose objects.
writer.Dispose();
eb.Dispose();
doc.Save("my.pdf", SDFDoc.SaveOptions.e_linearized);
doc.Close();

If your PDF generation method is more complex you could store
ElementBuilder, ElementWriter, Font, and other variables as members of
the PDF generation class. For example:

public class MyPDFGenerator {

private PDFDoc doc = null;
private Font fnt = null;
private ElementBuilder eb = null;
private ElementWriter writer = null;

public void Init() {
  doc = new PDFDoc();
  font = Font.CreateTrueTypeFont(doc, input_path + "font.ttf", true,
false);
  eb = new ElementBuilder();
  writer = new ElementWriter();
}

void AddPage() {
  writer.Begin(page);

  element = eb.CreateTextBegin(fnt, 120);
  writer.WriteElement(element);

  element = eb.CreateTextRun("Hello World!");
  element.SetTextMatrix(1, 0, 0, 1, 0, 600);

  writer.WriteElement(element);

  writer.WriteElement(eb.CreateTextEnd());
  writer.End();
  doc.PagePushBack(page);
}

public void Save(string fname) {
   doc.Save(fname, SDFDoc.SaveOptions.e_linearized);
   doc.Close();
   writer.Dispose();
   eb.Dispose();
}

};

In the above pseudocode the output PDF document will contain only a
single subsetted font. This means that the embedded font will contain
a subset of glyphs used throughout the PDF document and will most
likely be smaller than the input font.