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.

I would like to know if it is possible to import the FDF data into PDF and save it without using a thrid party component.

Also, if it is very difficult to do this, what would be the possible solution in terms of a free library that would be able to do it?

I just realized that iTextSharp is not free for commercial applications.

Was it helpful?

Solution

I have been able to achieve this using another library PDFSharp.

It is somewhat similar to how iTextSharp works except for some places where in iTextSharp is better and easier to use. I am posting the code in case someone would want to do something similar:

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