简体   繁体   English

使用iTextsharp将FDF写入PDF

[英]Write FDF to PDF using iTextsharp

I am trying to save a PDF file by saving the data from the FDF into a PDFTemplate , in my WPF application. 我试图通过将数据从FDF保存WPF应用程序中的PDFTemplate中来保存PDF文件。

So, the situation is like this. 所以,情况就是这样。 I have a PDFTemplate.pdf which serves as a template and has placeholders (or fields). 我有一个PDFTemplate.pdf ,它用作模板并具有占位符(或字段)。 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文件,该文件又包含要填充PDFTemplate所需的所有字段名称。此外,此FDF还包含PDFTemaplte的文件路径,因此在打开时,它知道要处理的PDF采用。

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. 现在,当尝试双击FDF时 ,它会打开Adober Acrobat Reader并显示填充了数据的PDFTemplate 。但是我无法使用“文件”菜单保存此文件,因为它说如果没有数据。

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. 因此,我决定使用iTextsharp ,读取PDFTemaplate文件,读取FDF文件并从那里获取数据,然后创建另一个PDF文件并将其与数据一起保存。

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. 请帮忙。

I was able to do it by not using the MemoryStream : 我能够通过不使用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();
  }

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. 我不确定,但是当我使用MemoryStream打开文件然后将其保存到另一个FileStream时 ,某些操作不起作用,不确定是什么。

But I tried to make it simpler and it worked. 但是我试图使其更简单并且有效。

Here is a similar example using memory stream instead of file stream. 这是使用内存流而不是文件流的类似示例。 I believe you only needed to set the memory stream position to 0. I also get the pdf template file name from the fdf FileSpec property which is helpful if you have several possible templates. 我相信您只需要将内存流位置设置为0。我还从fdf FileSpec属性获取pdf模板文件名,如果您有多个可能的模板,这将很有用。

using (Stream stream = new MemoryStream())
using (FdfReader fdfReader = new FdfReader(fdfFilePath))
using (PdfReader pdfReader = new PdfReader(fdfReader.FileSpec))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, stream)) {
    AcroFields pdfForm = pdfStamper.AcroFields;
    pdfForm.SetFields(fdfReader);
    pdfStamper.FormFlattening = true;
    pdfStamper.Writer.CloseStream = false;

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

    stream.Position = 0;
    //string contentType = "application/pdf";
    //SaveStreamToCloudStorage(contentType, stream, cloudStorageFileName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM