user2745708
user2745708

Reputation: 1

Unable to save pdf template after filling acrobat fields

I am doing one sample project using iTextSharp (version 5.4.5.0) to fill acrobat fields in PDF template.

  1. After filling acrobat fields, we are not able save the updated PDF template to local path by clicking "SAVE" button on pdf. It is giving error message "The document could not be saved. There was a problem reading this document (26)". Could you please suggest to overcome this error.

    public EmptyResult Index(FormCollection form)
    {
        PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
        using (MemoryStream ms = new MemoryStream())
        {
            reader.RemoveUsageRights();
            PdfStamper stamper = new PdfStamper(reader,ms);
           // Dim stamper As New PdfStamper(reader, New FileStream(sOutputFile, FileMode.Open), Chr(0), True)
            AcroFields fields = stamper.AcroFields;
            fields.SetField("1_Efternamn", "surya firstname");
            fields.SetField("1_Fornamn", "surya lastname");
            fields.SetField("pnummer", "1234567890");
            fields.SetField("2_anstallning_from", System.DateTime.Now.Date.ToString("yyyyMMdd"));
            fields.SetField("2_anstalld_tom", System.DateTime.Now.AddYears(2).Date.ToString("yyyyMMdd"));
            fields.SetField("2_chk_ff_anstalld", "true");
            fields.SetField("2_arbetsuppgift", "sample test ");
            //stamper.FormFlattening = true;2_arbetsuppgift
            stamper.Close();
            DownloadAsPDF(ms);
            reader.Close();
    
        }
        return null;
    
    }
    
    private void DownloadAsPDF(MemoryStream ms)
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment;filename=certificate.pdf");
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        Response.End();
        ms.Close();
    }
    

Upvotes: 0

Views: 4565

Answers (1)

Jzimme
Jzimme

Reputation: 11

I FOUND A WORK AROUND! Save As, wasn't working for me Either.

So, after you have made your changes:

  1. click on create PDF
  2. Select From Multiple Files.
  3. Window will pop up with current open files.
  4. Make sure only the document you want is on the list.
  5. Click Ok.
  6. Now it has [Binder 1] open.
  7. Click File > Save As
  8. Choose Location Desired
  9. Name document as Desired.
  10. Click Save.

Upvotes: 1

Related Questions