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

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.util.Matrix;

/**
 * Example to show superimposing a PDF page onto another PDF.
 */
public final class SuperimposePage
{
    
    private SuperimposePage()
    {
    }

    public static void main(String[] args) throws IOException
    {
        if (args.length != 2)
        {
            System.err.println("usage: " + SuperimposePage.class.getName() +
                    " <source-pdf> <dest-pdf>");
            System.exit(1);
        }
        String sourcePath = args[0];
        String destPath = args[1];
        
        try (PDDocument sourceDoc = PDDocument.load(new File(sourcePath)))
        {
            int sourcePage = 1;

            // create a new PDF and add a blank page
            try (PDDocument doc = new PDDocument())
            {
                PDPage page = new PDPage();
                doc.addPage(page);

                // write some sample text to the new page
                PDPageContentStream contents = new PDPageContentStream(doc, page);
                contents.beginText();
                contents.setFont(PDType1Font.HELVETICA_BOLD, 12);
                contents.newLineAtOffset(2, PDRectangle.LETTER.getHeight() - 12);
                contents.showText("Sample text");
                contents.endText();
                
                // Create a Form XObject from the source document using LayerUtility
                LayerUtility layerUtility = new LayerUtility(doc);
                PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, sourcePage - 1);
                
                // draw the full form
                contents.drawForm(form);
                
                // draw a scaled form
                contents.saveGraphicsState();
                Matrix matrix = Matrix.getScaleInstance(0.5f, 0.5f);
                contents.transform(matrix);
                contents.drawForm(form);
                contents.restoreGraphicsState();

                // draw a scaled and rotated form
                contents.saveGraphicsState();
                matrix.rotate(1.8 * Math.PI); // radians
                contents.transform(matrix);
                contents.drawForm(form);
                contents.restoreGraphicsState();

                contents.close();
                doc.save(destPath);
            }
        }
    }
}

@@ -2178,24 +2178,29 @@
             
+try (
 PDPageConten
@@ -2252,18 +2252,40 @@
 c, page)
-;%0A
+)%0A                %7B%0A    
         
@@ -2322,32 +2322,36 @@
                 
+  
+  
 contents.setFont
@@ -2380,24 +2380,28 @@
 _BOLD, 12);%0A
+    
             
@@ -2466,24 +2466,28 @@
 ht() - 12);%0A
+    
             
@@ -2532,32 +2532,36 @@
                 
+    
 contents.endText
@@ -2572,33 +2572,40 @@
                 
-%0A
+    %0A   
                 
@@ -2604,16 +2604,17 @@
         
+ 
 // Creat
@@ -2674,16 +2674,20 @@
 Utility%0A
+    
         
@@ -2761,16 +2761,20 @@
         
+    
 PDFormXO
@@ -2852,33 +2852,41 @@
                 
+ 
-%0A
+   %0A    
                 
@@ -2915,32 +2915,36 @@
                 
+   
+ 
 contents.drawFor
@@ -2960,32 +2960,36 @@
                 
+    
 %0A               
@@ -2981,32 +2981,36 @@
                 
+    
 // draw a scaled
@@ -3023,32 +3023,36 @@
                 
+ 
+   
 contents.saveGra
@@ -3057,32 +3057,36 @@
 raphicsState();%0A
+    
                 
@@ -3138,16 +3138,20 @@
  0.5f);%0A
+    
         
@@ -3194,32 +3194,36 @@
                 
+    
 contents.drawFor
@@ -3223,32 +3223,36 @@
 drawForm(form);%0A
+    
                 
@@ -3276,33 +3276,55 @@
 raphicsState();%0A
-%0A
+                    %0A  
                 
@@ -3315,32 +3315,34 @@
                 
+  
 // draw a scaled
@@ -3351,32 +3351,36 @@
 nd rotated form%0A
+    
                 
@@ -3425,16 +3425,20 @@
         
+    
 matrix.r
@@ -3478,32 +3478,36 @@
                 
+ 
+   
 contents.transfo
@@ -3526,32 +3526,36 @@
                 
+    
 contents.drawFor
@@ -3571,32 +3571,36 @@
                 
+   
+ 
 contents.restore
@@ -3616,17 +3616,16 @@
 tate();%0A
-%0A
         
@@ -3636,25 +3636,9 @@
     
-contents.close();
+%7D
 %0A   

use try-with-resources