0

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();
    }
    
user2745708
  • 1
  • 1
  • 1
  • 1
    Welcome to SO! Can you provide more information on the Save button? Such as, how exactly is it wired up to your code? – Derek Jan 03 '14 at 15:29
  • 1
    You need to use `ms.ToArray()` instead of `ms.GetBuffer()`, see this: http://stackoverflow.com/a/5119739/231316 – Chris Haas Jan 03 '14 at 15:51

1 Answers1

1

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.
Jzimme
  • 11
  • 1
  • Doesn't work with free version of adobe (Didn't expect it would but adding the note in case it used to work with the free version and/or someone with the free version finds this since it is the top thing on google when I looked for a solution to the error. The menus have changed a bit too). – Rodger Sep 23 '22 at 15:37