缓慢访问AcroFields(iTextSharp)

时间:2015-12-30 11:10:45

标签: c# itextsharp acrofields

我使用iTextSharp从PDF中提取SignatureNames。 我遇到了访问大尺寸和多页PDF(~40MB和~5000页)的AcroFiels的问题(过度缓慢)。

这是我的代码段:

using iTextSharp.text.pdf;

private static List<byte[]> GetSignsFromPDF(string filePath)
{
    var result = new List<byte[]>();
    var randomAccessFileOrArray = new RandomAccessFileOrArray(filePath);
    var reader = new PdfReader(randomAccessFileOrArray, null);
    var fields = reader.AcroFields;

    if (fields == null)
    {
        return result;
    }

    var signatureNames = fields.GetSignatureNames();
    signatureNames.Sort();

    foreach (string name in signatureNames)
    {
        var sigDict = fields.GetSignatureDictionary(name);
        var contents = sigDict.GetAsString(PdfName.CONTENTS);

        if (contents != null)
        {
            result.Add(contents.GetOriginalBytes());
        }
    }

    return result;
}

有一种更智能/更快捷的方式来访问AcroFields,还是应该等待iTextSharp的东西?

非常感谢。

1 个答案:

答案 0 :(得分:2)

在评论中,猜测出现过度缓慢是由于AcroFields实例中字段集初始化过程中iText(Sharp)不仅检查了字段在目录 - &gt;中引用 AcroForm - &gt; 字段,但也(实际上最重要的)来自所有文档页面的 ANNOTS

幸运的是,这个初始化不会在AcroFields构造函数中进行,因此我们可以在不检查所有页面的情况下注入一个字段集合。

以下方法是内部AcroFields方法Fill(负责延迟初始化)的副本,删除了页面遍历并可通过反射访问隐藏成员。它可以用来测试猜想。

void fill(PdfReader reader, AcroFields acroFields)
{
    IDictionary<string, AcroFields.Item> fields = new LinkedDictionary<string, AcroFields.Item>();
    PdfDictionary top = (PdfDictionary)PdfReader.GetPdfObjectRelease(reader.Catalog.Get(PdfName.ACROFORM));
    if (top == null)
        return;
    PdfBoolean needappearances = top.GetAsBoolean(PdfName.NEEDAPPEARANCES);
    if (needappearances == null || !needappearances.BooleanValue)
        acroFields.GenerateAppearances = true;
    else
        acroFields.GenerateAppearances = false;
    PdfArray arrfds = (PdfArray)PdfReader.GetPdfObjectRelease(top.Get(PdfName.FIELDS));
    if (arrfds == null || arrfds.Size == 0)
        return;

    System.Reflection.FieldInfo valuesField = typeof(AcroFields.Item).GetField("values", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.Reflection.FieldInfo widgetsField = typeof(AcroFields.Item).GetField("widgets", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.Reflection.FieldInfo widgetRefsField = typeof(AcroFields.Item).GetField("widget_refs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.Reflection.FieldInfo mergedField = typeof(AcroFields.Item).GetField("merged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.Reflection.FieldInfo pageField = typeof(AcroFields.Item).GetField("page", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.Reflection.FieldInfo tabOrderField = typeof(AcroFields.Item).GetField("tabOrder", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

    for (int j = 0; j < arrfds.Size; ++j)
    {
        PdfDictionary annot = arrfds.GetAsDict(j);
        if (annot == null)
        {
            PdfReader.ReleaseLastXrefPartial(arrfds.GetAsIndirectObject(j));
            continue;
        }
        if (!PdfName.WIDGET.Equals(annot.GetAsName(PdfName.SUBTYPE)))
        {
            PdfReader.ReleaseLastXrefPartial(arrfds.GetAsIndirectObject(j));
            continue;
        }
        PdfArray kids = (PdfArray)PdfReader.GetPdfObjectRelease(annot.Get(PdfName.KIDS));
        if (kids != null)
            continue;
        PdfDictionary dic = new PdfDictionary();
        dic.Merge(annot);
        PdfString t = annot.GetAsString(PdfName.T);
        if (t == null)
            continue;
        String name = t.ToUnicodeString();
        if (fields.ContainsKey(name))
            continue;
        AcroFields.Item item = new AcroFields.Item();
        fields[name] = item;
        ((List<PdfDictionary>)valuesField.GetValue(item)).Add(dic); // item.AddValue(dic);
        ((List<PdfDictionary>)widgetsField.GetValue(item)).Add(dic); // item.AddWidget(dic);
        ((List<PdfIndirectReference>)widgetRefsField.GetValue(item)).Add(arrfds.GetAsIndirectObject(j)); //item.AddWidgetRef(arrfds.GetAsIndirectObject(j)); // must be a reference
        ((List<PdfDictionary>)mergedField.GetValue(item)).Add(dic); // item.AddMerged(dic);
        ((List<int>)pageField.GetValue(item)).Add((int)-1); // item.AddPage(-1);
        ((List<int>)tabOrderField.GetValue(item)).Add((int)-1); // item.AddTabOrder(-1);
    }

    System.Reflection.FieldInfo fieldsField = typeof(AcroFields).GetField("fields", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    fieldsField.SetValue(acroFields, fields);
}

应该尽早调用AcroFields实例,例如:

using (PdfReader reader = new PdfReader(file))
{
    AcroFields acroFields = reader.AcroFields;
    fill(reader, acroFields);
    ...

如果使用这种方法可以大大减少时间(同时提供所需的字段),则可以确认猜想。

查看代码时,人们认识到它没有正确地遍历字段结构:字段可以按层次排列,但代码只考虑第一级元素。不过,它应该足以对上面提到的猜想进行第一次测试。