FOP-3032: Allow to embed native PDF in AFP

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop-pdf-images/trunk@1894578 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageConverter b/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageConverter
index 8c8af29..5ee52d5 100644
--- a/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageConverter
+++ b/src/java/META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImageConverter
@@ -1 +1,2 @@
+org.apache.fop.render.pdf.pdfbox.ImageConverterPDF2AFP

 org.apache.fop.render.pdf.pdfbox.ImageConverterPDF2G2D

diff --git a/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2AFP.java b/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2AFP.java
new file mode 100644
index 0000000..90dcce0
--- /dev/null
+++ b/src/java/org/apache/fop/render/pdf/pdfbox/ImageConverterPDF2AFP.java
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+package org.apache.fop.render.pdf.pdfbox;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.pdfbox.multipdf.Splitter;
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+import org.apache.xmlgraphics.image.loader.Image;
+import org.apache.xmlgraphics.image.loader.ImageFlavor;
+import org.apache.xmlgraphics.image.loader.MimeEnabledImageFlavor;
+import org.apache.xmlgraphics.image.loader.impl.AbstractImageConverter;
+import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+
+public class ImageConverterPDF2AFP extends AbstractImageConverter {
+    public Image convert(Image src, Map hints) throws IOException {
+        if (!(src instanceof ImagePDF)) {
+            return null;
+        }
+        ImagePDF imgPDF = (ImagePDF)src;
+        int selectedPage = ImageUtil.needPageIndexFromURI(src.getInfo().getOriginalURI());
+        PDDocument pdDocument = imgPDF.getPDDocument();
+        Splitter splitter = new Splitter();
+        splitter.setStartPage(selectedPage + 1);
+        splitter.setEndPage(selectedPage + 1);
+        pdDocument = splitter.split(pdDocument).get(0);
+        ByteArrayOutputStream pdf = new ByteArrayOutputStream();
+        pdDocument.save(pdf);
+        MimeEnabledImageFlavor imageFlavor = new MimeEnabledImageFlavor(src.getFlavor(), ImagePDF.MIME_PDF);
+        return new ImageRawStream(src.getInfo(), imageFlavor, new ByteArrayInputStream(pdf.toByteArray()));
+    }
+
+    public ImageFlavor getSourceFlavor() {
+        return ImagePDF.PDFBOX_IMAGE;
+    }
+
+    public ImageFlavor getTargetFlavor() {
+        return ImageFlavor.RAW;
+    }
+
+    public int getConversionPenalty() {
+        return 100;
+    }
+}
diff --git a/test/java/org/apache/fop/render/pdf/ImageConverterPDF2AFPTestCase.java b/test/java/org/apache/fop/render/pdf/ImageConverterPDF2AFPTestCase.java
new file mode 100644
index 0000000..db76948
--- /dev/null
+++ b/test/java/org/apache/fop/render/pdf/ImageConverterPDF2AFPTestCase.java
@@ -0,0 +1,51 @@
+/*
+ * 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.fop.render.pdf;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.commons.io.IOUtils;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
+
+import org.apache.fop.render.pdf.pdfbox.ImageConverterPDF2AFP;
+import org.apache.fop.render.pdf.pdfbox.ImagePDF;
+
+public class ImageConverterPDF2AFPTestCase {
+    @Test
+    public void testConverter() throws Exception {
+        PDDocument orgdoc = PDFBoxAdapterTestCase.load(PDFBoxAdapterTestCase.ANNOT);
+        String orgPage = IOUtils.toString(orgdoc.getPage(1).getContents());
+        Assert.assertEquals(orgdoc.getNumberOfPages(), 2);
+        ImageInfo info = new ImageInfo("x.pdf#page=2", ImagePDF.MIME_PDF);
+        ImagePDF imagePDF = new ImagePDF(info, orgdoc);
+        ImageConverterPDF2AFP converter = new ImageConverterPDF2AFP();
+        ImageRawStream stream = (ImageRawStream) converter.convert(imagePDF, null);
+        PDDocument doc = PDDocument.load(stream.createInputStream());
+        PDPage page = doc.getPage(0);
+        Assert.assertEquals(orgPage, IOUtils.toString(page.getContents()));
+        Assert.assertEquals(doc.getNumberOfPages(), 1);
+        Assert.assertEquals(stream.getMimeType(), ImagePDF.MIME_PDF);
+        orgdoc.close();
+        doc.close();
+    }
+}
diff --git a/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java b/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
index 10105bf..8124d10 100644
--- a/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
+++ b/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
@@ -101,31 +101,31 @@
     protected static final String TTCID2 = "ttcid2.pdf";
     protected static final String TTSubset1 = "ttsubset.pdf";
     protected static final String TTSubset2 = "ttsubset2.pdf";
-    private static final String TTSubset3 = "ttsubset3.pdf";
-    private static final String TTSubset5 = "ttsubset5.pdf";
-    private static final String TTSubset6 = "ttsubset6.pdf";
-    private static final String TTSubset7 = "ttsubset7.pdf";
+    protected static final String TTSubset3 = "ttsubset3.pdf";
+    protected static final String TTSubset5 = "ttsubset5.pdf";
+    protected static final String TTSubset6 = "ttsubset6.pdf";
+    protected static final String TTSubset7 = "ttsubset7.pdf";
     protected static final String CFFCID1 = "cffcid1.pdf";
-    private static final String CFFCID2 = "cffcid2.pdf";
+    protected static final String CFFCID2 = "cffcid2.pdf";
     protected static final String Type1Subset1 = "t1subset.pdf";
     protected static final String Type1Subset2 = "t1subset2.pdf";
-    private static final String Type1Subset3 = "t1subset3.pdf";
-    private static final String Type1Subset4 = "t1subset4.pdf";
+    protected static final String Type1Subset3 = "t1subset3.pdf";
+    protected static final String Type1Subset4 = "t1subset4.pdf";
     protected static final String ROTATE = "rotate.pdf";
-    private static final String ANNOT = "annot.pdf";
-    private static final String SHADING = "shading.pdf";
-    private static final String LINK = "link.pdf";
-    private static final String IMAGE = "image.pdf";
-    private static final String HELLOTagged = "taggedWorld.pdf";
-    private static final String XFORM = "xform.pdf";
-    private static final String LOOP = "loop.pdf";
-    private static final String ERROR = "error.pdf";
-    private static final String LIBREOFFICE = "libreoffice.pdf";
-    private static final String SMASK = "smask.pdf";
-    private static final String TYPE0TT = "type0tt.pdf";
-    private static final String TYPE0CFF = "type0cff.pdf";
-    private static final String ACCESSIBLERADIOBUTTONS = "accessibleradiobuttons.pdf";
-    private static final String PATTERN = "pattern.pdf";
+    protected static final String ANNOT = "annot.pdf";
+    protected static final String SHADING = "shading.pdf";
+    protected static final String LINK = "link.pdf";
+    protected static final String IMAGE = "image.pdf";
+    protected static final String HELLOTagged = "taggedWorld.pdf";
+    protected static final String XFORM = "xform.pdf";
+    protected static final String LOOP = "loop.pdf";
+    protected static final String ERROR = "error.pdf";
+    protected static final String LIBREOFFICE = "libreoffice.pdf";
+    protected static final String SMASK = "smask.pdf";
+    protected static final String TYPE0TT = "type0tt.pdf";
+    protected static final String TYPE0CFF = "type0cff.pdf";
+    protected static final String ACCESSIBLERADIOBUTTONS = "accessibleradiobuttons.pdf";
+    protected static final String PATTERN = "pattern.pdf";
 
     private static PDFPage getPDFPage(PDFDocument doc) {
         final Rectangle2D r = new Rectangle2D.Double();