[ 
https://issues.apache.org/jira/browse/PDFBOX-4645?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16924250#comment-16924250
 ] 

Maruan Sahyoun commented on PDFBOX-4645:
----------------------------------------

In addition to adding the font to the acroform resources you need to make sure 
that the font is picked up by the field definition. Otherwise the field will 
still use the old font setting.

I'm copying the code I did to answer a question on stackoverflow 
[https://stackoverflow.com/questions/42903217/write-cyrillic-chars-into-pdf-form-fields-with-pdfbox/43002076?noredirect=1#comment80747364_43002076]

You need to adjust it to macth your font defintion.

 

[code]
 PDDocument doc = PDDocument.load(new File(...));
 PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
 PDResources formResources = acroForm.getDefaultResources();
 PDTrueTypeFont font = (PDTrueTypeFont) 
formResources.getFont(COSName.getPDFName("Ubuntu"));

// here is the 'magic' to reuse the font as a new font resource
 TrueTypeFont ttFont = font.getTrueTypeFont();

PDFont font2 = PDType0Font.load(doc, ttFont, true);
 ttFont.close();

formResources.put(COSName.getPDFName("F0"), font2);

PDTextField formField = (PDTextField) acroForm.getField("Text2");
 formField.setDefaultAppearance("/F0 0 Tf 0 g");
 formField.setValue("öäüинформацию");

doc.save(...);
 doc.close();
 [code]

> impossible to use own TrueTypeFonts in acroForm (and font also not embedded)
> ----------------------------------------------------------------------------
>
>                 Key: PDFBOX-4645
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4645
>             Project: PDFBox
>          Issue Type: Bug
>          Components: AcroForm, PDModel
>    Affects Versions: 2.0.12, 2.0.13, 2.0.14, 2.0.15, 2.0.16
>            Reporter: André Hertwig
>            Priority: Major
>
> Hi
> I have a PDF form and want to add additional fonts, because the default fonts 
> does not allow cyrillic or greek chars for example.
> I add the font as following (LiberationSans-Regular):
> {code}
> PDResources formResources = acroForm.getDefaultResources();
> try (InputStream defaultTTFont1 = new FileInputStream(myFontFile)){
>       
>       PDFont font2 = PDType0Font.load(pdfDocument, defaultTTFont1, true);
>       COSName fontName = formResources.add(font2);
> }
> acroForm.setDefaultResources(formResources);
> {code}
> I debugged it, the valiable ttf is set.
> When filling a text field with for example cyrillic letters an exception 
> occurs, than I change the defaultAppereance of that field using the initially 
> added font.
> But the original TTF will not really used, instead there wil be created new 
> font classes with a TTF of null, so it seems to be impossible to use a 
> different TTF.
> Furthermore the font will not be embedded in the resulting PDF.
> {code}
> PDCIDFontType2.<init>(COSDictionary, PDType0Font, TrueTypeFont) line: 75      
> <- initialized with TrueTypeFont = null
> PDCIDFontType2.<init>(COSDictionary, PDType0Font) line: 62    
> PDFontFactory.createDescendantFont(COSDictionary, PDType0Font) line: 139      
> PDType0Font.<init>(COSDictionary) line: 192   
> PDFontFactory.createFont(COSDictionary, ResourceCache) line: 97       
> PDResources.getFont(COSName) line: 146        
> PDDefaultAppearanceString.processSetFont(List<COSBase>) line: 173     
> PDDefaultAppearanceString.processOperator(Operator, List<COSBase>) line: 132  
> PDDefaultAppearanceString.processAppearanceStringOperators(byte[]) line: 108  
> PDDefaultAppearanceString.<init>(COSString, PDResources) line: 86     
> PDTextField(PDVariableText).getDefaultAppearanceString() line: 93     
> AppearanceGeneratorHelper.<init>(PDVariableText) line: 100    
> PDTextField.constructAppearances() line: 263  
> PDTextField(PDTerminalField).applyChange() line: 228  
> PDTextField.setValue(String) line: 219        
> {code}
> As a result of this behavoir I get the following warning:
> {code}
> WARN  org.apache.pdfbox.pdmodel.font.PDCIDFontType2 - Using fallback font 
> LiberationSans for CID-keyed TrueType font LiberationSans-Regular
> {code}
> When opening the PDF afterwards, I will get an error from AcrobatReader, that 
> the font "LiberationSans-Regular" could be found, because the font is not 
> embedded... like my own fonts..
> I tried it back to version 2.0.12 - always the same behavior.



--
This message was sent by Atlassian Jira
(v8.3.2#803003)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to