Skip to content

Commit a4a6534

Browse files
author
Andrei Stryhelski
committed
Add PdfUA2 tests
DEVSIX-7930
1 parent 19ee24c commit a4a6534

38 files changed

+805
-22
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2023 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.forms;
24+
25+
import com.itextpdf.forms.fields.PdfFormCreator;
26+
import com.itextpdf.forms.fields.PdfTextFormField;
27+
import com.itextpdf.forms.fields.TextFormFieldBuilder;
28+
import com.itextpdf.forms.form.FormProperty;
29+
import com.itextpdf.forms.form.element.InputField;
30+
import com.itextpdf.forms.form.element.SignatureFieldAppearance;
31+
import com.itextpdf.forms.form.element.TextArea;
32+
import com.itextpdf.kernel.colors.ColorConstants;
33+
import com.itextpdf.kernel.font.PdfFont;
34+
import com.itextpdf.kernel.font.PdfFontFactory;
35+
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
36+
import com.itextpdf.kernel.geom.Rectangle;
37+
import com.itextpdf.kernel.pdf.PdfDocument;
38+
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
39+
import com.itextpdf.kernel.pdf.PdfName;
40+
import com.itextpdf.kernel.pdf.PdfString;
41+
import com.itextpdf.kernel.pdf.PdfVersion;
42+
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
43+
import com.itextpdf.kernel.pdf.PdfWriter;
44+
import com.itextpdf.kernel.pdf.WriterProperties;
45+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
46+
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
47+
import com.itextpdf.kernel.utils.CompareTool;
48+
import com.itextpdf.kernel.xmp.XMPException;
49+
import com.itextpdf.kernel.xmp.XMPMeta;
50+
import com.itextpdf.kernel.xmp.XMPMetaFactory;
51+
import com.itextpdf.layout.Document;
52+
import com.itextpdf.layout.borders.SolidBorder;
53+
import com.itextpdf.test.ExtendedITextTest;
54+
import com.itextpdf.test.annotations.type.IntegrationTest;
55+
import com.itextpdf.test.pdfa.VeraPdfValidator; // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
56+
57+
import java.io.ByteArrayInputStream;
58+
import java.io.IOException;
59+
import java.nio.file.Files;
60+
import java.nio.file.Paths;
61+
import org.junit.Assert;
62+
import org.junit.BeforeClass;
63+
import org.junit.Test;
64+
import org.junit.experimental.categories.Category;
65+
import static org.junit.Assert.fail;
66+
67+
@Category(IntegrationTest.class)
68+
public class PdfUA2FormTest extends ExtendedITextTest {
69+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/forms/PdfUA2FormTest/";
70+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/forms//PdfUA2FormTest/";
71+
72+
@BeforeClass
73+
public static void beforeClass() {
74+
createOrClearDestinationFolder(DESTINATION_FOLDER);
75+
}
76+
77+
@Test
78+
public void checkFormFieldTest() throws IOException, XMPException, InterruptedException {
79+
String outFile = DESTINATION_FOLDER + "formFieldTest.pdf";
80+
String cmpFile = SOURCE_FOLDER + "cmp_formFieldTest.pdf";
81+
82+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
83+
PdfVersion.PDF_2_0)))){
84+
Document document = new Document(pdfDocument);
85+
PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
86+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
87+
document.setFont(font);
88+
createSimplePdfUA2Document(pdfDocument);
89+
90+
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true);
91+
Rectangle rect = new Rectangle(210, 490, 150, 22);
92+
PdfTextFormField field = new TextFormFieldBuilder(pdfDocument, "fieldName")
93+
.setWidgetRectangle(rect).createText();
94+
field.put(PdfName.Contents, new PdfString("Description"));
95+
field.setValue("some value");
96+
field.setFont(font);
97+
form.addField(field);
98+
}
99+
compareAndValidate(outFile, cmpFile);
100+
}
101+
102+
@Test
103+
public void checkTextAreaTest() throws IOException, XMPException, InterruptedException {
104+
String outFile = DESTINATION_FOLDER + "textAreaTest.pdf";
105+
String cmpFile = SOURCE_FOLDER + "cmp_textAreaTest.pdf";
106+
107+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
108+
PdfVersion.PDF_2_0)))){
109+
Document document = new Document(pdfDocument);
110+
PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
111+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
112+
document.setFont(font);
113+
createSimplePdfUA2Document(pdfDocument);
114+
115+
TextArea formTextArea = new TextArea("form text area");
116+
formTextArea.setProperty(FormProperty.FORM_FIELD_FLATTEN, false);
117+
formTextArea.setProperty(FormProperty.FORM_FIELD_VALUE, "form\ntext\narea");
118+
119+
document.add(formTextArea);
120+
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true);
121+
form.getField("form text area").getPdfObject().put(PdfName.Contents, new PdfString("Description"));
122+
}
123+
compareAndValidate(outFile, cmpFile);
124+
}
125+
126+
@Test
127+
public void checkInputFieldTest() throws IOException, XMPException, InterruptedException {
128+
String outFile = DESTINATION_FOLDER + "inputFieldTest.pdf";
129+
String cmpFile = SOURCE_FOLDER + "cmp_inputFieldTest.pdf";
130+
131+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
132+
PdfVersion.PDF_2_0)))){
133+
Document document = new Document(pdfDocument);
134+
PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
135+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
136+
document.setFont(font);
137+
createSimplePdfUA2Document(pdfDocument);
138+
139+
InputField formInputField = new InputField("form input field");
140+
formInputField.setProperty(FormProperty.FORM_FIELD_FLATTEN, false);
141+
formInputField.setProperty(FormProperty.FORM_FIELD_VALUE, "form input field");
142+
formInputField.setProperty(FormProperty.FORM_FIELD_LABEL, "label form field");
143+
144+
document.add(formInputField);
145+
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true);
146+
form.getField("form input field").getPdfObject().put(PdfName.Contents, new PdfString("Description"));
147+
}
148+
compareAndValidate(outFile, cmpFile);
149+
}
150+
151+
@Test
152+
public void checkSignatureFormTest() throws IOException, XMPException, InterruptedException {
153+
String outFile = DESTINATION_FOLDER + "signatureFormTest.pdf";
154+
String cmpFile = SOURCE_FOLDER + "cmp_signatureFormTest.pdf";
155+
156+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
157+
PdfVersion.PDF_2_0)))){
158+
Document document = new Document(pdfDocument);
159+
PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
160+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
161+
document.setFont(font);
162+
createSimplePdfUA2Document(pdfDocument);
163+
TagTreePointer tagPointer = pdfDocument.getTagStructureContext().getAutoTaggingPointer();
164+
tagPointer.addTag(StandardRoles.FIGURE);
165+
tagPointer.getProperties().setAlternateDescription("Alt Description");
166+
167+
SignatureFieldAppearance formSigField = new SignatureFieldAppearance("form SigField");
168+
formSigField.setProperty(FormProperty.FORM_FIELD_FLATTEN, false);
169+
formSigField.setContent("form SigField");
170+
171+
formSigField.setBorder(new SolidBorder(ColorConstants.YELLOW, 1));
172+
formSigField.setFont(font);
173+
document.add(formSigField);
174+
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDocument, true);
175+
form.getField("form SigField").getPdfObject().put(PdfName.Contents, new PdfString("Description"));
176+
}
177+
compareAndValidate(outFile, cmpFile);
178+
}
179+
180+
private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException {
181+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
182+
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
183+
pdfDocument.setXmpMetadata(xmpMeta);
184+
pdfDocument.setTagged();
185+
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
186+
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
187+
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
188+
info.setTitle("PdfUA2 Title");
189+
}
190+
191+
private void compareAndValidate(String outPdf, String cmpPdf) throws IOException, InterruptedException {
192+
Assert.assertNull(new VeraPdfValidator().validate(outPdf)); // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
193+
String result = new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_");
194+
if (result != null) {
195+
fail(result);
196+
}
197+
}
198+
}
Binary file not shown.
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.1.0-jc003">
2+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
3+
<rdf:Description rdf:about=""
4+
xmlns:dc="http://purl.org/dc/elements/1.1/"
5+
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
6+
xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
7+
xmlns:pdfuaid="http://www.aiim.org/pdfua/ns/id/"
8+
dc:format="application/pdf">
9+
<pdfuaid:part>2</pdfuaid:part>
10+
<pdfuaid:rev>2024</pdfuaid:rev>
11+
</rdf:Description>
12+
</rdf:RDF>
13+
</x:xmpmeta>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2023 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.layout;
24+
25+
import com.itextpdf.io.font.PdfEncodings;
26+
import com.itextpdf.kernel.font.PdfFont;
27+
import com.itextpdf.kernel.font.PdfFontFactory;
28+
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
29+
import com.itextpdf.kernel.pdf.PdfDocument;
30+
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
31+
import com.itextpdf.kernel.pdf.PdfString;
32+
import com.itextpdf.kernel.pdf.PdfVersion;
33+
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
34+
import com.itextpdf.kernel.pdf.PdfWriter;
35+
import com.itextpdf.kernel.pdf.WriterProperties;
36+
import com.itextpdf.kernel.utils.CompareTool;
37+
import com.itextpdf.kernel.xmp.XMPException;
38+
import com.itextpdf.kernel.xmp.XMPMeta;
39+
import com.itextpdf.kernel.xmp.XMPMetaFactory;
40+
import com.itextpdf.layout.element.Paragraph;
41+
import com.itextpdf.test.ExtendedITextTest;
42+
import com.itextpdf.test.annotations.type.IntegrationTest;
43+
import com.itextpdf.test.pdfa.VeraPdfValidator; // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
44+
45+
import java.io.ByteArrayInputStream;
46+
import java.io.IOException;
47+
import java.nio.file.Files;
48+
import java.nio.file.Paths;
49+
import org.junit.Assert;
50+
import org.junit.BeforeClass;
51+
import org.junit.Test;
52+
import org.junit.experimental.categories.Category;
53+
import static org.junit.Assert.fail;
54+
55+
@Category(IntegrationTest.class)
56+
public class PdfUA2FontTest extends ExtendedITextTest {
57+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/layout/PdfUA2FontTest/";
58+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/layout/PdfUA2FontTest/";
59+
public static final String FONT_FOLDER = "./src/test/resources/com/itextpdf/layout/fonts/";
60+
61+
@BeforeClass
62+
public static void beforeClass() {
63+
createOrClearDestinationFolder(DESTINATION_FOLDER);
64+
}
65+
66+
@Test
67+
public void checkPuritan2WithUTF8Test() throws IOException, XMPException, InterruptedException {
68+
String outFile = DESTINATION_FOLDER + "puritan2WithUTF8Test.pdf";
69+
String cmpFile = SOURCE_FOLDER + "cmp_puritan2WithUTF8Test.pdf";
70+
71+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
72+
PdfVersion.PDF_2_0)))){
73+
Document document = new Document(pdfDocument);
74+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "Puritan2.otf",
75+
PdfEncodings.UTF8, EmbeddingStrategy.FORCE_EMBEDDED);
76+
document.setFont(font);
77+
createSimplePdfUA2Document(pdfDocument);
78+
79+
Paragraph paragraph = new Paragraph("Simple paragraph");
80+
document.add(paragraph);
81+
}
82+
compareAndValidate(outFile, cmpFile);
83+
}
84+
85+
@Test
86+
public void checkFreeSansWithMacromanTest() throws IOException, XMPException, InterruptedException {
87+
String outFile = DESTINATION_FOLDER + "freeSansWithMacromanTest.pdf";
88+
String cmpFile = SOURCE_FOLDER + "cmp_freeSansWithMacromanTest.pdf";
89+
90+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
91+
PdfVersion.PDF_2_0)))){
92+
Document document = new Document(pdfDocument);
93+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
94+
PdfEncodings.MACROMAN, EmbeddingStrategy.FORCE_EMBEDDED);
95+
document.setFont(font);
96+
createSimplePdfUA2Document(pdfDocument);
97+
98+
Paragraph paragraph = new Paragraph("Simple paragraph");
99+
document.add(paragraph);
100+
}
101+
compareAndValidate(outFile, cmpFile);
102+
}
103+
104+
@Test
105+
public void checkNotoSansRegularTest() throws IOException, XMPException, InterruptedException {
106+
String outFile = DESTINATION_FOLDER + "notoSansRegularTest.pdf";
107+
String cmpFile = SOURCE_FOLDER + "cmp_notoSansRegularTest.pdf";
108+
109+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
110+
PdfVersion.PDF_2_0)))){
111+
Document document = new Document(pdfDocument);
112+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "NotoSans-Regular.ttf",
113+
PdfEncodings.WINANSI, EmbeddingStrategy.FORCE_EMBEDDED);
114+
document.setFont(font);
115+
createSimplePdfUA2Document(pdfDocument);
116+
117+
Paragraph paragraph = new Paragraph("Simple paragraph");
118+
document.add(paragraph);
119+
}
120+
compareAndValidate(outFile, cmpFile);
121+
}
122+
123+
@Test
124+
public void checkOpenSansRegularTest() throws IOException, XMPException, InterruptedException {
125+
String outFile = DESTINATION_FOLDER + "openSansRegularTest.pdf";
126+
String cmpFile = SOURCE_FOLDER + "cmp_openSansRegularTest.pdf";
127+
128+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
129+
PdfVersion.PDF_2_0)))){
130+
Document document = new Document(pdfDocument);
131+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "Open_Sans/OpenSans-Regular.ttf",
132+
PdfEncodings.WINANSI, EmbeddingStrategy.FORCE_EMBEDDED);
133+
document.setFont(font);
134+
createSimplePdfUA2Document(pdfDocument);
135+
136+
Paragraph paragraph = new Paragraph("Simple paragraph");
137+
document.add(paragraph);
138+
}
139+
compareAndValidate(outFile, cmpFile);
140+
}
141+
142+
@Test
143+
public void checkType0FontTest() throws IOException, XMPException, InterruptedException {
144+
String outFile = DESTINATION_FOLDER + "type0FontTest.pdf";
145+
String cmpFile = SOURCE_FOLDER + "cmp_type0FontTest.pdf";
146+
147+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(
148+
PdfVersion.PDF_2_0)))){
149+
Document document = new Document(pdfDocument);
150+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
151+
PdfEncodings.IDENTITY_H, EmbeddingStrategy.FORCE_EMBEDDED);
152+
document.setFont(font);
153+
createSimplePdfUA2Document(pdfDocument);
154+
155+
Paragraph paragraph = new Paragraph("Simple paragraph");
156+
document.add(paragraph);
157+
}
158+
compareAndValidate(outFile, cmpFile);
159+
}
160+
161+
private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException {
162+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
163+
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
164+
pdfDocument.setXmpMetadata(xmpMeta);
165+
pdfDocument.setTagged();
166+
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
167+
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
168+
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
169+
info.setTitle("PdfUA2 Title");
170+
}
171+
172+
private void compareAndValidate(String outPdf, String cmpPdf) throws IOException, InterruptedException {
173+
Assert.assertNull(new VeraPdfValidator().validate(outPdf)); // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
174+
String result = new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_");
175+
if (result != null) {
176+
fail(result);
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)