You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/08/20 15:15:48 UTC
svn commit: r1881024 - in /pdfbox/branches/issue45/examples/src:
main/java/org/apache/pdfbox/examples/interactive/form/
test/java/org/apache/pdfbox/examples/interactive/
test/java/org/apache/pdfbox/examples/interactive/form/
Author: tilman
Date: Thu Aug 20 15:15:47 2020
New Revision: 1881024
URL: http://svn.apache.org/viewvc?rev=1881024&view=rev
Log:
PDFBOX-4939: increase test coverage
Added:
pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/
pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/
pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java (with props)
Modified:
pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java
pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateMultiWidgetsForm.java
Modified: pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java?rev=1881024&r1=1881023&r2=1881024&view=diff
==============================================================================
--- pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java (original)
+++ pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/AddBorderToField.java Thu Aug 20 15:15:47 2020
@@ -54,12 +54,12 @@ public final class AddBorderToField
PDField field = acroForm.getField("SampleField");
PDAnnotationWidget widget = field.getWidgets().get(0);
- // Create the definition for a green border
- PDAppearanceCharacteristicsDictionary fieldAppearance =
+ // Create the definition for a red border
+ PDAppearanceCharacteristicsDictionary fieldAppearance =
new PDAppearanceCharacteristicsDictionary(new COSDictionary());
- PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE);
- fieldAppearance.setBorderColour(green);
-
+ PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
+ fieldAppearance.setBorderColour(red);
+
// Set the information to be used by the widget which is responsible
// for the visual style of the form field.
widget.setAppearanceCharacteristics(fieldAppearance);
Modified: pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateMultiWidgetsForm.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateMultiWidgetsForm.java?rev=1881024&r1=1881023&r2=1881024&view=diff
==============================================================================
--- pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateMultiWidgetsForm.java (original)
+++ pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateMultiWidgetsForm.java Thu Aug 20 15:15:47 2020
@@ -138,7 +138,7 @@ public final class CreateMultiWidgetsFor
// set the field value
textBox.setValue("Sample field");
- document.save("MultiWidgetsForm.pdf");
+ document.save("target/MultiWidgetsForm.pdf");
document.close();
}
}
Added: pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java?rev=1881024&view=auto
==============================================================================
--- pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java (added)
+++ pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java Thu Aug 20 15:15:47 2020
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.interactive.form;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
+import org.apache.pdfbox.rendering.PDFRenderer;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test of some the form examples.
+ *
+ * @author Tilman Hausherr
+ */
+public class TestCreateSimpleForms
+{
+ public TestCreateSimpleForms()
+ {
+ }
+
+ /**
+ * Test of CreateSimpleForm
+ *
+ * @throws java.io.IOException
+ */
+ @Test
+ public void testCreateSimpleForm() throws IOException
+ {
+ CreateSimpleForm.main(null);
+ PDDocument doc = PDDocument.load(new File("target/SimpleForm.pdf"));
+ new PDFRenderer(doc).renderImage(0);
+ PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
+ PDTextField textBox = (PDTextField) acroForm.getField("SampleField");
+ Assert.assertEquals("Sample field content", textBox.getValue());
+ try
+ {
+ textBox.setValue("Åódź");
+ Assert.fail("should have failed with IllegalArgumentException");
+ }
+ catch (IllegalArgumentException ex)
+ {
+ Assert.assertTrue(ex.getMessage().contains("U+0141 ('Lslash') is not available"));
+ }
+
+ PDFont font = getFontFromWidgetResources(textBox, "Helv");
+ Assert.assertEquals("Helvetica", font.getName());
+ Assert.assertTrue(font.isStandard14());
+ doc.close();
+ }
+
+ @Test
+ public void testAddBorderToField() throws IOException
+ {
+ CreateSimpleForm.main(null);
+
+ PDDocument doc = PDDocument.load(new File("target/SimpleForm.pdf"));
+ PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
+ PDTextField textBox = (PDTextField) acroForm.getField("SampleField");
+ PDAnnotationWidget widget = textBox.getWidgets().get(0);
+ PDAppearanceCharacteristicsDictionary appearanceCharacteristics = widget.getAppearanceCharacteristics();
+ PDColor borderColour = appearanceCharacteristics.getBorderColour();
+ PDColor backgroundColour = appearanceCharacteristics.getBackground();
+ Assert.assertEquals(borderColour.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertEquals(backgroundColour.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertArrayEquals(new float[]{0,1,0}, borderColour.getComponents(), 0);
+ Assert.assertArrayEquals(new float[]{1,1,0}, backgroundColour.getComponents(), 0);
+ doc.close();
+
+ AddBorderToField.main(null);
+
+ PDDocument doc2 = PDDocument.load(new File("target/AddBorderToField.pdf"));
+ new PDFRenderer(doc2).renderImage(0);
+ PDAcroForm acroForm2 = doc2.getDocumentCatalog().getAcroForm();
+ PDTextField textBox2 = (PDTextField) acroForm2.getField("SampleField");
+ PDAnnotationWidget widget2 = textBox2.getWidgets().get(0);
+ PDAppearanceCharacteristicsDictionary appearanceCharacteristics2 = widget2.getAppearanceCharacteristics();
+ PDColor borderColour2 = appearanceCharacteristics2.getBorderColour();
+ Assert.assertEquals(borderColour2.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertArrayEquals(new float[]{1,0,0}, borderColour2.getComponents(), 0);
+
+ doc2.close();
+ }
+
+ /**
+ * Test of CreateSimpleFormWithEmbeddedFont
+ *
+ * @throws java.io.IOException
+ */
+ @Test
+ public void testCreateSimpleFormWithEmbeddedFont() throws IOException
+ {
+ CreateSimpleFormWithEmbeddedFont.main(null);
+ PDDocument doc = PDDocument.load(new File("target/SimpleFormWithEmbeddedFont.pdf"));
+ new PDFRenderer(doc).renderImage(0);
+ PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
+ PDTextField textBox = (PDTextField) acroForm.getField("SampleField");
+ Assert.assertEquals("Sample field Ä°", textBox.getValue());
+ textBox.setValue("Åódź");
+ PDFont font = getFontFromWidgetResources(textBox, "F1");
+ Assert.assertEquals("LiberationSans", font.getName());
+ doc.close();
+ }
+
+ /**
+ * Test of CreateSimpleFormWithEmbeddedFont
+ *
+ * @throws java.io.IOException
+ */
+ @Test
+ public void testCreateMultiWidgetsForm() throws IOException
+ {
+ CreateMultiWidgetsForm.main(null);
+
+ PDDocument doc = PDDocument.load(new File("target/MultiWidgetsForm.pdf"));
+ Assert.assertEquals(2, doc.getNumberOfPages());
+ new PDFRenderer(doc).renderImage(0);
+ new PDFRenderer(doc).renderImage(1);
+ PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
+ PDTextField textBox = (PDTextField) acroForm.getField("SampleField");
+ Assert.assertEquals("Sample field", textBox.getValue());
+ List<PDAnnotationWidget> widgets = textBox.getWidgets();
+ Assert.assertEquals(2, widgets.size());
+ PDAnnotationWidget w1 = widgets.get(0);
+ PDAnnotationWidget w2 = widgets.get(1);
+ PDPage page1 = w1.getPage();
+ PDPage page2 = w2.getPage();
+ Assert.assertFalse(page1.getCOSObject().equals(page2.getCOSObject()));
+ Assert.assertEquals(page1, doc.getPage(0));
+ Assert.assertEquals(page2, doc.getPage(1));
+ Assert.assertEquals(page1.getAnnotations().get(0).getCOSObject(), w1.getCOSObject());
+ Assert.assertEquals(page2.getAnnotations().get(0).getCOSObject(), w2.getCOSObject());
+ Assert.assertFalse(w1.equals(w2));
+ PDAppearanceCharacteristicsDictionary appearanceCharacteristics1 = w1.getAppearanceCharacteristics();
+ PDAppearanceCharacteristicsDictionary appearanceCharacteristics2 = w2.getAppearanceCharacteristics();
+ PDColor backgroundColor1 = appearanceCharacteristics1.getBackground();
+ PDColor backgroundColor2 = appearanceCharacteristics2.getBackground();
+ PDColor borderColour1 = appearanceCharacteristics1.getBorderColour();
+ PDColor borderColour2 = appearanceCharacteristics2.getBorderColour();
+ Assert.assertEquals(backgroundColor1.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertEquals(backgroundColor2.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertEquals(borderColour1.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertEquals(borderColour2.getColorSpace(), PDDeviceRGB.INSTANCE);
+ Assert.assertArrayEquals(new float[]{1,1,0}, backgroundColor1.getComponents(), 0);
+ Assert.assertArrayEquals(new float[]{0,1,0}, backgroundColor2.getComponents(), 0);
+ Assert.assertArrayEquals(new float[]{0,1,0}, borderColour1.getComponents(), 0);
+ Assert.assertArrayEquals(new float[]{1,0,0}, borderColour2.getComponents(), 0);
+ doc.close();
+ }
+
+ private PDFont getFontFromWidgetResources(PDTextField textBox, String fontResourceName) throws IOException
+ {
+ PDAnnotationWidget widget = textBox.getWidgets().get(0);
+ PDAppearanceDictionary appearance = widget.getAppearance();
+ PDAppearanceEntry normalAppearance = appearance.getNormalAppearance();
+ PDAppearanceStream appearanceStream = normalAppearance.getAppearanceStream();
+ PDResources resources = appearanceStream.getResources();
+ return resources.getFont(COSName.getPDFName(fontResourceName));
+ }
+}
Propchange: pdfbox/branches/issue45/examples/src/test/java/org/apache/pdfbox/examples/interactive/form/TestCreateSimpleForms.java
------------------------------------------------------------------------------
svn:eol-style = native