首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >免费库将FDF导入PDF中

免费库将FDF导入PDF中
EN

Stack Overflow用户
提问于 2013-06-24 12:54:58
回答 1查看 2.5K关注 0票数 1

我试图保存一个PDF文件,方法是将数据从FDF保存到一个PDFTemplate中,在我的WPF应用程序中。

所以情况是这样的。我有一个PDFTemplate.pdf,它用作模板,并有占位符(或字段)。现在,我以亲语法的方式生成这个FDF文件,它反过来包含要填充PDFTemplate所需的所有字段名。此外,这个FDF还包含PDFTemaplte的文件路径,以便在打开时知道要使用哪个PDF

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

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

而且,如果很难做到这一点,那么对于能够做到这一点的免费库来说,可能的解决方案是什么呢?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-26 08:23:35

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

它有点类似于iTextSharp的工作方式,除了在iTextSharp中更好和更易于使用的一些地方。如果有人想做类似的事情,我会发布代码:

代码语言:javascript
复制
//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();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17276026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档