我正在使用iTextSharp(版本5.4.5.0)进行一个示例项目来填充PDF模板中的acrobat字段。
填写acrobat字段后,我们无法通过点击pdf上的“保存”按钮将更新的PDF模板保存到本地路径。它给出了错误消息“文档无法保存。读取此文档时存在问题(26)”。你能否建议克服这个错误。
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();
}
答案 0 :(得分:1)
我发现了一个工作!另存为,对我不起作用。
所以,在您做出更改之后: