Skip to content

Commit 6e33840

Browse files
author
Kate Ivanova
committed
Add test for AcroForm functionality breaking pdf/a conformance
DEVSIX-5932
1 parent 8d88ca4 commit 6e33840

File tree

1 file changed

+97
-50
lines changed

1 file changed

+97
-50
lines changed

pdfa/src/test/java/com/itextpdf/pdfa/PdfAFormFieldTest.java

Lines changed: 97 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.pdfa;
4444

45+
import com.itextpdf.commons.utils.MessageFormatUtil;
4546
import com.itextpdf.forms.PdfAcroForm;
47+
import com.itextpdf.forms.PdfPageFormCopier;
4648
import com.itextpdf.forms.fields.PdfButtonFormField;
4749
import com.itextpdf.forms.fields.PdfChoiceFormField;
4850
import com.itextpdf.forms.fields.PdfFormField;
4951
import com.itextpdf.io.logs.IoLogMessageConstant;
5052
import com.itextpdf.io.font.PdfEncodings;
5153
import com.itextpdf.kernel.colors.ColorConstants;
54+
import com.itextpdf.kernel.exceptions.PdfException;
5255
import com.itextpdf.kernel.font.PdfFont;
5356
import com.itextpdf.kernel.font.PdfFontFactory;
5457
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
@@ -59,6 +62,7 @@ This file is part of the iText (R) project.
5962
import com.itextpdf.kernel.pdf.PdfDocument;
6063
import com.itextpdf.kernel.pdf.PdfName;
6164
import com.itextpdf.kernel.pdf.PdfOutputIntent;
65+
import com.itextpdf.kernel.pdf.PdfReader;
6266
import com.itextpdf.kernel.pdf.PdfString;
6367
import com.itextpdf.kernel.pdf.PdfWriter;
6468
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
@@ -72,6 +76,7 @@ This file is part of the iText (R) project.
7276
import com.itextpdf.layout.renderer.DrawContext;
7377
import com.itextpdf.layout.renderer.IRenderer;
7478
import com.itextpdf.layout.renderer.ParagraphRenderer;
79+
import com.itextpdf.pdfa.exceptions.PdfAConformanceException;
7580
import com.itextpdf.test.ExtendedITextTest;
7681
import com.itextpdf.test.annotations.LogMessage;
7782
import com.itextpdf.test.annotations.LogMessages;
@@ -91,30 +96,30 @@ This file is part of the iText (R) project.
9196
@Category(IntegrationTest.class)
9297
public class PdfAFormFieldTest extends ExtendedITextTest {
9398

94-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/pdfa/";
95-
public static final String destinationFolder = "./target/test/com/itextpdf/pdfa/PdfAFormFieldTest/";
99+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/pdfa/";
100+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/pdfa/PdfAFormFieldTest/";
96101

97102
@BeforeClass
98103
public static void beforeClass() {
99-
createDestinationFolder(destinationFolder);
104+
createDestinationFolder(DESTINATION_FOLDER);
100105
}
101106

102107
@Test
103108
public void pdfAButtonFieldTest() throws Exception {
104109
PdfDocument pdf;
105-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
110+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
106111
String file = "pdfAButtonField.pdf";
107-
String filename = destinationFolder + file;
112+
String filename = DESTINATION_FOLDER + file;
108113
pdf = new PdfADocument(
109114
new PdfWriter(new FileOutputStream(filename)),
110115
PdfAConformanceLevel.PDF_A_1B,
111116
new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB ICC preference", is));
112117

113118
PageSize pageSize = PageSize.LETTER;
114119
Document doc = new Document(pdf, pageSize);
115-
PdfFontFactory.register(sourceFolder + "FreeSans.ttf", sourceFolder + "FreeSans.ttf");
120+
PdfFontFactory.register(SOURCE_FOLDER + "FreeSans.ttf", SOURCE_FOLDER + "FreeSans.ttf");
116121
PdfFont font = PdfFontFactory.createFont(
117-
sourceFolder + "FreeSans.ttf", EmbeddingStrategy.PREFER_EMBEDDED);
122+
SOURCE_FOLDER + "FreeSans.ttf", EmbeddingStrategy.PREFER_EMBEDDED);
118123

119124
PdfButtonFormField group = PdfFormField.createRadioGroup(pdf, "group", "", PdfAConformanceLevel.PDF_A_1B);
120125
group.setReadOnly(true);
@@ -152,7 +157,8 @@ public void pdfAButtonFieldTest() throws Exception {
152157

153158
pdf.close();
154159
Assert.assertNull(
155-
new CompareTool().compareByContent(filename, sourceFolder + "cmp/PdfAFormFieldTest/cmp_" + file, destinationFolder, "diff_"));
160+
new CompareTool().compareByContent(filename, SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_" + file,
161+
DESTINATION_FOLDER, "diff_"));
156162
}
157163

158164
static class PdfAButtonFieldTestRenderer extends ParagraphRenderer {
@@ -206,10 +212,10 @@ public IRenderer getNextRenderer() {
206212
@Test
207213
public void pdfA1DocWithPdfA1ButtonFieldTest() throws IOException, InterruptedException {
208214
String name = "pdfA1DocWithPdfA1ButtonField";
209-
String fileName = destinationFolder + name + ".pdf";
210-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ButtonField.pdf";
215+
String fileName = DESTINATION_FOLDER + name + ".pdf";
216+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ButtonField.pdf";
211217

212-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
218+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
213219

214220
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
215221

@@ -225,17 +231,17 @@ public void pdfA1DocWithPdfA1ButtonFieldTest() throws IOException, InterruptedEx
225231

226232
pdfDoc.close();
227233

228-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
234+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
229235
Assert.assertNull(new VeraPdfValidator().validate(fileName));
230236
}
231237

232238
@Test
233239
public void pdfA1DocWithPdfA1CheckBoxFieldTest() throws IOException, InterruptedException {
234240
String name = "pdfA1DocWithPdfA1CheckBoxField";
235-
String fileName = destinationFolder + name + ".pdf";
236-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1CheckBoxField.pdf";
241+
String fileName = DESTINATION_FOLDER + name + ".pdf";
242+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1CheckBoxField.pdf";
237243

238-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
244+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
239245

240246
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
241247

@@ -248,20 +254,20 @@ public void pdfA1DocWithPdfA1CheckBoxFieldTest() throws IOException, Interrupted
248254
conformanceLevel));
249255
pdfDoc.close();
250256

251-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
257+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
252258
Assert.assertNull(new VeraPdfValidator().validate(fileName));
253259
}
254260

255261
@Test
256262
@LogMessages(messages = {@LogMessage(messageTemplate = IoLogMessageConstant.FIELD_VALUE_IS_NOT_CONTAINED_IN_OPT_ARRAY)})
257263
public void pdfA1DocWithPdfA1ChoiceFieldTest() throws IOException, InterruptedException {
258264
String name = "pdfA1DocWithPdfA1ChoiceField";
259-
String fileName = destinationFolder + name + ".pdf";
260-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ChoiceField.pdf";
265+
String fileName = DESTINATION_FOLDER + name + ".pdf";
266+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ChoiceField.pdf";
261267

262-
PdfFont fontFreeSans = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf",
268+
PdfFont fontFreeSans = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
263269
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
264-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
270+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
265271

266272
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
267273
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -276,20 +282,20 @@ public void pdfA1DocWithPdfA1ChoiceFieldTest() throws IOException, InterruptedEx
276282

277283
pdfDoc.close();
278284

279-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
285+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
280286
Assert.assertNull(new VeraPdfValidator().validate(fileName));
281287
}
282288

283289
@Test
284290
public void pdfA1DocWithPdfA1ComboBoxFieldTest() throws IOException, InterruptedException {
285291
String name = "pdfA1DocWithPdfA1ComboBoxField";
286-
String fileName = destinationFolder + name + ".pdf";
287-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ComboBoxField.pdf";
292+
String fileName = DESTINATION_FOLDER + name + ".pdf";
293+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ComboBoxField.pdf";
288294

289-
PdfFont fontCJK = PdfFontFactory.createFont(sourceFolder + "NotoSansCJKtc-Light.otf",
295+
PdfFont fontCJK = PdfFontFactory.createFont(SOURCE_FOLDER + "NotoSansCJKtc-Light.otf",
290296
PdfEncodings.IDENTITY_H, EmbeddingStrategy.FORCE_EMBEDDED);
291297

292-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
298+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
293299

294300
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
295301
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -302,21 +308,21 @@ public void pdfA1DocWithPdfA1ComboBoxFieldTest() throws IOException, Interrupted
302308

303309
pdfDoc.close();
304310

305-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
311+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
306312
Assert.assertNull(new VeraPdfValidator().validate(fileName));
307313
}
308314

309315
@Test
310316
@LogMessages(messages = {@LogMessage(messageTemplate = IoLogMessageConstant.MULTIPLE_VALUES_ON_A_NON_MULTISELECT_FIELD)})
311317
public void pdfA1DocWithPdfA1ListFieldTest() throws IOException, InterruptedException {
312318
String name = "pdfA1DocWithPdfA1ListField";
313-
String fileName = destinationFolder + name + ".pdf";
314-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ListField.pdf";
319+
String fileName = DESTINATION_FOLDER + name + ".pdf";
320+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ListField.pdf";
315321

316-
PdfFont fontFreeSans = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf",
322+
PdfFont fontFreeSans = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
317323
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
318324

319-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
325+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
320326

321327
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
322328
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -335,20 +341,20 @@ public void pdfA1DocWithPdfA1ListFieldTest() throws IOException, InterruptedExce
335341

336342
pdfDoc.close();
337343

338-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
344+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
339345
Assert.assertNull(new VeraPdfValidator().validate(fileName));
340346
}
341347

342348
@Test
343349
public void pdfA1DocWithPdfA1PushButtonFieldTest() throws IOException, InterruptedException {
344350
String name = "pdfA1DocWithPdfA1PushButtonField";
345-
String fileName = destinationFolder + name + ".pdf";
346-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1PushButtonField.pdf";
351+
String fileName = DESTINATION_FOLDER + name + ".pdf";
352+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1PushButtonField.pdf";
347353

348-
PdfFont fontFreeSans = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf",
354+
PdfFont fontFreeSans = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
349355
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
350356

351-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
357+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
352358

353359
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
354360
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -361,17 +367,17 @@ public void pdfA1DocWithPdfA1PushButtonFieldTest() throws IOException, Interrupt
361367

362368
pdfDoc.close();
363369

364-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
370+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
365371
Assert.assertNull(new VeraPdfValidator().validate(fileName));
366372
}
367373

368374
@Test
369375
public void pdfA1DocWithPdfA1RadioButtonFieldTest() throws IOException, InterruptedException {
370376
String name = "pdfA1DocWithPdfA1RadioButtonField";
371-
String fileName = destinationFolder + name + ".pdf";
372-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1RadioButtonField.pdf";
377+
String fileName = DESTINATION_FOLDER + name + ".pdf";
378+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1RadioButtonField.pdf";
373379

374-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
380+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
375381

376382
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
377383
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -390,21 +396,21 @@ public void pdfA1DocWithPdfA1RadioButtonFieldTest() throws IOException, Interrup
390396

391397
pdfDoc.close();
392398

393-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
399+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
394400
Assert.assertNull(new VeraPdfValidator().validate(fileName));
395401
}
396402

397403
@Test
398404
public void pdfA1DocWithPdfA1TextFieldTest() throws IOException, InterruptedException {
399405
String name = "pdfA1DocWithPdfA1TextField";
400-
String fileName = destinationFolder + name + ".pdf";
401-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1TextField.pdf";
406+
String fileName = DESTINATION_FOLDER + name + ".pdf";
407+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1TextField.pdf";
402408

403-
PdfFont fontFreeSans = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf",
409+
PdfFont fontFreeSans = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
404410
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
405411
fontFreeSans.setSubset(false);
406412

407-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
413+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
408414

409415
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
410416
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -417,21 +423,21 @@ public void pdfA1DocWithPdfA1TextFieldTest() throws IOException, InterruptedExce
417423

418424
pdfDoc.close();
419425

420-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
426+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
421427
Assert.assertNull(new VeraPdfValidator().validate(fileName));
422428
}
423429

424430
@Test
425431
public void pdfA1DocWithPdfA1SignatureFieldTest() throws IOException, InterruptedException {
426432
String name = "pdfA1DocWithPdfA1SignatureField";
427-
String fileName = destinationFolder + name + ".pdf";
428-
String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1SignatureField.pdf";
433+
String fileName = DESTINATION_FOLDER + name + ".pdf";
434+
String cmp = SOURCE_FOLDER + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1SignatureField.pdf";
429435

430-
PdfFont fontFreeSans = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf",
436+
PdfFont fontFreeSans = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf",
431437
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
432438
fontFreeSans.setSubset(false);
433439

434-
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
440+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
435441

436442
PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B;
437443
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel,
@@ -444,7 +450,48 @@ public void pdfA1DocWithPdfA1SignatureFieldTest() throws IOException, Interrupte
444450

445451
pdfDoc.close();
446452

447-
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, destinationFolder));
453+
Assert.assertNull(new CompareTool().compareByContent(fileName, cmp, DESTINATION_FOLDER));
448454
Assert.assertNull(new VeraPdfValidator().validate(fileName));
449455
}
456+
457+
@Test
458+
// TODO: DEVSIX-3913 update this test after the ticket will be resolved
459+
public void mergePdfADocWithFormTest() throws IOException {
460+
String fileName = DESTINATION_FOLDER + "pdfADocWithTextFormField.pdf";
461+
String mergedDocFileName = DESTINATION_FOLDER + "mergedPdfADoc.pdf";
462+
463+
try (InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
464+
PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), PdfAConformanceLevel.PDF_A_1B,
465+
new PdfOutputIntent("Custom", "",
466+
"http://www.color.org", "sRGB ICC preference", is));
467+
Document doc = new Document(pdfDoc)) {
468+
469+
PdfFont font = PdfFontFactory.createFont(SOURCE_FOLDER + "FreeSans.ttf", PdfEncodings.WINANSI);
470+
471+
doc.add(new Paragraph(new Text("Some text").setFont(font).setFontSize(10)));
472+
473+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
474+
form.addField(PdfFormField.createText(pdfDoc, new Rectangle(150, 100, 100, 20),
475+
"text", "textField", font, 10, false,
476+
PdfAConformanceLevel.PDF_A_1B).setFieldName("text").setPage(1), pdfDoc.getPage(1));
477+
}
478+
479+
Assert.assertNull(new VeraPdfValidator().validate(fileName));
480+
481+
PdfADocument pdfDocToMerge;
482+
try (InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
483+
PdfDocument newDoc = new PdfDocument(new PdfReader(fileName))) {
484+
pdfDocToMerge = new PdfADocument(new PdfWriter(mergedDocFileName).setSmartMode(true),
485+
PdfAConformanceLevel.PDF_A_1B,
486+
new PdfOutputIntent("Custom", "",
487+
"http://www.color.org", "sRGB ICC preference", is));
488+
489+
newDoc.copyPagesTo(1, newDoc.getNumberOfPages(), pdfDocToMerge, new PdfPageFormCopier());
490+
}
491+
492+
Exception ex = Assert.assertThrows(PdfException.class, () -> pdfDocToMerge.close());
493+
Assert.assertEquals(MessageFormatUtil
494+
.format(PdfAConformanceException.ALL_THE_FONTS_MUST_BE_EMBEDDED_THIS_ONE_IS_NOT_0,
495+
"Helvetica"), ex.getMessage());
496+
}
450497
}

0 commit comments

Comments
 (0)