源码:Documentdocument=newDocument(PageSize.A4);PdfWriterwriter=PdfWriter.getInstance(document,newFileOutputStream("D:\\2M9006-A4-P251-1.pdf"));PdfReaderreader=newPdfRead...
源码:
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:\\2M9006-A4-P251-1.pdf"));
PdfReader reader = new PdfReader("D:\\M9006-A4-P251-1.pdf");
int n = reader.getNumberOfPages();
PdfImportedPage page = null;
Image image = null;
document.open();
for (int i = 1; i <= n; i++) {
System.out.println(n);
page = writer.getImportedPage(reader, i);
image = Image.getInstance(page);
if (i % 2 == 0) {
image.setRotationDegrees(180);
//image.setAbsolutePosition(70,0);
} else {
image.setRotationDegrees(180);
//image.setAbsolutePosition(-70,0);
}
image.setRotationDegrees(90);
image.setAlignment(Image.ALIGN_CENTER);
document.add(image);
}
document.newPage();
document.close();
M9006-A4-P251-1.pdf文件大小为:12.2M 内容为两张大图
Exception in thread "main" java.lang.NullPointerException
at com.lowagie.text.pdf.PdfDictionary.toPdf(PdfDictionary.java:149)
at com.lowagie.text.pdf.PdfStream.superToPdf(PdfStream.java:231)
at com.lowagie.text.pdf.PRStream.toPdf(PRStream.java:175)
at com.lowagie.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:167)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:370)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:350)
at com.lowagie.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:342)
at com.lowagie.text.pdf.PdfWriter.addToBody(PdfWriter.java:1875)
at com.lowagie.text.pdf.PdfReaderInstance.writeAllPages(PdfReaderInstance.java:169)
at com.lowagie.text.pdf.PdfWriter.addSharedObjectsToBody(PdfWriter.java:1173)
at com.lowagie.text.pdf.PdfWriter.close(PdfWriter.java:1231)
at com.lowagie.text.pdf.PdfDocument.close(PdfDocument.java:942)
at com.lowagie.text.Document.close(Document.java:520)
at com.serial.tt.main(tt.java:54)
展开