1、官方的example文件:iText · GitHub
itext-java-7.2.5\kernel\src\test\java\com\itextpdf\kernel\pdf\PdfStampingTest.java
2、修改代码:
@Testpublic void stamping1() throws IOException {String filename1 = destinationFolder + "stamping1_1.pdf";String filename2 = destinationFolder + "stamping1_2.pdf";PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(filename1));pdfDoc1.getDocumentInfo().setAuthor("Alexander Chingarev").setCreator("iText 6").setAuthor("aaaaaaaaaaaa").setKeywords("123").setProducer("abcdefghijklmkuyvklfsdklsdfjksdfj").setSubject("kldsjf").setTrapped(new PdfName("hellksdl")).setTitle("Empty iText 6 Document");PdfPage page1 = pdfDoc1.addNewPage();page1.getContentStream(0).getOutputStream().write(ByteUtils.getIsoBytes("%Hello World\n"));page1.flush();pdfDoc1.close();PdfReader reader2 = new PdfReader(filename1);PdfWriter writer2 = new PdfWriter(filename2);PdfDocument pdfDoc2 = new PdfDocument(reader2, writer2);pdfDoc2.getDocumentInfo().setCreator("iText 7").setTitle("Empty iText 7 Document");pdfDoc2.close();PdfReader reader3 = new PdfReader(filename2);PdfDocument pdfDoc3 = new PdfDocument(reader3);for (int i = 0; i < pdfDoc3.getNumberOfPages(); i++) {pdfDoc3.getPage(i + 1);}assertEquals("Rebuilt", false, reader3.hasRebuiltXref());assertEquals("Fixed", false, reader3.hasFixedXref());verifyPdfPagesCount(pdfDoc3.getCatalog().getPageTree().getRoot().getPdfObject());pdfDoc3.close();PdfReader reader = new PdfReader(destinationFolder + "stamping1_2.pdf");PdfDocument document = new PdfDocument(reader);assertEquals("Rebuilt", false, reader.hasRebuiltXref());PdfDictionary trailer = document.getTrailer();PdfDictionary info = trailer.getAsDictionary(PdfName.Info);PdfString creator = info.getAsString(PdfName.Creator);assertEquals("iText 7", creator.toString());byte[] bytes = document.getPage(1).getContentBytes();assertEquals("%Hello World\n", new String(bytes));String date = document.getDocumentInfo().getPdfObject().getAsString(PdfName.ModDate).getValue();Calendar cl = PdfDate.decode(date);double diff = DateTimeUtil.getUtcMillisFromEpoch(null) - DateTimeUtil.getUtcMillisFromEpoch(cl);String message = "Unexpected creation date. Different from now is " + (float) diff / 1000 + "s";assertTrue(message, diff < 5000);document.close();}
3、我们来看一下生成的pdf中的信息如何显示的:
我们发现这里面的信息,已成功的被修改了。
4、读取相关信息:
PdfReader reader = new PdfReader(destinationFolder + "stamping1_1.pdf");PdfDocument document = new PdfDocument(reader);assertEquals("Rebuilt", false, reader.hasRebuiltXref());PdfDictionary trailer = document.getTrailer();PdfDictionary info = trailer.getAsDictionary(PdfName.Info);PdfString creator = info.getAsString(PdfName.Creator);System.out.println(creator.toString());System.out.println(info.toString());System.out.println(info.getAsString(PdfName.Title));System.out.println(info.getAsString(PdfName.Producer));assertEquals("iText 7", creator.toString());byte[] bytes = document.getPage(1).getContentBytes();assertEquals("%Hello World\n", new String(bytes));String date = document.getDocumentInfo().getPdfObject().getAsString(PdfName.ModDate).getValue();Calendar cl = PdfDate.decode(date);double diff = DateTimeUtil.getUtcMillisFromEpoch(null) - DateTimeUtil.getUtcMillisFromEpoch(cl);String message = "Unexpected creation date. Different from now is " + (float) diff / 1000 + "s";assertTrue(message, diff < 5000);document.close();
我们会发现Console都输出来了。