Q: I want to set the font and size of an new empty textfield without
embedding fonts.
a. When using the code below, the CourierBold font is only used when
the field has focus. (Viewed in AcrobatReader9)
b. If I add a default value string Dim f1 As Field = doc.FieldCreate
("xxxxx", Field.Type.e_text,”x”). It complains that I miss a resource.
If I then embed the font, it works. But I do not want to embed any
font as I will use standard font that exist on every windows computer.
And I do not want any prefilled data in the field.
Example-code:
Dim f1 As Field = doc.FieldCreate("xxxxx", Field.Type.e_text)
f1.SetJustification
(PDF.Field.TextJustification.e_left_justified)
f1.SetFlag(Field.Flag.e_no_scroll, True)
Dim annot1 As Annot = Annot.CreateWidget(sdfdoc, New Rect
(100, 100, 200, 150), f1)
Dim da_str As String = " /Courier-Bold 10 Tf "
annot1.GetSDFObj().PutString("DA", da_str)
f1.EraseAppearance()
f1.RefreshAppearance()
blank_page.AnnotPushBack(annot1)
How do I solve this?
--------------
A: The following code demonstrates how to create a text field widget
with a given font and font size. Although a font is created it is not
embedded (since it is a standard font). In case you wanted to embed
(and optionally subset) a font you could do so using other
pdftron.PDF.Font.Create???() methods.
PDFNet.Initialize()
PDFNet.SetResourcesPath(...)
Dim doc As PDFDoc = New PDFDoc
Dim blank_page As Page = doc.PageCreate() ' Create a blank page and
some new fields (aka "AcroForms").
Dim f1 As Field = doc.FieldCreate("xxxxx", Field.Type.e_text)
f1.SetJustification(PDF.Field.TextJustification.e_left_justified)
f1.SetFlag(Field.Flag.e_no_scroll, True)
f1.SetValue("Hello!")
Dim annot1 As Annot = Annot.CreateWidget(doc, New Rect(100, 100, 200,
150), f1)
' Set the default appearance.
annot1.GetSDFObj().PutString("DA", " /Courier-Bold 10 Tf ")
' Create the font resource for the default appearance.
Dim resources As Obj = doc.GetAcroForm().PutDict("DR") ' <======
Dim f As Font = Font.Create(doc,
Font.StandardType1Font.e_courier_bold)
resources.PutDict("Font").Put("Courier-Bold", f.GetSDFObj())
blank_page.AnnotPushBack(annot1)
doc.PagePushBack(blank_page)
doc.RefreshFieldAppearances()
doc.Save(output_path + "forms_test1.pdf", 0)
doc.Close()
------------------
You may also want to add new font resources to an existing font
dictionary instead of replacing DR (Default Resources) with new value
each time. For example, you may want to use the following pseudocode:
void AddFontResource(string font_res_name, Font font)
{
Obj acroforms = doc.GetAcroForm();
If (acroforms == null) acroforms = doc.CreateIndirectDict();
Obj dr = acroforms.FindObj("DR");
If (dr == null) dr = acroforms.PutDict("DR");
Obj fnt = dr.FindObj("Font");
If (fnt == null) dr = dr.PutDict("Font");
fnt.Put(font_name, font.GetSDFObj());
}
Then instead of the three lines in the above code, add a new font
resource as follows:
AddFontResource("Courier-Bold", Font.Create(doc,
Font.StandardType1Font.e_courier_bold))