【问题标题】:Not seeing blank signature field added to PDF没有看到添加到 PDF 的空白签名字段
【发布时间】:2016-12-20 13:06:21
【问题描述】:

我将 TextFields 替换为签名字段,但无法在生成的 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 之类的第 3 方工具),我真的需要一个签名字段吗?还是简单的 PDFTextFields 真的有用?

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

提前感谢您的帮助。

【问题讨论】:

    标签: itext


    【解决方案1】:

    我刚刚使用

    测试了您的代码
    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 用箭头清楚地标明了签名字段的位置:

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

    早期的 Adob​​e 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);
    }
    

    结果如下:


    第二个任务:*我真的需要签名字段,如果 - 客户将在稍后阶段将签名文本添加到这些字段(使用像 DocuSign 这样的第 3 方工具)?还是简单的 PDFTextFields 真的有用?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多