Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to generate a PDF file from template file and FDF file.
For Example,
PDF File = Template File + FDF File

The PDF file is generated successfully using template and fdf files and able to open it manually using Acrobat Pdf.

However, I am unable to open the generated pdf file using PdfReader object of iTextSharp.
For Example,
PdfReader obj = new PdfReader(outputFileName);

I get the following error on the above line:
Error: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Here is the method and client call to the method.

1) Am I using correct method to generate the PDF file? If not, what is the correct method?
2) Is there any known issue with the PdfReader?

Any help would greatly be appreciated.


C#
public static void GenerateFormWithData(string formFilename, string fdfFileName, string outputFileName)
        {
            File.Copy(formFilename, outputFileName, true);

            using (FileStream outputStream = new FileStream(outputFileName, FileMode.Open))
            {
                FdfReader fdfReader = new FdfReader(fdfFileName);
                PdfReader formReader = new PdfReader(fdfReader.FileSpec);
                using (PdfStamper pdfStamper = new PdfStamper(formReader, outputStream))
                {
                    AcroFields pdfForm = pdfStamper.AcroFields;

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

                    pdfStamper.Close();
                    outputStream.Close();
                }
                fdfReader.Close();
                formReader.Close();
            }
	PdfReader obj = new PdfReader(outputFileName); // ERROR on this line
            // Note: I am able to open the outputFileName when I double click on the file in Windows Explorer. But not able to read the file using PdfReader after generating it.

        }


            // Client Call
            string formFile = Path.Combine(defaultPath, "MyTemplate.pdf");
            string fdfFile = Path.Combine(defaultPath, "FormDataFile.fdf");
            string outFile = Path.Combine(defaultPath, "MyOutput.pdf");
            PdfLibrary.GenerateFormWithData(formFile, fdfFile, outFile);
Posted
Updated 6-Jul-17 3:21am

Hi Chirag B...

using statement itself calls a Dispose method for an Object used within it.

So.. Remove those Strike Through lines from your code and Try to Debug it.
C#
using (FileStream outputStream = new FileStream(outputFileName, FileMode.Open))
            {
                FdfReader fdfReader = new FdfReader(fdfFileName);
                PdfReader formReader = new PdfReader(fdfReader.FileSpec);
                using (PdfStamper pdfStamper = 
                               new PdfStamper(formReader, outputStream))
                {
                    AcroFields pdfForm = pdfStamper.AcroFields;
 
                    pdfForm.SetFields(fdfReader);
                    pdfStamper.FormFlattening = true;
                    pdfStamper.Writer.CloseStream = false;
 
                 pdfStamper.Close(); 
                    outputStream.Close();
                }
                fdfReader.Close();
                formReader.Close();
            }



Happy Programming...:-)
 
Share this answer
 
Is it possible to import annotation fdf?
 
Share this answer
 
Comments
Dave Kreskowiak 6-Jul-17 10:09am    
Post your own question with the proper details. Don't resurrect a THREE YEAR OLD question that has nothing to do with yours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900