没有看到添加到PDF的空白签名字段

时间:2016-12-20 13:06:22

标签: itext

我将TextFields替换为Signature字段,但无法在生成的PDF中看到这些空白签名字段。 客户需要在以后阶段对这些空白字段进行数字签名。

    /// <summary>
    /// Adds a text field to the report that the user can update signatures to the specified location.
    /// </summary>
    /// <param name="writer">pdfwriter of document.</param>
    /// <param name="xPosition">The lower left x position of the text field.</param>
    /// <param name="yPosition">The lower left y position of the text field.</param>
    /// <param name="width">The width of the text field.</param>
    /// <param name="height">The height of the text field.</param>
    /// <param name="fieldId"></param>   
    protected virtual void AddTextField(iTextSharp.text.pdf.PdfWriter writer, String fieldId, float xPosition, float yPosition, float width, float height)
    {
        Rectangle position = new Rectangle(xPosition, yPosition, xPosition + width, Math.Max(yPosition - height, 0));
        iTextSharp.text.pdf.TextField field = new iTextSharp.text.pdf.TextField(writer, position, fieldId);

        // Requirement is to change existing textFields to blank Signature fields
        // In the method, memoryStream, reader and stamper are not available.

        // Below is the textField I need to transform into Signature field.
        // field.Text = String.Empty;
        // field.Font = FontFactory.GetFont("Arial Narrow").BaseFont;
        // field.TextColor = Color.WHITE;
        // field.FontSize = 9;

        // Not seeing the signature field appear on the pdf.
        // Don't know exactly the reason
        PdfFormField sig = PdfFormField.CreateSignature(writer);
        sig.SetWidget(position, null);
        sig.Flags = PdfAnnotation.FLAGS_PRINT;
        sig.Put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
        sig.FieldName = fieldId;
        sig.SetPage();

        writer.AddAnnotation(sig);

//Also tried below code
        //PdfFormField pfield = PdfFormField.CreateSignature(writer);
        //pfield.FieldName = fieldId;
        //pfield.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
        //pfield.SetWidget(position, null);
        //pfield.SetPage();
        //pfield.MKBorderColor = Color.BLACK;
        //pfield.MKBackgroundColor = Color.BLUE;
        //PdfAppearance tp = PdfAppearance.CreateAppearance(writer, width, height);
        //tp.Rectangle(position);
        //tp.Stroke();
        //pfield.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);

        //writer.AddAnnotation(pfield);


    }

第二个任务: *我是否真的需要一个签名字段 - 如果 - 客户端将在以后阶段向这些字段添加签名文本(使用像DocuSign这样的第三方工具)?或简单的PDFTextFields真的有效吗?

在查询中添加上述问题,因为这可能有助于我的情景。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我刚刚使用

测试了您的代码
public void AddSignatureFieldLikePrashantJha()
{
    Document document = new Document();
    Stream stream = new FileStream(@"emptySignatureFieldLikePrashantJha.pdf", FileMode.Create);
    PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
    document.Open();
    AddTextField(pdfWriter, "Signature", 100, 100, 400, 50);
    document.Close();
}

在结果中,Adobe Reader DC通过箭头清楚地指示了签名字段位置:

Screenshot Adobe Reader DC

将焦点放在该字段上(例如,通过按TAB键),字段甚至会被赋予一个帧:

Screenshot Adobe Reader DC with focus

早期的Adobe Reader版本显示它更清晰,例如9.5:

Screenshot Adobe Reader 9.5

因此,我无法重现你是

  

无法在生成的PDF中看到这些空白签名字段

如果您希望字段区域显得更加明显,您确实可以使用PdfAppearance,例如

protected virtual void AddFancySignatureField(iTextSharp.text.pdf.PdfWriter writer, String fieldId, float xPosition, float yPosition, float width, float height)
{
    Rectangle position = new Rectangle(xPosition, yPosition, xPosition + width, Math.Max(yPosition - height, 0));
    iTextSharp.text.pdf.TextField field = new iTextSharp.text.pdf.TextField(writer, position, fieldId);

    PdfFormField sig = PdfFormField.CreateSignature(writer);
    sig.SetWidget(position, null);
    sig.Flags = PdfAnnotation.FLAGS_PRINT;
    sig.Put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
    sig.FieldName = fieldId;
    sig.SetPage();

    PdfAppearance tp = PdfAppearance.CreateAppearance(writer, width, height);
    PdfShading radial = PdfShading.SimpleRadial(writer, 0, height / 2, 0, 0, height / 2, width, BaseColor.RED, BaseColor.GREEN);
    tp.PaintShading(radial);
    sig.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);

    writer.AddAnnotation(sig);
}

结果如下:

Screenshot Adobe Reader DC, fancy

  

第二个任务:*我是否真的需要一个签名字段 - 如果 - 客户端将在以后阶段向这些字段添加签名文本(使用像DocuSign这样的第三方工具)?或简单的PDFTextFields真的有效吗?

这在很大程度上取决于使用的确切第三方工具。一些这样的工具需要签名字段,一些需要内容中的文本标记,一些需要坐标给出的位置......