【问题标题】:Free library to import FDF into PDF [closed]将 FDF 导入 PDF 的免费库 [关闭]
【发布时间】:2013-06-24 12:54:58
【问题描述】:

我正在尝试通过将 FDF 中的数据保存到 PDFTemplate 中的 WPFPDF 文件/strong> 应用程序。

所以,情况是这样的。我有一个 PDFTemplate.pdf 作为模板并具有占位符(或字段)。现在我以编程方式生成这个 FDF 文件,该文件又包含要填写的 PDFTemplate 所需的所有字段名称。此外,这个 FDF strong> 还包含 PDFTemaplte 的文件路径,以便在打开时知道要使用哪个 PDF

现在,当尝试双击 FDF 时,它会打开 Adober Acrobat Reader 并显示填有数据的 PDFTemplate . 但是我不能使用文件菜单保存这个文件,因为它说这个文件将在没有数据的情况下保存。

我想知道是否可以在不使用第三方组件的情况下将 FDF 数据导入 PDF 并保存。

另外,如果很难做到这一点,那么就能够做到这一点的免费图书馆而言,可能的解决方案是什么?

我刚刚意识到 iTextSharp 对于商业应用程序不是免费的。

【问题讨论】:

    标签: c# pdf fdf


    【解决方案1】:

    我已经能够使用另一个库 PDFSharp 来实现这一点。

    它有点类似于 iTextSharp 的工作方式,除了 iTextSharp 中更好且更易于使用的某些地方。我发布代码以防有人想做类似的事情:

    //Create a copy of the original PDF file from source 
    //to the destination location
    File.Copy(formLocation, outputFileNameAndPath, true);
    
    //Open the newly created PDF file
    using (var pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(
                        outputFileNameAndPath, 
                        PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify))
    {
       //Get the fields from the PDF into which the data 
       //is supposed to be inserted
        var pdfFields = pdfDoc.AcroForm.Fields;
    
        //To allow appearance of the fields
        if (pdfDoc.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
        {
            pdfDoc.AcroForm.Elements.Add(
                "/NeedAppearances", 
                new PdfSharp.Pdf.PdfBoolean(true));
        }
        else
        {
            pdfDoc.AcroForm.Elements["/NeedAppearances"] = 
                new PdfSharp.Pdf.PdfBoolean(true);
        }
    
        //To set the readonly flags for fields to their original values
        bool flag = false;
    
        //Iterate through the fields from PDF
        for (int i = 0; i < pdfFields.Count(); i++)
        {
            try
            {
                //Get the current PDF field
                var pdfField = pdfFields[i];
    
                flag = pdfField.ReadOnly;
    
                //Check if it is readonly and make it false
                if (pdfField.ReadOnly)
                {
                    pdfField.ReadOnly = false;
                }
    
                pdfField.Value = new PdfSharp.Pdf.PdfString(
                                 fdfDataDictionary.Where(
                                 p => p.Key == pdfField.Name)
                                 .FirstOrDefault().Value);
    
                //Set the Readonly flag back to the field
                pdfField.ReadOnly = flag;
            }
            catch (Exception ex)
            {
                throw new Exception(ERROR_FILE_WRITE_FAILURE + ex.Message);
            }
        }
    
        //Save the PDF to the output destination
        pdfDoc.Save(outputFileNameAndPath);                
        pdfDoc.Close();
    }
    

    【讨论】:

    • 想知道可能性,如果 fdf 文件包含 annot 而不是字段,这段代码是否可以工作?
    猜你喜欢
    • 2010-12-08
    • 2011-01-25
    • 2016-07-03
    • 1970-01-01
    • 2021-10-14
    • 2012-02-15
    • 1970-01-01
    • 2010-09-25
    • 2021-09-16
    相关资源
    最近更新 更多