使用itext pdf压模在pdf中添加多个附件 [英] Add multiple attachments in a pdf using itext pdf stamper

查看:554
本文介绍了使用itext pdf压模在pdf中添加多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pdf文件中添加多个附件。
当我进入循环时它只附上最后一个附件。



示例代码

  PdfReader reader = new PdfReader(FILE); 

PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(realPath +/ Temp /+ sosValues.getCmaId()+。pdf));

。对于(SOSCustomerOrderFile cmaOrder:orderList)
{
PdfFileSpecification FS = PdfFileSpecification.fileEmbedded(stamper.getWriter(),NULL,cmaOrder.getFileName(),cmaOrder.getFileData() );
/ * stamper.getWriter(),null,test.txt,Some test.getBytes()); * /
stamper.addFileAttachment(Attachment,fs);
}
stamper.close();


解决方案

您已经调整了现有示例以尝试重现该问题,我们的SSCCE告诉我们您的指控是错误的。您可以通过调整我们的SSCCE来证明您是对的,我们可以在我们的机器上运行它并重现问题。


I want to add multiple attachments to a pdf file. When I go into the loop it only attaches the last attachment.

sample code

PdfReader reader = new PdfReader(FILE);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(realPath+"/Temp/"+sosValues.getCmaId()+".pdf"));

for(SOSCustomerOrderFile cmaOrder:orderList)
{
    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, cmaOrder.getFileName(), cmaOrder.getFileData());
    /*  stamper.getWriter(), null, "test.txt", "Some test".getBytes());*/
    stamper.addFileAttachment("Attachment", fs);
}
stamper.close();

解决方案

You have adapted the existing example AddEmbeddedFile to add more than one attachment, and you claim that only one attachment is added.

I can not reproduce this. I have also adapted the existing example by creating a new example: AddEmbeddedFiles

public static final String[] ATTACHMENTS = {
    "hello", "world", "what", "is", "up"
};
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    for (String s : ATTACHMENTS) {
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
            stamper.getWriter(), null, String.format("%s.txt", s),
            String.format("Some test: %s", s).getBytes());
        stamper.addFileAttachment(String.format("Content: %s", s), fs);
    }
    stamper.close();
}

The result contains all the expected attachments:

The only difference that I see, is that you give every embedded file the same name, but even if I do that, I can still see all the attachments correctly.

Another difference, is that I use an array of String values, but that shouldn't really matter, assuming that cmaOrder.getFileName() indeed returns a file name (e.g. "order1.doc", "order2.xls",...) and that cmaOrder.getFileData() returns a byte[] with the actual bytes of that file. By not telling us what the SOSCustomerOrderFile class is about, you are forcing us to make that assumption.

If you have the file on disk, you could also do something like this:

PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
    stamper.getWriter(), cmaOrder.getFilePath(), cmaOrder.getFileName(), null);

That is: if the SOSCustomerOrderFile class has a getFilePath() method that returns the path to the file. We don't know that, we don't have access (nor do we want access) to your complete code base. We can only create an SSCCE in order to try to reproduce the problem, and our SSCCE tells us that your allegation is wrong. You can prove that you're right by adapting our SSCCE in such a way that we can run it on our machines and reproduce the problem.

这篇关于使用itext pdf压模在pdf中添加多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆