既存の pdf を編集し、結果をブラウザに送信したい ここに私の方法があります:
public static byte[] Generate()
{
var templatePath = HttpContext.Current.Server.MapPath("~/my_template.pdf");
// Based on:
// http://www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs-with-c-and-itextsharp/
var reader = new PdfReader(templatePath);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;
foreach (string fieldKey in fieldKeys)
{
if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldA")
form.SetField(fieldKey, "1234");
if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldB")
form.SetField(fieldKey, "5678");
}
// "Flatten" the form so it wont be editable/usable anymore
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
return outStream.ToArray();
}
メソッド呼び出し
byte[] buffer = Generate();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
この場合、誰かが私を助けることができますか?