To work with a PDF document, you need a library that supports different kinds of fonts. DsPdf provides support for following font types:
To make sure that any of these listed fonts used in a layout can be viewed as it is after downloading or saving the file, DsPdf library provides the following techniques:
While creating a PDF file, you may want to use fonts other than the standard fonts. To do so, usually you need to add a font from C:\Windows\Fonts directory. Registering the whole directory every time you want to use different fonts can become unmanageable and time consuming. DsPdf library solves this issue with FontCollection class, that adds global font management services to your application. The FontCollection class provides font related services to different program modules. Fonts can be registered with a FontCollection via RegisterFont or RegisterDirectory methods. Both these methods register disk files, and do not load the actual fonts into memory. This saves space and improves performance as fonts are loaded only when needed.
DsPdf supports the 14 standard fonts that are mentioned in the PDF specification 2.0 (section 9.6.2). To use these standard PDF fonts, specify one of the standard fonts using the StandardFonts enum members.
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; var textFormat = new TextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 14 }; // Render text using DrawString method g.DrawString("1. Test string.", textFormat, new PointF(72, 72)); // Save Document doc.Save(stream); } |
To use an external font file for applying fonts:
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { GrapeCity.Documents.Text.Font gabriola = GrapeCity.Documents.Text.Font.FromFile("Gabriola.ttf"); // Now that we have our font, use it to render some text TextFormat tf = new TextFormat() { Font = gabriola, FontSize = 16 }; GcPdfDocument doc = new GcPdfDocument(); GcPdfGraphics g = doc.NewPage().Graphics; g.DrawString("Sample text drawn with font {gabriola.FontFamilyName}.", tf, new PointF(72, 72)); // Save Document doc.Save(stream); } |
To embed font in a PDF file, you can use the FontEmbedMode property provided by the GcPdfDocument class. By default, font subsets containing only glyphs used in the document, are embedded. However, this can be changed and set to embed full font using FontEmbedMode enum, which leads to huge file size.
C# |
Copy Code
|
---|---|
// Use GcPdfDocument object to set the FontEmbedMode
doc.FontEmbedMode = FontEmbedMode.EmbedFullFont;
|
To use FontCollection:
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { // Create a FontCollection instance: FontCollection fc = new FontCollection(); // Get the font file using RegisterFont method: fc.RegisterFont("georgia.ttf"); // Generate a sample document using the font collection to provide fonts: var doc = new GcPdfDocument(); var page = doc.Pages.Add(); var g = page.Graphics; // Allow the TextLayout created internally by GcGraphics // to find the specified fonts in the font collection: g.FontCollection = fc; // Use GcGraphics.DrawString to show that the font collection is also used // by the graphics once the FontCollection has been set on it: g.DrawString("Text rendered using Georgia bold, drawn by GcGraphics.DrawString() method.", new TextFormat() { FontName = "georgia", FontSize = 10 }, new PointF(72, 72 * 4)); // Done: doc.Save(stream); } |
To use fallback fonts:
C# |
Copy Code
|
---|---|
public void CreatePDF(Stream stream) { // Set up GcPdfDocument: GcPdfDocument doc = new GcPdfDocument(); GcPdfGraphics g = doc.NewPage().Graphics; // Set up some helper vars for rendering lines of text: const float margin = 36; PointF ip = new PointF(margin, margin); // Initialize a text format with one of the standard fonts. Standard fonts are minimal // and contain very few glyphs for non-Latin characters. TextFormat tf = new TextFormat() { Font = StandardFonts.Courier, FontSize = 14 }; // Get the list of fallback font families: string[] fallbacks = FontCollection.SystemFonts.GetFallbackFontFamilies(); // Add the original list of fallback font families to global SystemFonts: FontCollection.SystemFonts.AppendFallbackFontFamilies(fallbacks); // On some systems, default system fallbacks might not provide Japanese glyphs, // so we add our own fallback: Font arialuni = Font.FromFile(Path.Combine("Resources", "Fonts", "ARIALUNI.TTF")); FontCollection.SystemFonts.AppendFallbackFonts(arialuni); // As the fallback fonts are available, the Japanese text will render // correctly as an appropriate fallback will have been found: g.DrawString("Sample text with fallbacks available: あなたは日本語を話せますか?", tf, ip); ip.Y += 36; // Done: doc.Save(stream); } |
To list all fonts in a PDF document along with some of the key font properties, use the following code example. The example code loads the PDF document into a temporary document to get the listing of all fonts and creates a Font object from each of those PDF fonts, and reports whether the operation succeeded.
C# |
Copy Code
|
---|---|
// Open an arbitrary PDF, load it into a temp document and get all fonts: using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "Test.pdf"), FileMode.Open, FileAccess.Read)) { var doc1 = new GcPdfDocument(); doc1.Load(fs); var fonts = doc1.GetFonts(); tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:"); tl.AppendLine(); int i = 0; foreach (var font in fonts) { var nativeFont = font.CreateNativeFont(); tl.Append($"{i}:\tBaseFont: {font.BaseFont}; IsEmbedded: {font.IsEmbedded}."); tl.AppendParagraphBreak(); if (nativeFont != null) tl.AppendLine($"\tCreateNativeFont succeeded, family: {nativeFont.FontFamilyName};" + $" bold: {nativeFont.FontBold}; italic: {nativeFont.FontItalic}."); else tl.AppendLine("\tCreateNativeFont failed"); tl.AppendLine(); ++i; } tl.PerformLayout(true); |
DsPdf library supports variety of fonts that can work with multilingual characters to write a PDF in different languages. It also provides support for font features along with special characters, such as End-User Defined Characters (EUDC) support, surrogates, ligatures, and Unicode characters.
For more information about implementation of font features using DsPdf, see DsPdf sample browser.
Back to Top