使用iTextsharp将FDF写入PDF [英] Write FDF to PDF using iTextsharp

查看:122
本文介绍了使用iTextsharp将FDF写入PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将 FDF 中的数据保存到 PDFTemplate 中来保存 PDF 文件,在我的 WPF 申请。

I am trying to save a PDF file by saving the data from the FDF into a PDFTemplate, in my WPF application.

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

So, the situation is like this. I have a PDFTemplate.pdf which serves as a template and has placeholders (or fields). Now I generate this FDF file pro-grammatically, which in turn contain all the field names required for the PDFTemplate to be filled in. Also, this FDF contains the file path for the PDFTemaplte also, so that on opening, it knows which PDF to use.

现在,当尝试并双击 FDF 时,它会打开 Adob​​er Acrobat Reader 并显示 PDFTemplate 并填入数据。但我无法使用文件菜单保存此文件,因为它表示此文件将在没有数据的情况下保存。

Now, when try and double click on the FDF, it open the Adober Acrobat Reader and displays the PDFTemplate with the data filled in. But I can't save this file using the File menu, as it says this file will be saved without the data.

那么,我决定使用 iTextsharp ,阅读 PDFTemaplate 文件,阅读 FDF 文件并从中获取数据,然后创建另一个 PDF 文件并保存它与数据一起。

So, then I decide to use iTextsharp, read the PDFTemaplate file, read the FDF file and get the data from there, and create another PDF file and save it along with the data.

以下是我正在使用的代码,但是当我打开新保存的文件时,它说该文件已损坏且无法修复:

The following is the code that I am using but when I open the newly saved file, it says that the file is damaged and could not be repaired:

    using (MemoryStream pdfFlat = new MemoryStream())
    using (PdfReader pdfReader = new PdfReader(templateLocation))
    using(PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfFlat))
    using(FdfReader fdfReader = new FdfReader(fdfFileNameAndPath))
    {
        AcroFields pdfForm = pdfStamper.AcroFields;
        pdfForm.SetFields(fdfReader);
        pdfStamper.FormFlattening = true;
        pdfStamper.Writer.CloseStream = false;

        using (FileStream saveStream = 
            new FileStream(
                outputFileNameAndPath, 
                FileMode.Create, 
                FileAccess.Write))
        {
            pdfFlat.WriteTo(saveStream);
            pdfFlat.Flush();
            saveStream.Close();
        }

        fdfReader.Close();
        pdfStamper.Close();
        pdfReader.Close();
        pdfFlat.Close();
    }

我不确定我做错了什么。请帮助。

I am not sure as to what am I doing wrong. Please help.

推荐答案

我能够通过不使用 MemoryStream <来实现/ strong>:

I was able to do it by not using the MemoryStream:

  File.Copy(formLocation, outputFileNameAndPath, true);

  using (FileStream pdfFlat = new FileStream(outputFileNameAndPath,FileMode.Open))
  using (PdfReader pdfReader = new PdfReader(formLocation))
  using (PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfFlat))
  using (FdfReader fdfReader = new FdfReader(fdfFileNameAndPath))
  {
      AcroFields pdfForm = pdfStamper.AcroFields;

      pdfForm.SetFields(fdfReader);
      pdfStamper.FormFlattening = true;
      pdfStamper.Writer.CloseStream = false;

      fdfReader.Close();
      pdfStamper.Close();
      pdfReader.Close();
      pdfFlat.Close();
  }

我不确定但是当我使用 时MemoryStream 打开文件,然后将其保存到另一个 FileStream ,有些东西无法正常工作,不确定是什么。

I am not sure but when I was using the MemoryStream to open the file and then save it into another FileStream, something was not working, not sure what.

但我试图让它变得更简单并且有效。

But I tried to make it simpler and it worked.

这篇关于使用iTextsharp将FDF写入PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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