/*
 * 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.multipdf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.junit.Before;
import org.junit.Test;

/**
 * Test merging different PDFs with AcroForms.
 * 
 * 
 */
public class MergeAcroFormsTest
{
    private static final File OUT_DIR = new File("target/test-output/merge/");

    @Before
    public void setUp()
    {
        OUT_DIR.mkdirs();
    }
    
    /*
     * PDFBOX-1031 Ensure that after merging the PDFs there is an Annots entry per page.
     */
    @Test
    public void testAnnotsEntry() throws IOException {
        
        // Merge the PDFs form PDFBOX-1031
        PDFMergerUtility merger = new PDFMergerUtility();
        
        URL url1 = new URL("https://issues.apache.org/jira/secure/attachment/12481683/1.pdf");
        InputStream is1 = url1.openStream();

        URL url2 = new URL("https://issues.apache.org/jira/secure/attachment/12481684/2.pdf");
        InputStream is2 = url2.openStream();
        File pdfOutput = new File(OUT_DIR,"PDFBOX-1031.pdf");
        merger.setDestinationFileName(pdfOutput.getAbsolutePath());
        merger.addSource(is1);
        merger.addSource(is2);
        merger.mergeDocuments(null);
        
        // Test merge result
        PDDocument mergedPDF = PDDocument.load(pdfOutput);
        assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
        
        assertNotNull("There shall be an /Annots entry for the first page", mergedPDF.getPage(0).getCOSObject().getDictionaryObject(COSName.ANNOTS));
        assertEquals("There shall be 1 annotation for the first page", 1, mergedPDF.getPage(0).getAnnotations().size());
        
        assertNotNull("There shall be an /Annots entry for the second page", mergedPDF.getPage(1).getCOSObject().getDictionaryObject(COSName.ANNOTS));
        assertEquals("There shall be 1 annotation for the second page", 1, mergedPDF.getPage(0).getAnnotations().size());

        mergedPDF.close();
    }
}

@@ -1107,16 +1107,137 @@
 cument;%0A
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;%0Aimport org.apache.pdfbox.pdmodel.interactive.form.PDField;%0A
 import o
@@ -3104,11 +3104,1738 @@
 );%0A    %7D
+    %0A    %0A    /*%0A     * PDFBOX-1100 Ensure that after merging the PDFs there is an AP and V entry.%0A     */%0A    @Test%0A    public void testAPEntry() throws IOException %7B%0A        %0A        // Merge the PDFs form PDFBOX-1100%0A        PDFMergerUtility merger = new PDFMergerUtility();%0A        %0A        URL url1 = new URL(%22https://issues.apache.org/jira/secure/attachment/12490774/a.pdf%22);%0A        InputStream is1 = url1.openStream();%0A%0A        URL url2 = new URL(%22https://issues.apache.org/jira/secure/attachment/12490775/b.pdf%22);%0A        InputStream is2 = url2.openStream();%0A        File pdfOutput = new File(OUT_DIR,%22PDFBOX-1100.pdf%22);%0A        merger.setDestinationFileName(pdfOutput.getAbsolutePath());%0A        merger.addSource(is1);%0A        merger.addSource(is2);%0A        merger.mergeDocuments(null);%0A        %0A        // Test merge result%0A        PDDocument mergedPDF = PDDocument.load(pdfOutput);%0A        assertEquals(%22There shall be 2 pages%22, 2, mergedPDF.getNumberOfPages());%0A        %0A        PDAcroForm acroForm = mergedPDF.getDocumentCatalog().getAcroForm();%0A        %0A        PDField formField = acroForm.getField(%22Testfeld%22);%0A        assertNotNull(%22There shall be an /AP entry for the field%22, formField.getCOSObject().getDictionaryObject(COSName.AP));%0A        assertNotNull(%22There shall be a /V entry for the field%22, formField.getCOSObject().getDictionaryObject(COSName.V));%0A%0A        formField = acroForm.getField(%22Testfeld2%22);%0A        assertNotNull(%22There shall be an /AP entry for the field%22, formField.getCOSObject().getDictionaryObject(COSName.AP));%0A        assertNotNull(%22There shall be a /V entry for the field%22, formField.getCOSObject().getDictionaryObject(COSName.V));%0A%0A        mergedPDF.close();%0A    %7D%0A    %0A    %0A    
 %0A%7D%0A

add unit test to ensure /AP and /V entry