Question

I have this example to fill a FDF programmatically and to visualize it.

private readonly string pdfFormFileName = "PDFForm.pdf";

protected void OpenPDF_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/vnd.fdf";

    FdfWriter fdfWriter = new FdfWriter();

    fdfWriter.File = GetAbsolutePath() + pdfFormFileName;

    fdfWriter.SetFieldAsName("txtFirstName", FirstName.Text);
    fdfWriter.SetFieldAsName("txtLastName", LastName.Text);

    Response.AddHeader("Content-disposition", "inline; filename=FlatPDFForm.fdf");        
    fdfWriter.WriteTo(Response.OutputStream);        
    Response.End();
}

Instead of displaying the file, I need to save it to a file.

Could you pls help me?

Was it helpful?

Solution

I found the solution!

string newFile = Server.MapPath("~/") + "ModF23_Final.pdf";
string pdfTemplate = Server.MapPath("~/") + "ModF23.pdf";

PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("2", FirstName.Text);
pdfFormFields.SetField("2-1", LastName.Text);
pdfStamper.FormFlattening = true;
pdfStamper.Close();

OTHER TIPS

Andrea could you try using this line of code

Response.ClearHeaders();
Response.ContentType = "application/vnd.fdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;Filename=FlatPDFForm.fdf");
Response.TransmitFile("FlatPDFForm.fdf");
Response.End();

Comment out this line fdfWriter.WriteTo(Response.OutputStream);

ADDED correct ContentType

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top