pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1643663 - /pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
Date Sun, 07 Dec 2014 11:49:07 GMT
Author: msahyoun
Date: Sun Dec  7 11:49:07 2014
New Revision: 1643663

URL: http://svn.apache.org/r1643663
Log:
PDFBOX-2197 add transformation to sample

Modified:
    pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java

Modified: 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=1643663&r1=1643662&r2=1643663&view=diff
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
(original)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/documentmanipulation/SuperimposePage.java
Sun Dec  7 11:49:07 2014
@@ -20,6 +20,7 @@ import java.awt.geom.AffineTransform;
 
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
@@ -30,9 +31,10 @@ import org.apache.pdfbox.util.LayerUtili
  * Example to show superimposing a PDF page onto another PDF.
  *
  */
-public class SuperimposePage {
+public class SuperimposePage
+{
 
-    public static void main(String[] args)
+    public static void main(final String[] args)
     {
         try
         {
@@ -40,6 +42,11 @@ public class SuperimposePage {
             // Create a new document with some basic content
             PDDocument aDoc = new PDDocument();
             PDPage aPage = new PDPage();
+
+            // get the page crop box. Will be used later to place the
+            // imported page.
+            PDRectangle cropBox = aPage.findCropBox();
+
             aDoc.addPage(aPage);
 
             PDPageContentStream aContent = new PDPageContentStream(aDoc, aPage);
@@ -60,22 +67,20 @@ public class SuperimposePage {
             PDDocument toBeImported = PDDocument.load(args[0]);
 
             // Get the page as a PDXObjectForm to place it
-            PDXObjectForm mountable = layerUtility.importPageAsForm(
-                    toBeImported, 0);
+            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);
+            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
+            // use some sample transformations 
+            AffineTransform transform = new AffineTransform(0, 0.5, -0.5, 0, cropBox.getWidth(),
0);
+            contentStream.drawXObject(mountable, transform);
+            transform = new AffineTransform(0.5, 0.5, -0.5, 0.5, 0.5 * cropBox.getWidth(),
0.2 * cropBox.getHeight());
             contentStream.drawXObject(mountable, transform);
 
             // restore former graphics state
@@ -93,4 +98,4 @@ public class SuperimposePage {
             System.out.println(" error creating pdf file." + e.toString());
         }
     }
-}
\ No newline at end of file
+}



Mime
View raw message