我已经能够编写代码来创建新的 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();
Run Code Online (Sandbox Code Playgroud)
据我所知,Adobe Reader 确实支持插入文档。不确定是否也可以使用 Java 代码来实现。
编辑:
下面是我编写的将 PDF 嵌入到 PDF 中的代码。单击并打开嵌入的 pdf/附件时出现错误。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
您在问题中使用的代码是错误的。如果您将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();
}
Run Code Online (Sandbox Code Playgroud)
您有一个src嵌入文本文件的现有 PDF 。修改示例应该相当容易,以便您添加 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();
}
Run Code Online (Sandbox Code Playgroud)
这将在现有 PDF 的第一页上由 定义的坐标添加可见注释rect。
您也可能错误地使用了“附加文件”的概念。也许您的意思是将一个文件连接到另一个文件。这在我对以下问题的回答中得到了解释:如何正确合并文档?
更新:
您正在使用此方法创建一个PdfFileSpecification实例:
fileEmbedded(
PdfWriter writer,
String filePath,
String fileDisplay,
byte[] fileStore,
String mimeType,
PdfDictionary fileParameter,
int compressionLevel)
Run Code Online (Sandbox Code Playgroud)
但是你的参数全错了:
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?
Run Code Online (Sandbox Code Playgroud)
你创造的方式PdfContent2是完全错误的。很难想象你在这里想要实现什么。您正在以完全违反 PDF 参考的方式连接 PDF 字节。
| 归档时间: |
|
| 查看次数: |
8042 次 |
| 最近记录: |