You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (JIRA)" <ji...@apache.org> on 2019/07/10 17:42:00 UTC
[jira] [Updated] (PDFBOX-4594) Multiline field text with auto font
sizing should be size adjusted
[ https://issues.apache.org/jira/browse/PDFBOX-4594?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Tilman Hausherr updated PDFBOX-4594:
------------------------------------
Description:
The code to set the size of multiline sets it to 12 when the font size is 0 in /DA. However Adobe does this differently. Attached: file generated by the code below, and file after adding a "d" in the field with Adobe Reader.
The code to generate the file:
{code:java}
try (PDDocument document = new PDDocument())
{
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// Adobe Acrobat uses Helvetica as a default font and
// stores that under the name '/Helv' in the resources dictionary
PDFont font = PDType1Font.HELVETICA;
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), font);
// Add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
// Add and set the resources and default appearance at the form level
acroForm.setDefaultResources(resources);
// Acrobat sets the font size on the form level to be
// auto sized as default. This is done by setting the font size to '0'
String defaultAppearanceString = "/Helv 0 Tf 0 g";
acroForm.setDefaultAppearance(defaultAppearanceString);
// Add a form field to the form.
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
// Acrobat sets the font size to 12 as default
// This is done by setting the font size to '12' on the
// field level.
// The text color is set to blue in this example.
// To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
defaultAppearanceString = "/Helv 0 Tf 0 0 1 rg";
textBox.setDefaultAppearance(defaultAppearanceString);
textBox.setMultiline(true);
// add the field to the acroform
acroForm.getFields().add(textBox);
// Specify the widget annotation associated with the field
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 750, 200, 15);
widget.setRectangle(rect);
widget.setPage(page);
// set green border and yellow background
// if you prefer defaults, just delete this code block
PDAppearanceCharacteristicsDictionary fieldAppearance
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE));
fieldAppearance.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE));
widget.setAppearanceCharacteristics(fieldAppearance);
// make sure the widget annotation is visible on screen and paper
widget.setPrinted(true);
// Add the widget annotation to the page
page.getAnnotations().add(widget);
// set the field value
textBox.setValue("Sample field");
document.save("target/SimpleMultilineForm.pdf");
}{code}
was:
The code to set the size of multiline sets it to 12. However Adobe does this differently. Attached: file generated by the code below, and file after adding a "d" in the field with Adobe Reader.
The code to generate the file:
{code:java}
try (PDDocument document = new PDDocument())
{
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// Adobe Acrobat uses Helvetica as a default font and
// stores that under the name '/Helv' in the resources dictionary
PDFont font = PDType1Font.HELVETICA;
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), font);
// Add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
// Add and set the resources and default appearance at the form level
acroForm.setDefaultResources(resources);
// Acrobat sets the font size on the form level to be
// auto sized as default. This is done by setting the font size to '0'
String defaultAppearanceString = "/Helv 0 Tf 0 g";
acroForm.setDefaultAppearance(defaultAppearanceString);
// Add a form field to the form.
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
// Acrobat sets the font size to 12 as default
// This is done by setting the font size to '12' on the
// field level.
// The text color is set to blue in this example.
// To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
defaultAppearanceString = "/Helv 0 Tf 0 0 1 rg";
textBox.setDefaultAppearance(defaultAppearanceString);
textBox.setMultiline(true);
// add the field to the acroform
acroForm.getFields().add(textBox);
// Specify the widget annotation associated with the field
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 750, 200, 15);
widget.setRectangle(rect);
widget.setPage(page);
// set green border and yellow background
// if you prefer defaults, just delete this code block
PDAppearanceCharacteristicsDictionary fieldAppearance
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE));
fieldAppearance.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE));
widget.setAppearanceCharacteristics(fieldAppearance);
// make sure the widget annotation is visible on screen and paper
widget.setPrinted(true);
// Add the widget annotation to the page
page.getAnnotations().add(widget);
// set the field value
textBox.setValue("Sample field");
document.save("target/SimpleMultilineForm.pdf");
}{code}
> Multiline field text with auto font sizing should be size adjusted
> ------------------------------------------------------------------
>
> Key: PDFBOX-4594
> URL: https://issues.apache.org/jira/browse/PDFBOX-4594
> Project: PDFBox
> Issue Type: Improvement
> Components: AcroForm
> Affects Versions: 2.0.16
> Reporter: Tilman Hausherr
> Priority: Major
> Attachments: SimpleMultilineForm-ADOBE.pdf, SimpleMultilineForm.pdf
>
>
> The code to set the size of multiline sets it to 12 when the font size is 0 in /DA. However Adobe does this differently. Attached: file generated by the code below, and file after adding a "d" in the field with Adobe Reader.
> The code to generate the file:
> {code:java}
> try (PDDocument document = new PDDocument())
> {
> PDPage page = new PDPage(PDRectangle.A4);
> document.addPage(page);
> // Adobe Acrobat uses Helvetica as a default font and
> // stores that under the name '/Helv' in the resources dictionary
> PDFont font = PDType1Font.HELVETICA;
> PDResources resources = new PDResources();
> resources.put(COSName.getPDFName("Helv"), font);
> // Add a new AcroForm and add that to the document
> PDAcroForm acroForm = new PDAcroForm(document);
> document.getDocumentCatalog().setAcroForm(acroForm);
> // Add and set the resources and default appearance at the form level
> acroForm.setDefaultResources(resources);
> // Acrobat sets the font size on the form level to be
> // auto sized as default. This is done by setting the font size to '0'
> String defaultAppearanceString = "/Helv 0 Tf 0 g";
> acroForm.setDefaultAppearance(defaultAppearanceString);
> // Add a form field to the form.
> PDTextField textBox = new PDTextField(acroForm);
> textBox.setPartialName("SampleField");
> // Acrobat sets the font size to 12 as default
> // This is done by setting the font size to '12' on the
> // field level.
> // The text color is set to blue in this example.
> // To use black, replace "0 0 1 rg" with "0 0 0 rg" or "0 g".
> defaultAppearanceString = "/Helv 0 Tf 0 0 1 rg";
> textBox.setDefaultAppearance(defaultAppearanceString);
> textBox.setMultiline(true);
> // add the field to the acroform
> acroForm.getFields().add(textBox);
> // Specify the widget annotation associated with the field
> PDAnnotationWidget widget = textBox.getWidgets().get(0);
> PDRectangle rect = new PDRectangle(50, 750, 200, 15);
> widget.setRectangle(rect);
> widget.setPage(page);
> // set green border and yellow background
> // if you prefer defaults, just delete this code block
> PDAppearanceCharacteristicsDictionary fieldAppearance
> = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
> fieldAppearance.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE));
> fieldAppearance.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE));
> widget.setAppearanceCharacteristics(fieldAppearance);
> // make sure the widget annotation is visible on screen and paper
> widget.setPrinted(true);
> // Add the widget annotation to the page
> page.getAnnotations().add(widget);
> // set the field value
> textBox.setValue("Sample field");
> document.save("target/SimpleMultilineForm.pdf");
> }{code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org