Q: I am using Font.CreateCIDTrueTypeFont on Windows with a font source
that has the bold styling applied, yet the output does not produce
bold text. With the provided example code, and my limited knowledge
of PDF file format, I would expect to see a font entry like "/
BaseFont /FranklinGothicBook,Bold" somewhere in the file, yet I do not
see it. What am I doing wrong?
public void TestBoldFont()
{
PDFNet.Initialize("Omitted");
PDFDoc doc = new PDFDoc();
Page page = doc.PageCreate();
doc.PagePushBack(page);
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(page);
string fontFace = "Franklin Gothic Book";
string testText = "Quick Brown Lazy Fox";
System.Drawing.FontFamily fontFamily = new
System.Drawing.FontFamily(fontFace);
System.Drawing.Font font = new System.Drawing.Font(fontFamily,
14);
System.Drawing.Font fontBold = new System.Drawing.Font(fontFamily,
14,
System.Drawing.FontStyle.Bold);
pdftron.PDF.Font tronFont = Font.CreateCIDTrueTypeFont(doc, font,
true, true);
pdftron.PDF.Font tronFontBold = Font.CreateCIDTrueTypeFont(doc,
fontBold, true,
true);
Element textBlockBegin = builder.CreateTextBegin(tronFont, 14);
writer.WriteElement(textBlockBegin);
Element textHeader = builder.CreateUnicodeTextRun(testText + "
This is normal
style");
double x = inchesToPDFUnits(.5+ 0.35);
double y = inchesToPDFUnits(10 + 0.1);
textHeader.SetTextMatrix(1, 0, 0, 1, x, y);
//Graphics State needed to set the color
GState graphicsState = textHeader.GetGState();
graphicsState.SetTextRenderMode
(GState.TextRenderingMode.e_fill_text);
graphicsState.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
graphicsState.SetFillColor(new ColorPt(64 / 255.0, 100 / 255.0,
126 / 255.0));
writer.WriteElement(textHeader);
Element textBlockEnd = builder.CreateTextEnd();
writer.WriteElement(textBlockEnd);
//Bold Test
textBlockBegin = builder.CreateTextBegin();
writer.WriteElement(textBlockBegin);
textHeader = builder.CreateUnicodeTextRun(testText + " This is
bold style");
x = inchesToPDFUnits(.5 + 0.35);
y = inchesToPDFUnits(9.5 + 0.1);
textHeader.SetTextMatrix(1, 0, 0, 1, x, y);
//Graphics State needed to set the color
graphicsState.SetFont(tronFontBold, 14); //try setting font on
run, rather than
//text block
graphicsState = textHeader.GetGState();
graphicsState.SetTextRenderMode
(GState.TextRenderingMode.e_fill_text);
graphicsState.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
graphicsState.SetFillColor(new ColorPt(64 / 255.0, 100 / 255.0,
126 / 255.0));
writer.WriteElement(textHeader);
textBlockEnd = builder.CreateTextEnd();
writer.WriteElement(textBlockEnd);
writer.Flush();
writer.End();
string path = @"C:\temp\fontBold.pdf";
doc.Save(path , pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
PDFNet.Terminate();
Process.Start(path);
}
internal double inchesToPDFUnits(double inches)
{
return inches * 72;
}
-----------
A: Is it safe to assume that your system is missing bold version for
"Franklin Gothic Book"? If this is a correct assumption PDFNet is
embedding the font which most closely matches description provided in
'System.Drawing.Font'. Unlike GDI, PDF format does not have a way of
simulating/synthesizing font styles. So bold, italic styles require
separate font files (which are sometimes wrapped into a single file
called True Type Collection - ttc; or which are sometimes stored as
separate files - e.g. as in case of Arial font). PDFNet searches for
all fonts in "Franklin Gothic Book" family and then selects a specific
font based on a style.
In the absence of a specific fonts, some PDFNet users simulate the
bold appearance by enabling font stroking (element.GetGState
().SetTextRenderMode(GState.TextRenderingMode.e_fill_stroke_text);
element.GetGState().SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
element.GetGState().SetStrokeColor(same_as_fillcolor)) and setting
certain stoke width (element.GetGState().SetLineWidth(width)) which is
proportionate to font weight. Similarly, italic style can be simulated
by adding a slight shear component to the text matrix
(element.SetTextmatrix()).