Q: I attempted to do what you described (in C# for .NET 2005 -
attached). I'm trying to embed the specified font, OCRAExtended
font, but subset the part of the font that we're actually using. This
program should run and manipulate the ExampleScanline.pdf into
Output.pdf (in the bin/Debug subfolder of the solution).
------------------
A: The problem is that pdfdoc.GetSDFDoc().Swap() is conflicting with
Font caching. To work around it you could close, then re-open the
document, but a more elegant solution is to simply change font
reference using fonts.Put(fontid, new_font.GetSDFObj()); This is shown
in the attached code sample.
using System;
using System.Text;
using pdftron;
using pdftron.PDF;
using pdftron.SDF;
using pdftron.PDF.PDFA;
namespace TestEmbedFont
{
class Program
{
const string INPUT_FILE = @"ExampleScanline.pdf";
const string FONT_FILE_NAME = @"OCRAEXT.TTF";
const string OUTPUT_FILE_NAME = @"Output.pdf"; // where should
the file be written. Ends up in the bin\Debug subfolder when run in
VS2005
static void Main(string[] args)
{
// Initialize PDFNet
PDFNet.Initialize();
// Load the source PDF document
PDFDoc pdfDoc = new PDFDoc(INPUT_FILE);
pdfDoc.InitSecurityHandler();
pdftron.PDF.Font new_font =
pdftron.PDF.Font.CreateTrueTypeFont(pdfDoc, FONT_FILE_NAME, true,
true);
// Loop through the pages
for (PageIterator itr = pdfDoc.GetPageIterator();
itr.HasNext(); itr.Next())
{
// Find the font dictionary and switch the font
Page pg = itr.Current();
System.Diagnostics.Debug.WriteLine("Begin page
processing.");
Obj res = pg.GetResourceDict();
if (res == null) continue;
Obj fonts = res.FindObj("Font");
if (fonts == null) continue;
for (DictIterator i = fonts.GetDictIterator();
i.HasNext(); i.Next())
{
pdftron.PDF.Font font = new pdftron.PDF.Font
(i.Value());
if (font.GetType() !=
pdftron.PDF.Font.Type.e_TrueType) continue;
string fname = font.GetName();
System.Diagnostics.Debug.WriteLine("Begin font
processing (" + fname + ").");
if (fname != "OCRAExtended") continue;
// ... we encountered 'MyFont', so embed the
font...
// ... possibly check for a custom tag or similar
fonts.Put(i.Key().GetName(), new_font.GetSDFObj
());
}
// Now create a temporary page and write every element
from the source page onto the target page
Page tempPage = pdfDoc.PageCreate();
ElementReader reader = new ElementReader();
ElementWriter writer = new ElementWriter();
writer.Begin(tempPage);
reader.Begin(pg);
Element element = null;
while ((element = reader.Next()) != null)
writer.WriteElement(element);
reader.End();
writer.End();
}
// Save the edited PDF
pdfDoc.Save(OUTPUT_FILE_NAME,
SDFDoc.SaveOptions.e_remove_unused);
// Finish up
pdftron.PDFNet.Terminate();
// Sample code to play with the PDF compliance piece
// PDFNet.Initialize();
// PDFACompliance compliance = new PDFACompliance(true,
INPUT_FILE, null, PDFACompliance.Conformance.e_Level1B, null, 1000,
false);
// compliance.SaveAs(OUTPUT_FILE_NAME, true);
// PDFNet.Terminate();
}
}
}