SDN Featured Content

Do you Think Hibernate is better technology that EJB

Java/J2EE blogs

Welcome to my java /J2EE blogs.


Monday, December 28, 2009

Splitting of a pdf document using java itext API

Below is the simple java program that can split the pdf document to a specific range of the pages.

 

package com.pdf.split;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFSplit
{
    /**
     * Default constructor
     */
    public PDFSplit(){
    }
    /**
     * parameterized constructor
     */
    public PDFSplit(String fromPath,int fromPage,int toPage,String toPath){
    splitPDF1(fromPath,fromPage,toPage,toPath);
    }

    public static void main(String[] args)
    {

        try
        {

            splitPDF(new FileInputStream("C:\\PAYware_SIM_Integration_Guide.pdf"),

            new FileOutputStream("C:\\output2.pdf"), 2, 10);

        }
        catch (Exception e)
        {

            e.printStackTrace();

        }

    }

    /**
     *
     * @param inputStream
     * @param outputStream
     * @param fromPage
     * @param toPage
     */
    public static void splitPDF(InputStream inputStream,

    OutputStream outputStream, int fromPage, int toPage)
    {

        Document document = new Document();

        try
        {
            PdfReader inputPDF = new PdfReader(inputStream);
            int totalPages = inputPDF.getNumberOfPages();
            if (fromPage > toPage)
            {
            fromPage = toPage;
            }
            if (toPage > totalPages)
            {
            toPage = totalPages;
            }
            // Create a writer for the outputstream 
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data 
            PdfImportedPage page;
            while (fromPage <= toPage)
            {
            document.newPage();
            page = writer.getImportedPage(inputPDF, fromPage);
            cb.addTemplate(page, 0, 0);
            fromPage++;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        finally
        {
        if (document.isOpen()){
            document.close();
        }
        try{
        if (outputStream != null)
        outputStream.close();
        }
        catch (IOException ioe)
        {
        ioe.printStackTrace();
        }
        }
    }

    /**
     *
     * @param inputStream
     * @param outputStream
     * @param fromPage
     * @param toPage
     */
    public static void splitPDF1(String pathFrom,int fromPage, int toPage,String toPath){
        Document document = new Document();
        OutputStream outputStream=null;
        InputStream inputStream=null;
        try
        {
            inputStream=new FileInputStream(pathFrom);
            outputStream=new FileOutputStream(toPath);
            PdfReader inputPDF = new PdfReader(inputStream);
            int totalPages = inputPDF.getNumberOfPages();
            //make fromPage equals to toPage if it is greater 
            if (fromPage > toPage)
            {
            fromPage = toPage;
            }
            if (toPage > totalPages)
            {
            toPage = totalPages;
            }
            // Create a writer for the outputstream 
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data 
            PdfImportedPage page;
            while (fromPage <= toPage)
            {
            document.newPage();
            page = writer.getImportedPage(inputPDF, fromPage);
            cb.addTemplate(page, 0, 0);
            fromPage++;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        finally
        {
        if (document.isOpen()){
            document.close();
        }
        try{
        if (outputStream != null)
        outputStream.close();
        }
        catch (IOException ioe)
        {
        ioe.printStackTrace();
        }
        }
    }
}

No comments: