Question

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

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.

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.

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.

Was it helpful?

Solution

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();
  }

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.

OTHER TIPS

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.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top