Общее·количество·просмотров·страницы

Java Dev Notes - разработка на Java (а также на JavaScript/Python/Flex и др), факты, события из АйТи

пятница, 31 августа 2012 г.

Разбивка PDF на страницы

Исходник говорит сам за себя:
import java.io.FileOutputStream;

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

/**
 * See
 * http://stackoverflow.com/questions/5736675/itext-split-a-pdf-into-several-pdf-1-per-page
 * 
 * Pdf to image:
 * http://stackoverflow.com/questions/4886042/pdf-to-image-using-java
 *
 * Display PDF into flash:http://www.swftools.org/
 * http://stackoverflow.com/questions/580807/how-can-i-show-doc-or-rtf-or-pdf-in-flash-player-or-in-none-editble-format
 * 
 * Display PDF in html5
 * https://github.com/mozilla/pdf.js
 * http://www.pdftron.com/pdfnet/webviewer/demo.html
 * 
 *  http://stackoverflow.com/questions/3113334/is-there-any-way-to-embed-a-pdf-file-into-an-html5-page
 *  
 *  pdf.js: Rendering PDF with HTML5 and JavaScript
 *  http://andreasgal.com/2011/06/15/pdf-js/
 *  http://habrahabr.ru/post/122034/
 *  
 *  google on
 *  html5 view PDF
 *  
 */
public class PdfIntoPagesMain {

 public static void main(String[] args) {

        try {
            //String inFile = args[0].toLowerCase();
         
         String inFile = "book1/book1.pdf";
         
            System.out.println ("Reading " + inFile);
            PdfReader reader = new PdfReader(inFile);
            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(reader.getPageSizeWithRotation(1));
                PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
                document.open();
                PdfImportedPage page = writer.getImportedPage(reader, ++i);
                writer.addPage(page);
                document.close();
                writer.close();
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }

        /* example : 
            java SplitPDFFile d:\temp\x\tx.pdf

            Reading d:\temp\x\tx.pdf
            Number of pages : 3
            Writing d:\temp\x\tx-001.pdf
            Writing d:\temp\x\tx-002.pdf
            Writing d:\temp\x\tx-003.pdf
         */

    }

}

Постоянные читатели