-
Split a PDF file (using iText)java 2022. 7. 7. 13:22728x90반응형
This HowTo is based on the iText package. You need a recent version (ex. 2.*)
This a command line utility. You specify the pdf file to be split. Each page is extracted to its own pdf file.
/* * java SplitPDFFile file.pdf * * gives file-001.pdf ... file-nnn.pdf * * itext-2.1.5.jar */ import java.io.FileOutputStream; import com.lowagie.text.Document; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; public class SplitPDFFile { /** * @param args */ public static void main(String[] args) { try { String inFile = args[0].toLowerCase(); 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 */ } }
itextpdf-5.4.0.jar.zip1.71MBitextpdf-5.4.0-sources.jar.zip1.78MB728x90반응형'java' 카테고리의 다른 글
Java 배열로 스택(Stack) 구현하기 (0) 2022.08.17 Java - 문자열(String)에서 숫자(int)만 추출하는 방법 (0) 2022.08.17 [Java] iText를 이용하여 HTML 텍스트를 PDF로 변환하기. (0) 2022.07.07 Guava 요약 4-Cache (0) 2022.05.24 Guava Cache 메모리 캐 시 사용 실천 - 정시 비동기 리 셋 및 단순 추상 패키지 (6) 2022.05.24