我正在拆分一个pdf文件,它只有一页,而且相当长而且很大,我 想要将其分为两页或更多页。
我尝试使用itext5
。
public static void splitPDF(InputStream inputStream,
OutputStream outputStream, int fromPage, int toPage) {
Rectangle rect = new Rectangle(PageSize.A4);
Document document = new Document(rect);
try {
PdfReader.unethicalreading = true;
PdfReader inputPDF = new PdfReader(inputStream);
int totalPages = inputPDF.getNumberOfPages();
System.out.println(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 = writer.getImportedPage(inputPDF, 1);
document.newPage();
cb.addTemplate(page, 0, 0);
document.newPage();
cb.addTemplate(page, -PageSize.A4.getWidth(), 0);
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();
}
}
}
我希望将一页分成两个或更多。