|
| 1 | +package com.itextpdf.pdfa; |
| 2 | + |
| 3 | +import com.itextpdf.forms.PdfAcroForm; |
| 4 | +import com.itextpdf.forms.fields.PdfButtonFormField; |
| 5 | +import com.itextpdf.forms.fields.PdfFormField; |
| 6 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 7 | +import com.itextpdf.kernel.font.PdfFont; |
| 8 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 9 | +import com.itextpdf.kernel.geom.Rectangle; |
| 10 | +import com.itextpdf.kernel.pdf.PdfAConformanceLevel; |
| 11 | +import com.itextpdf.kernel.pdf.PdfOutputIntent; |
| 12 | +import com.itextpdf.kernel.pdf.PdfString; |
| 13 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 14 | +import com.itextpdf.kernel.utils.CompareTool; |
| 15 | +import com.itextpdf.test.ExtendedITextTest; |
| 16 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.BeforeClass; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.experimental.categories.Category; |
| 21 | + |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | + |
| 26 | +@Category(IntegrationTest.class) |
| 27 | +public class PdfAPushbuttonfieldTest extends ExtendedITextTest { |
| 28 | + |
| 29 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/pdfa/"; |
| 30 | + public static final String cmpFolder = "./src/test/resources/com/itextpdf/pdfa/cmp/PdfAPushbuttonfieldTest/"; |
| 31 | + public static final String destinationFolder = "./target/test/com/itextpdf/pdfa/PdfAPushbuttonfieldTest/"; |
| 32 | + |
| 33 | + @BeforeClass |
| 34 | + public static void beforeClass() { |
| 35 | + createDestinationFolder(destinationFolder); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void pdfA1bButtonAppearanceTest() throws IOException, InterruptedException { |
| 40 | + String name = "pdfA1b_ButtonAppearanceTest"; |
| 41 | + String outPath = destinationFolder + name + ".pdf"; |
| 42 | + String cmpPath = cmpFolder + "cmp_" + name + ".pdf"; |
| 43 | + String diff = "diff_" + name + "_"; |
| 44 | + |
| 45 | + PdfWriter writer = new PdfWriter(outPath); |
| 46 | + InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm"); |
| 47 | + PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is); |
| 48 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent); |
| 49 | + doc.setTagged(); |
| 50 | + doc.getCatalog().setLang(new PdfString("en-US")); |
| 51 | + doc.addNewPage(); |
| 52 | + PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true); |
| 53 | + |
| 54 | + Rectangle rect = new Rectangle(36, 626, 100, 40); |
| 55 | + PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true); |
| 56 | + PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B); |
| 57 | + form.addField(button); |
| 58 | + |
| 59 | + doc.close(); |
| 60 | + |
| 61 | + Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void pdfA1bButtonAppearanceRegenerateTest() throws IOException, InterruptedException { |
| 66 | + String name = "pdfA1b_ButtonAppearanceRegenerateTest"; |
| 67 | + String outPath = destinationFolder + name + ".pdf"; |
| 68 | + String cmpPath = cmpFolder + "cmp_" + name + ".pdf"; |
| 69 | + String diff = "diff_" + name + "_"; |
| 70 | + |
| 71 | + PdfWriter writer = new PdfWriter(outPath); |
| 72 | + InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm"); |
| 73 | + PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is); |
| 74 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent); |
| 75 | + doc.setTagged(); |
| 76 | + doc.getCatalog().setLang(new PdfString("en-US")); |
| 77 | + doc.addNewPage(); |
| 78 | + PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true); |
| 79 | + |
| 80 | + Rectangle rect = new Rectangle(36, 626, 100, 40); |
| 81 | + PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true); |
| 82 | + PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B); |
| 83 | + button.regenerateField(); |
| 84 | + form.addField(button); |
| 85 | + |
| 86 | + doc.close(); |
| 87 | + |
| 88 | + Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void pdfA1bButtonAppearanceSetValueTest() throws IOException, InterruptedException { |
| 93 | + String name = "pdfA1b_ButtonAppearanceSetValueTest"; |
| 94 | + String outPath = destinationFolder + name + ".pdf"; |
| 95 | + String cmpPath = cmpFolder + "cmp_" + name + ".pdf"; |
| 96 | + String diff = "diff_" + name + "_"; |
| 97 | + |
| 98 | + PdfWriter writer = new PdfWriter(outPath); |
| 99 | + InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm"); |
| 100 | + PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is); |
| 101 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent); |
| 102 | + doc.setTagged(); |
| 103 | + doc.getCatalog().setLang(new PdfString("en-US")); |
| 104 | + doc.addNewPage(); |
| 105 | + PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true); |
| 106 | + |
| 107 | + Rectangle rect = new Rectangle(36, 626, 100, 40); |
| 108 | + PdfFont font = PdfFontFactory.createFont(sourceFolder + "FreeSans.ttf", "WinAnsi", true); |
| 109 | + PdfFormField button = PdfFormField.createPushButton(doc, rect, "push button", "push", font, 12, PdfAConformanceLevel.PDF_A_1B); |
| 110 | + button.setValue("button"); |
| 111 | + form.addField(button); |
| 112 | + |
| 113 | + doc.close(); |
| 114 | + |
| 115 | + Assert.assertNull(new CompareTool().compareByContent(outPath, cmpPath, destinationFolder, diff)); |
| 116 | + } |
| 117 | +} |
0 commit comments