我正在尝试使用itext将A4 pdf转换为A5小册子格式pdf,但未按照A5格式转换。任何人都建议如何做,这将对您大有帮助。
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
// Creating a reader
PdfReader reader = new PdfReader(src);
// step 1
Document document = new Document(PageSize.A5.rotate());
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
float a4_width = PageSize.A4.getWidth();
int n = reader.getNumberOfPages();
int p = 0;
PdfImportedPage page;
while (p++ < n) {
page = writer.getImportedPage(reader, p);
if (p % 2 == 1) {
canvas.addTemplate(page, 0, 0);
} else {
canvas.addTemplate(page, a4_width, 0);
document.newPage();
}
}
// step 5
document.close();
reader.close();
}