當從頭創建一個新的PDF時,如何添加表單字段?

[英]iTextSharp — When creating a new PDF from scratch — how do I add form fields?


I am using iTextSharp, and have a new document being created in memory (I am combining multiple PDFs, then adding a new page with a digital signature on it.)

我使用了iTextSharp,並在內存中創建了一個新文檔(我正在合並多個pdf文件,然后添加一個帶有數字簽名的新頁面)。

I have a bit of a problem however. I have my Document object, and everything outputs, but how the heck do I add a PdfFormField to a Document? Do I have to use the stamper? This exists only in memory and is not saved anywhere.

但是我有一點問題。我有我的文檔對象和所有的輸出,但是我怎么才能在文檔中添加PdfFormField呢?我必須使用印章嗎?這只存在於內存中,並不保存在任何地方。

e.g:

例句:

Document document = new Document();
MemoryStream output = new MemoryStream();

try
{
    PdfWriter writer = PdfWriter.GetInstance(document, output);

    document.Open();
    PdfContentByte content = writer.DirectContent;

    // .... content adds a bunch of pages in
}
finally
{
    document.Close();
}

return File(output.GetBuffer(), "application/pdf",
            "MRF-" + receipt.OrderNumber + ".pdf");

I have a signature block as such that I want to add to the end of the document:

我有一個簽名塊,我想把它添加到文檔末尾:

 PdfFormField sig = PdfFormField.CreateSignature(writer);
 sig.SetWidget(new iTextSharp.text.Rectangle(100, 100, 250, 150), null);
 sig.Flags = PdfAnnotation.FLAGS_PRINT;
 sig.Put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
 sig.FieldName = "Signature1";

But I can't figure out for the life of me how to do something like document.add(sig) as it needs an IElement.

但是我不知道如何像document.add(sig)這樣做,因為它需要一個IElement。

1 个解决方案

#1


3  

Here's is the C#/ASP.NET version converted from Java, using an example from the iText book written by the creator of iText:

這里是c# / ASP。NET版本從Java轉換而來,使用了iText的創建者編寫的iText書中的一個示例:

Response.ContentType = "application/pdf";
Response.AddHeader(
  "Content-Disposition", "attachment; filename=signatureTest.pdf"
);        
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  document.Add(new Paragraph("A paragraph"));
  PdfFormField sig = PdfFormField.CreateSignature(writer);
  sig.SetWidget(new Rectangle(100, 100, 250, 150), null);
  sig.FieldName = "testSignature";
  sig.Flags = PdfAnnotation.FLAGS_PRINT;
  sig.SetPage();
  sig.MKBorderColor = BaseColor.BLACK;
  sig.MKBackgroundColor = BaseColor.WHITE;
  PdfAppearance appearance = PdfAppearance.CreateAppearance(writer, 72, 48);
  appearance.Rectangle(0.5f, 0.5f, 71.5f, 47.5f);
  appearance.Stroke();
  sig.SetAppearance(
    PdfAnnotation.APPEARANCE_NORMAL, appearance
  );
  writer.AddAnnotation(sig);
}

If you take a look at the Java example you'll notice there is also code to sign the document, which is purposely left out of the example above. Signing PDF in ASP.NET is not a trivial task.

如果您看一下Java示例,您會注意到還有一些代碼要對文檔進行簽名,在上面的示例中故意省略了這些代碼。簽名PDF在ASP。NET並不是一項微不足道的任務。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/03/02/72f11364756ad8415e7eb24e6381b04e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com