我已经能够编写代码来创建新的PDF或打开现有的PDF并追加。 我现在想要实现的是在其中插入另一个PDF。下面是我为了附加PDF而编写的示例代码。
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader reader = new PdfReader(templateInputStream);
PdfImportedPage page = writer.getImportedPage(reader, 1);
document.newPage();
cb.addTemplate(page, 0, 0);
// Append sample line
document.add(new Paragraph("my timestamp"));
document.close();
据我所知,Adobe Reader支持插入文档。不确定使用Java代码是否也能实现相同目的。
修改
以下是我在PDF中嵌入PDF的代码。点击并打开嵌入式pdf / attachment时,我收到错误。
public class AddEmbeddedFile {
public static final String SRC = "C:\\Users\\User\\Desktop\\testSource.pdf";
public static final String Embed = "C:\\Users\\User\\Desktop\\testEmbed.pdf";
public static final String DEST = "C:\\Users\\User\\Desktop\\testDest.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddEmbeddedFile().manipulatePdf(SRC, DEST, Embed);
}
public void manipulatePdf(String src, String dest, String embed) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfReader reader2 = new PdfReader(embed);
int n = reader2.getNumberOfPages();
reader2.close();
ByteArrayOutputStream boas;
byte[] PDFContent = null;
byte[] PDFContent2 = new byte[0];
for (int i = 0; i < n; ) {
reader2.selectPages(String.valueOf(++i));
boas = new ByteArrayOutputStream();
PdfStamper stamper2 = new PdfStamper(reader2, boas);
PDFContent = boas.toByteArray();
byte[] c = new byte[PDFContent.length + PDFContent2.length];
System.arraycopy(PDFContent, 0, c, 0, PDFContent.length);
System.arraycopy(PDFContent2, 0, c, PDFContent.length, PDFContent2.length);
PDFContent2 = c;
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
stamper.getWriter(), null, "tests.pdf", PDFContent2, "pdf", null, 0);
stamper.addFileAttachment("some test file", fs);
stamper.close();
}
}
答案 0 :(得分:3)
您在问题中使用的代码是错误的。如果您将PdfImportedPage
个对象导入PdfWriter
,则会丢失原始文档中可能存在的所有交互性。您需要改为使用PdfStamper
。
将附件添加到现有PDF有两种不同的方法。在评论中已经提到了一个,您可以参考AddEmbeddedFile示例:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
stamper.getWriter(), null, "test.txt", "Some test".getBytes());
stamper.addFileAttachment("some test file", fs);
stamper.close();
}
您有一个现有的PDF src
,其中嵌入了一个文本文件。应该很容易调整示例,以便添加PDF字节而不是纯文本。在这种情况下,最终用户只有在打开附件面板时才会看到PDF。
附加文件的另一种方法是使用附件注释:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
stamper.getWriter(), null, "test.txt", "Some test".getBytes());
Rectangle rect = new Rectangle(36, 770, 72, 806);
PdfAnnotation attachment = dfAnnotation.createFileAttachment(
stamper.getWriter(), rect, "My attachment", fs);
stamper.addAnnotation(attachment, 1);
stamper.close();
}
这将在rect
定义的坐标上的现有PDF的第一页上添加可见注释。
您也可能错误地使用“附加文件”这一概念。也许你的意思是看到将一个文件连接到另一个文件。我在回答以下问题时解释了这一点:How to merge documents correctly?
<强>更新强>
您正在使用此方法创建PdfFileSpecification
实例:
fileEmbedded(
PdfWriter writer,
String filePath,
String fileDisplay,
byte[] fileStore,
String mimeType,
PdfDictionary fileParameter,
int compressionLevel)
但你的参数都错了:
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
stamper.getWriter(), // correct
null, // should be "C:\\Users\\User\\Desktop\\testEmbed.pdf"
"tests.pdf", // correct
PDFContent2, // this is completely wrong!!!
"pdf", // should be "application/pdf"
null, // OK
0); // choosing 0 means that you don't want to compress the attachment. Why not?
您创建PdfContent2
的方式完全错误。很难想象你想在这里实现什么。您正在以完全违反PDF参考的方式连接PDF字节。
答案 1 :(得分:-1)
更新
PdfFileSpecification fileSpecification = PdfFileSpecification.fileEmbedded(pdfWriter,
pdfAttach.getAbsolutePath(), pdfAttach.getName(), null);
pdfWriter.addFileAttachment("Sample Attachment", fileSpecification);