itext与acrofields的pdf合并-合并时字段丢失 [英] itext sharp merge pdfs with acrofields - fields go missing when merging

查看:692
本文介绍了itext与acrofields的pdf合并-合并时字段丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经尝试过了,但是没有用. form.GenerateAppearances = true;我合并了2个文档,然后将其保存.然后,我再次打开它以填充所有字段.它说所有的Acrofields钥匙都不见了,但是当我在Nitro pro中打开它时.为什么在代码中看不到它们?保存之前是否必须添加一些内容?

I have tried this now and its not working. form.GenerateAppearances = true; I merge my 2 documents and then save it. Then I open it again to populate all the fields. It says all the Acrofields keys are gone but when I open it in Nitro pro its there. Why can't I see them in code? Do I have to add something before I save?

private static void CombineAndSavePdf1(string savePath, List<string> lstPdfFiles)
{
    using (Stream outputPdfStream = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, outputPdfStream);
        document.Open();
        PdfReader reader;
        int totalPageCnt;
        PdfStamper stamper;
        string[] fieldNames;
        foreach (string file in lstPdfFiles)
        {
            reader = new PdfReader(file);
            totalPageCnt = reader.NumberOfPages;
            for (int pageCnt = 0; pageCnt < totalPageCnt; )
            {
                //have to create new reader for each page or PdfStamper will throw error
                reader = new PdfReader(file);
                stamper = new PdfStamper(reader, outputPdfStream);
                fieldNames = new string[stamper.AcroFields.Fields.Keys.Count];
                stamper.AcroFields.Fields.Keys.CopyTo(fieldNames, 0);
                foreach (string name in fieldNames)
                {
                    stamper.AcroFields.RenameField(name, name);
                }

                copy.AddPage(copy.GetImportedPage(reader, ++pageCnt));

            }
            copy.FreeReader(reader);
        }
    }
}

推荐答案

您正在以错误的方式合并文档.请参阅 MergeForms 以了解如何正确执行此操作.您的代码中缺少的关键行是:

You are merging the documents the wrong way. See MergeForms to find out how to do it correctly. The key line that is missing in your code, is:

copy.setMergeFields();

没有它,这些字段就会消失(如您所注意到的).

Without it, the fields disappear (as you have noticed).

还有一个 MergeForms2 示例,该示例说明了如何合并两个相同的表单.在这种情况下,您需要重命名字段,因为每个字段都需要具有唯一的名称.我添加了对第二个示例的引用,因为我看到您也尝试重命名字段.但是,您的代码中存在一个严重缺陷:您创建了stamper对象,但从未执行过stamper.close().您使用reader对象也是有问题的.总而言之,最好丢掉您的代码,并使用iText官方网站上的两个示例重新开始.

There's also a MergeForms2 example that explains how to merge two identical forms. In this case, you need to rename the fields, because each field needs to have a unique name. I'm adding a reference to this second example, because I see that you also try renaming the fields. There is, however, a serious flaw in your code: you create a stamper object, but you never do stamper.close(). Your use of the reader object is also problematic. All in all, it would be best to throw away your code, and to start anew using the two examples from the official iText web site.

更新:我添加了标签到你的问题.直到那时我才注意到您使用的是iTextSharp而不是iText.对于C#开发人员来说,将Java代码移植到C#应该很容易,但是我从未编写过C#程序,因此请像使用伪代码一样使用JAVA示例. C#中的代码没有什么不同.

Update: I've added the tags itext and itextsharp to your question. Only then I noticed that you're using iTextSharp instead of iText. Porting the Java code to C# should be easy for a C# developer, but I've never written a C# program, so please use the JAVA examples as if you were using pseudo-code. The code in C# won't be that different.

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
    copy.setMergeFields();
    document.open();
    List<PdfReader> readers = new ArrayList<PdfReader>();
    for (int i = 0; i < 3; ) {
        PdfReader reader = new PdfReader(renameFields(src, ++i));
        readers.add(reader);
        copy.addDocument(reader);
    }
    document.close();
    for (PdfReader reader : readers) {
        reader.close();
    }
}

public byte[] renameFields(String src, int i) throws IOException, DocumentException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, baos);
    AcroFields form = stamper.getAcroFields();
    Set<String> keys = new HashSet<String>(form.getFields().keySet());
    for (String key : keys) {
        form.renameField(key, String.format("%s_%d", key, i));
    }
    stamper.close();
    reader.close();
    return baos.toByteArray();
}

这篇关于itext与acrofields的pdf合并-合并时字段丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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