Outlining PDF Fonts in your C# Applications
Has this ever happened to you or one of your clients? - An important PDF is sent to a print supplier for duplication and ultimately mass distribution. A week goes by while the supplier gets your job in the queue. Then, they call and say the file is unusable because when they open it in their system, all the fonts have changed, and the file is now virtually unreadable.
The deadline is quickly approaching. Normally redoing these documents would be a very time-consuming process, potentially pushing a long-awaited release of information and sales material because the print provider asked to 'remove the fonts.' Though the deadline is looming, you understand the issue; they don't have the same fonts installed in their system like the system that created the PDF in the first place. Lucky for your team, you have GcPdf as part of your tool kit, so you can quickly and easily convert the file by outlining the fonts. Or, in other words, putting them into glyphs, ensuring the document looks the same as its original format, without having to worry about fonts being installed at your print provider.
This blog describes the steps to draw all the pages of an original PDF with text font information to a temporary document. During this drawing process, the text is drawn as graphics paths. Once the newly drawn document is saved, the PDF will not contain any fonts because all the glyphs from the original PDF will be drawn/rendered as graphic paths making the text into an image.
The steps below are provided to convert a PDFs text to an outline.
- Load sample PDF to a temporary document
- Draw the pages of the pdf
- Draw text as a graphics path
- Save the document
Load Sample PDF to a Temporary Document
First, create a temporary PDF document.
// 1.1) Create a temp document
var srcDoc = new GcPdfDocument();
Next, load a pdf into the temporary document using a specified file stream. Below is a sample PDF called Wetlands.pdf, used for this example; it contains text with font format. Load the sample PDF to the temporary document using the Load method of the GcPdfDocument’s class.
using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")))
{
// 1.1) Create a temp document
var srcDoc = new GcPdfDocument();
// 1.2) Load PDF containing font info to temp document
srcDoc.Load(fs);
}
Draw the Pages of the PDF
Now, draw all pages of the Wetlands.pdf document to the temp document by first creating a new page with a specified size:
// 2.) Draw all pages of the source document on pages of the new PDF:
foreach (var srcPage in srcDoc.Pages) {
// 2.1) Create a new page with a specified size
var page = doc.Pages.Add(srcPage.Size);
}
Then, draw the pages specifying the graphics and draw bounds:
// 2.) Draw all pages of the source document on pages of the new PDF: f
oreach (var srcPage in srcDoc.Pages) {
// 2.1) Create a new page with a specified size
var page = doc.Pages.Add(srcPage.Size);
// 2.2) Draw the pages
srcPage.Draw(page.Graphics, srcPage.Bounds);
}
Draw Text as a Graphics Path
Next, set a value indicating that the strings and GrapeCity.Documents.Text.TextLayout object are to be rendered using graphic primitives instead of specialized text, drawing the text as a graphics path.
This is accomplished by setting the Graphics.DrawTextAsPath to True:
GcPdfDocument doc = new GcPdfDocument();
using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")))
{
// 1.1) Create a temp document
var srcDoc = new GcPdfDocument();
// 1.2) Load PDF containing font info to temp document
srcDoc.Load(fs);
// 2.) Draw all pages of the source document on pages of the new PDF:
foreach (var srcPage in srcDoc.Pages)
{
// 2.1) Create a new page with a specified size
var page = doc.Pages.Add(srcPage.Size);
// 3) Setting Graphics.DrawTextAsPath to true makes all glyphs draw as graphics paths instead of rendering text using fonts:
page.Graphics.DrawTextAsPath = true;
// 2.2) Draw the source page on the target:
srcPage.Draw(page.Graphics, srcPage.Bounds);
}
Save the Document
Finally, save the temporary document using the Save method:
GcPdfDocument doc = new GcPdfDocument();
using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")))
{
// 1.1) Create a temp document
var srcDoc = new GcPdfDocument();
// 1.2) Load PDF containing font info to temp document
srcDoc.Load(fs);
// 2.) Draw all pages of the source document on pages of the new PDF:
foreach (var srcPage in srcDoc.Pages)
{
// 2.1) Create a new page with a specified size
var page = doc.Pages.Add(srcPage.Size);
// 3) Setting Graphics.DrawTextAsPath to true makes all glyphs draw as graphics paths instead of rendering text using fonts:
page.Graphics.DrawTextAsPath = true;
// 2.2) Draw the source page on the target:
srcPage.Draw(page.Graphics, srcPage.Bounds);
}
// 4.) Save the newly drawn document
doc.Save(stream);
return doc.Pages.Count;
}
Notice with the newly saved PDF, selecting or copying text, or searching the text is not possible. This is because when text is converted to an outline, the text is made into shapes, so they will appear the same on any machine.
GcPdf is an extremely powerful cross-platform solution for document management. Check out our live demos showing the above example and other features of GcPdf: Convert text to glyph outlines (remove all fonts).