Am 07.03.2017 um 09:05 schrieb chitgoks:
I have a few last questions. this is with regards to FreeText.

1) how to set the font, font size and foreground color?

PDAnnotationMarkup's setColor will set it as background if sub type is free
text. but i wish to change the text color. In a different library i used,
it was through using an appearance. But PDAppearanceDictionary does not
contain methods on setting font, font size and color.

2) If my dimension for PDRectangle is short, free text will cut off some
text unless i click it in acrobat then its width will be recalculated and
fit to the text. Is there a way to make the rectangle auto-fit based on the
free text's text width and height?


1) The /C is explained on page 384 and it isn't what you want. What you want is the /DA entry, which is explained on page 396. I didn't find any method, so what you'd have to do is something like this:

annot.getCOSObject().setString(COSName.DA, "1 0 0 rg /Arial 9 Tf");

1 0 0 = red
/Arial = font
9 = font size

You'd need to add the font to the resource directory of /Acroform/DR. But I noticed it also works without,. In any case, display is not supported by PDFBox when /AP is missing, but it will work in Adobe Reader.

Here's code that works in the AddAnnotations example:

            PDAnnotationMarkup freeTextMark = new PDAnnotationMarkup();
freeTextMark.getCOSObject().setName(COSName.SUBTYPE, PDAnnotationMarkup.SUB_TYPE_FREETEXT); PDColor yellow = new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE); freeTextMark.setColor(yellow); // this sets background only (contradicts spec) position = new PDRectangle(); // Reuse the variable, but note it's a new object!
            position.setLowerLeftX(1 * INCH);
            position.setLowerLeftY(ph - 5f * INCH - INCH);
            position.setUpperRightX(pw - INCH);
            position.setUpperRightY(ph - 5f * INCH);
            freeTextMark.setRectangle(position);
            freeTextMark.setTitlePopup("Author");
            freeTextMark.setContents("This is the text");
            // Blue RGB color, "Helv" font, 20 point
freeTextMark.getCOSObject().setString(COSName.DA, "0 0 1 rg /Helv 20 Tf");
            annotations.add(freeTextMark);

            // set the "Helv" font
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
            if (acroForm == null)
            {
                acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
            }
            PDResources dr = acroForm.getDefaultResources();
            if (dr == null)
            {
                dr = new PDResources();
                acroForm.setDefaultResources(dr);
            }
            dr.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);


This displays a box with blue text and blue border and yellow background.

Yes, a lot of methods are missing there. These annotation types are a bit like the far end of a garden that hasn't been worked on yet.

2) I couldn't find any.

Tilman


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

Reply via email to