TwoOnOnePDF.java Source code

Java tutorial

Introduction

Here is the source code for TwoOnOnePDF.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class TwoOnOnePDF {
    public static void main(String[] args) {
        System.out.println("");
        try {
            PdfReader reader = new PdfReader("YourOwnPDF.pdf");
            int n = reader.getNumberOfPages();

            Rectangle psize = reader.getPageSize(1);
            float width = psize.height();
            float height = psize.width();

            Document document = new Document(new Rectangle(width, height));
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TwoOnOnePDF.pdf"));
            document.open();

            PdfContentByte cb = writer.getDirectContent();
            document.newPage();
            PdfImportedPage page1 = writer.getImportedPage(reader, 1);
            cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);

            document.close();
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
}