You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2014/11/17 08:41:43 UTC

svn commit: r1640085 - in /pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation: ./ SuperimposePage.java package-info.java

Author: msahyoun
Date: Mon Nov 17 07:41:43 2014
New Revision: 1640085

URL: http://svn.apache.org/r1640085
Log:
PDFBOX-2197 Sample for superimposing a page

Added:
    pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/
    pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java   (with props)
    pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java   (with props)

Added: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java?rev=1640085&view=auto
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java (added)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java Mon Nov 17 07:41:43 2014
@@ -0,0 +1,96 @@
+/*
+ * 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.examples.documentmanipulation;
+
+import java.awt.geom.AffineTransform;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
+import org.apache.pdfbox.util.LayerUtility;
+
+/**
+ * Example to show superimposing a PDF page onto another PDF.
+ *
+ */
+public class SuperimposePage {
+
+    public static void main(String[] args)
+    {
+        try
+        {
+
+            // Create a new document with some basic content
+            PDDocument aDoc = new PDDocument();
+            PDPage aPage = new PDPage();
+            aDoc.addPage(aPage);
+
+            PDPageContentStream aContent = new PDPageContentStream(aDoc, aPage);
+
+            PDFont font = PDType1Font.HELVETICA_BOLD;
+            aContent.beginText();
+            aContent.setFont(font, 12);
+            aContent.moveTextPositionByAmount(2, 5);
+            aContent.drawString("Import a pdf file:");
+            aContent.endText();
+            aContent.close();
+
+            // Superimpose a page form a source document
+
+            // This will handle the actual import and resources
+            LayerUtility layerUtility = new LayerUtility(aDoc);
+
+            PDDocument toBeImported = PDDocument.load(args[0]);
+
+            // Get the page as a PDXObjectForm to place it
+            PDXObjectForm mountable = layerUtility.importPageAsForm(
+                    toBeImported, 0);
+            // add compression to the stream (import deactivates compression)
+            mountable.getPDStream().addCompression();
+
+            // add to the existing content stream
+            PDPageContentStream contentStream = new PDPageContentStream(aDoc,
+                    aPage, true, true);
+
+            // Store the graphics state
+            contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));
+
+            // use a transformation to be able to scale and move easily
+            AffineTransform transform = new AffineTransform();
+
+            // draw the PDXObjectForm
+            contentStream.drawXObject(mountable, transform);
+
+            // restore former graphics state
+            contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
+            contentStream.close();
+
+            // close the imported document
+            toBeImported.close();
+
+            aDoc.save(args[1]);
+            aDoc.close();
+        }
+        catch (Exception e)
+        {
+            System.out.println(" error creating pdf file." + e.toString());
+        }
+    }
+}
\ No newline at end of file

Propchange: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java?rev=1640085&view=auto
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java (added)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java Mon Nov 17 07:41:43 2014
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+/**
+ * Set of samples how to work with existing PDF files.
+ *
+ */
+
+package org.apache.pdfbox.examples.documentmanipulation;
\ No newline at end of file

Propchange: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain