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