AcroFormUtils { public static void mergeDefaults(PDAcroForm mergeThis, PDAcroForm intoThis) { if (!intoThis.isNeedAppearances() && mergeThis.isNeedAppearances()) { intoThis.setNeedAppearances(true); } String da = mergeThis.getDefaultAppearance(); if (isBlank(intoThis.getDefaultAppearance()) && !isBlank(da)) { intoThis.setDefaultAppearance(da); } int quadding = mergeThis.getCOSObject().getInt(COSName.Q); if ((quadding >= 0 && quadding <= 2) && !intoThis.getCOSObject().containsKey(COSName.Q)) { intoThis.setQuadding(quadding); } final COSDictionary formResources = ofNullable( intoThis.getCOSObject().getDictionaryObject(COSName.DR, COSDictionary.class)) .orElseGet(COSDictionary::new); ofNullable(mergeThis.getCOSObject().getDictionaryObject(COSName.DR, COSDictionary.class)).ifPresent(dr -> { for (COSName currentKey : dr.keySet()) { ofNullable(dr.getDictionaryObject(currentKey)).ifPresent(value -> { if (value instanceof COSDictionary) { mergeResourceDictionaryValue(formResources, (COSDictionary) value, currentKey); } else if (value instanceof COSArray) { mergeResourceArrayValue(formResources, (COSArray) value, currentKey); } else { LOG.warn("Unsupported resource dictionary type {}", value); } }); } }); intoThis.getCOSObject().setItem(COSName.DR, formResources); LOG.debug("Merged AcroForm dictionary"); } private  AcroFormUtils(); static void mergeDefaults(PDAcroForm mergeThis, PDAcroForm intoThis);  }
@Test public void megeDetaults() throws IOException { try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_with_full_dic.pdf")))) { PDAcroForm destination = new PDAcroForm(new PDDocument()); AcroFormUtils.mergeDefaults(anotherDoc.getDocumentCatalog().getAcroForm(), destination); assertEquals(2, destination.getQuadding()); assertEquals("/ArialMT 0 Tf 0 g ", destination.getDefaultAppearance()); assertTrue(destination.isNeedAppearances()); assertTrue(destination.getDefaultResources().getCOSObject().size() > 0); } }
@Test public void falseNeedAppearance() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); mergeThis.setNeedAppearances(false); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); intoThis.setNeedAppearances(true); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertTrue(intoThis.isNeedAppearances()); }
@Test public void blankNeedAppearance() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); mergeThis.setDefaultAppearance("this"); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); intoThis.setDefaultAppearance("that"); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertEquals("that", intoThis.getDefaultAppearance()); }
@Test public void negativeQuadding() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); mergeThis.setQuadding(-2); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertEquals(0, intoThis.getQuadding()); }
@Test public void invalidQuadding() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); mergeThis.setQuadding(20); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertEquals(0, intoThis.getQuadding()); }
@Test public void alreadySetQuadding() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); mergeThis.setQuadding(2); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); intoThis.setQuadding(1); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertEquals(1, intoThis.getQuadding()); }
@Test public void invalidResourceType() { PDAcroForm mergeThis = new PDAcroForm(new PDDocument()); PDResources dr = new PDResources(); dr.getCOSObject().setInt(COSName.FONT, 10); mergeThis.setDefaultResources(dr); PDAcroForm intoThis = new PDAcroForm(new PDDocument()); AcroFormUtils.mergeDefaults(mergeThis, intoThis); assertNull(intoThis.getDefaultResources().getCOSObject().getItem(COSName.FONT)); }
@Test public void mergeFormsWithProcSet() throws IOException { try (PDDocument anotherDoc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/forms/simple_form_proc_set.pdf")))) { PDAcroForm destination = new PDAcroForm(new PDDocument()); AcroFormUtils.mergeDefaults(anotherDoc.getDocumentCatalog().getAcroForm(), destination); COSBase procSet = destination.getDefaultResources().getCOSObject().getItem(COSName.PROC_SET); assertNotNull(procSet); assertEquals(2, ((COSArray) procSet).size()); } }