Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

itext Split PDF Vertically

I have requirement where I have to split a PDF page right at center vertically. I searched through various posts and could not identify the right way to do it

I want to use iText Library using Java.

I used the SplitPDFFile.java from

iText: split a PDF into several PDF (1 per page)

and modified it like below, but page not getting split but it copies entire page.

import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

    public class SplitPDFFile {

        /**
         * @param args
         */
        public static void main(String[] args) {

            try {
                String inFile = "C:/input.pdf";

               System.out.println ("Reading " + inFile);
                PdfReader reader = new PdfReader(inFile);
                Rectangle cropBox = reader.getCropBox(1);            
                Rectangle  psize = reader.getPageSize(1);
                cropBox.setRight(psize.getWidth()/2);
                System.out.println(psize.getWidth());
                System.out.println(psize.getHeight());
                int n = reader.getNumberOfPages();
                System.out.println ("Number of pages : " + n);
                int i = 0;
                while ( i < n ) {
                    String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
                        + "-" + String.format("%03d", i + 1) + ".pdf";
                    System.out.println ("Writing " + outFile);
                    Document document = new Document(cropBox);
                    PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
                    writer.setCropBoxSize(cropBox); 
                    document.open();
                    PdfImportedPage page = writer.getImportedPage(reader, ++i);
                    writer.addPage(page);
                    document.close();
                    writer.close();                     
                } 
            }
            catch (Exception e) {
                e.printStackTrace();
            }   
        }   
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this method using PdfCopy, intermittently manipulating the PdfReader copied from:

void splitIntoHalfPages(InputStream source, File target) throws IOException, DocumentException
{
    final PdfReader reader = new PdfReader(source);

    try (   OutputStream targetStream = new FileOutputStream(target)    )
    {
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, targetStream);
        document.open();

        for (int page = 1; page <= reader.getNumberOfPages(); page++)
        {
            PdfDictionary pageN = reader.getPageN(page);
            Rectangle cropBox = reader.getCropBox(page);
            PdfArray leftBox = new PdfArray(new float[]{cropBox.getLeft(), cropBox.getBottom(), (cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getTop()});
            PdfArray rightBox = new PdfArray(new float[]{(cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getBottom(), cropBox.getRight(), cropBox.getTop()});

            PdfImportedPage importedPage = copy.getImportedPage(reader, page);
            pageN.put(PdfName.CROPBOX, leftBox);
            copy.addPage(importedPage);
            pageN.put(PdfName.CROPBOX, rightBox);
            copy.addPage(importedPage);
        }

        document.close();
    }
    finally
    {
        reader.close();
    }
}

(SplitIntoHalfPages.java)

This methods creates a copy of the source document containing each page twice, once with the CropBox limited to the left half page, once to the right one.

Beware: This method only splits the page content. If your source PDFs have annotations, you might want to also process them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...