【问题标题】:Flatten dynamic pdf using iTextSharp使用 iTextSharp 展平动态 pdf
【发布时间】:2015-10-30 07:39:26
【问题描述】:

我正在使用 itextsharp 从模板 pdf 文件创建 pdf。但是新的 pdf 文件是像动态 pdf 一样创建的。我想将此文件转换为静态 pdf 文件,所以我尝试使用 xfaworker。我从 itextsharp.licensekey.dll 收到“签名已损坏”错误。我如何使用 xfaworker 或其他 dll 来展平动态 pdf?

public string Create(FaxPDFModel model, MemoryStream ms)
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string templatePath = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
    PdfReader pdfTemplate = new PdfReader(Path.Combine(Path.Combine(templatePath, "Docs"), "fax_template.pdf"));
    PdfStamper stamper = new PdfStamper(pdfTemplate, ms);
    stamper.Writer.CloseStream = false;
    BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "ISO-8859-9", BaseFont.EMBEDDED);
    var acroFields = stamper.AcroFields;
    acroFields.GenerateAppearances = true;
    stamper.FormFlattening = true;
    acroFields.AddSubstitutionFont(bf);
    acroFields.SetField("Name", "Mutabakat test");
    acroFields.SetField("Title", "DANIŞMANLIK");
    acroFields.SetField("Department", "test");
    acroFields.SetField("Phone", "0 (212) 555 55 55");
    stamper.Close();

    string path = Path.Combine(Path.Combine(templatePath, "Docs"), System.Guid.NewGuid().ToString());
    string pdfPath = path + ".pdf";
    Document document = new Document();
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
    XFAFlattener xfaf = new XFAFlattener(document, writer);
    ms.Position = 0;   
    xfaf.Flatten(new PdfReader(ms));
    document.Close();
    return pdfPath;
}

【问题讨论】:

    标签: c# pdf pdf-generation itextsharp xfa


    【解决方案1】:

    如果你想解决你的问题,你必须从修复以下错误开始:

    1.您正在使用代码来填写 AcroForms 而不是代码来填写 XFA 表单:

    如果您有动态 XFA 表单,您的 PDF 将充当 XML 的容器。这种形式不需要键值对形式的数据。此表单需要存储为 XML 的数据。

    您不能使用此代码:

    var acroFields = stamper.AcroFields;
    acroFields.AddSubstitutionFont(bf);
    acroFields.SetField("Name", "Mutabakat test");
    acroFields.SetField("Title", "DANIŞMANLIK");
    acroFields.SetField("Department", "test");
    acroFields.SetField("Phone", "0 (212) 555 55 55");
    

    此代码要求您的表单是 AcroForm。您需要像这样填写表格:

    AcroFields form = stamper.AcroFields;
    XfaForm xfa = form.Xfa;
    xfa.FillXfaForm(new FileStream(xml, FileMode.Open));
    

    在这个sn-p中xml指的是存储为XML的数据。

    2。您尝试在展平表单之前展平表单:

    我在代码的第一部分看到了这一行:

     stamper.FormFlattening = true;
    

    使用此行,您将删除 PDF 中的所有交互性。关闭stamper 对象后,您不再拥有表单,因此您的代码的第二部分将永远无法工作。

    3.您正在尝试嵌入 Standard Type 1 字体:

    这行没有意义:

    BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "ISO-8859-9", BaseFont.EMBEDDED);
    

    Times-Roman 是 Standard Type 1 字体(在过去,我们称之为 Base14 字体); iText 从不嵌入为 PDF 定义的 14 种 Standard Type 1 字体中的任何一种,因此参数 BaseFont.EMBEDDED 将被忽略。

    4.真正的错误:

    XFA Worker 是 iTextSharp 的闭源插件。它需要有效的许可证密钥。当您收到错误提示 "Signature was corrupted" 时,您使用的许可证密钥已损坏。

    可能的原因:

    • 您不是 iText Group 的客户。您正在使用在某处找到的密钥,并且更改了其中的一些内容。
    • 您是 iText Group 的客户。你收到了一把钥匙,但不知何故它被篡改了。过去,我们遇到过类似的问题,客户在读取密钥时就好像它是用 EBCDIC 编码的一样。请联系您在 iText Group 的客户经理了解更多信息。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多