Package com.lowagie.examples.general.copystamp

Source Code of com.lowagie.examples.general.copystamp.TwoOnOne

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.general.copystamp;

import java.io.FileOutputStream;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgRectangle;
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;

/**
* Reads the pages of an existing PDF file and puts 2 pages from the existing doc
* into one of the new doc.
*/
public class TwoOnOne {
    /**
     * Reads the pages of an existing PDF file and puts 2 pages from the existing doc
     * into one of the new doc.
     * @param args no arguments needed
     */
    public static void main(String[] args) {
        System.out.println("Import pages as images");
        try {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader("ChapterSection.pdf");
            // we retrieve the total number of pages
            int n = reader.getNumberOfPages();
            // we retrieve the size of the first page
            LwgRectangle psize = reader.getPageSize(1);
            float width = psize.getHeight();
            float height = psize.getWidth();
           
            // step 1: creation of a document-object
            LwgDocument document = new LwgDocument(new LwgRectangle(width, height));
            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2on1.pdf"));
            // step 3: we open the document
            document.open();
            // step 4: we add content
            PdfContentByte cb = writer.getDirectContent();
            int i = 0;
            int p = 0;
            while (i < n) {
                document.newPage();
                p++;
                i++;
                PdfImportedPage page1 = writer.getImportedPage(reader, i);
                cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
                if (i < n) {
                    i++;
                    PdfImportedPage page2 = writer.getImportedPage(reader, i);
                    cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
                }
                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb.beginText();
                cb.setFontAndSize(bf, 14);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0? 1 : 0)), width / 2, 40, 0);
                cb.endText();
            }
            // step 5: we close the document
            document.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
}
TOP

Related Classes of com.lowagie.examples.general.copystamp.TwoOnOne

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.