0

Creo un pdf en asp.net desde c#, el cual toma los datos de una formulario web que está en html y se convierte a pdf, y necesito agregar un campo de firma, pero no sé cómo hacer que me quede en cierto lugar el respectivo campo de firma, ya que no siempre puedo decir que vaya a quedar en el mismo lugar, eso depende de la información que tenga el formulario, podría quedar en la primer página o en la segunda o tercera, no sé si de pronto ocupe más páginas, por lo cual no puedo agregar el campo de firma con una dirección. Este es el código que tengo para la implementación del campo de firma.

PdfFormField field = PdfFormField.CreateSignature(writer);
        field.FieldName = "SIGNAME";
        // set the widget properties
        field.SetPage();            
        field.SetWidget(new iTextSharp.text.Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_PUSH);                        
        field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
        // add it as an annotation
        writer.AddAnnotation(field);
        // maybe you want to define an appearance
        PdfAppearance tp = PdfAppearance.CreateAppearance(writer, 72, 48);
        tp.SetColorStroke(BaseColor.BLUE);
        tp.SetColorFill(BaseColor.LIGHT_GRAY);
        tp.Rectangle(0.5f, 0.5f, 71.5f, 47.5f);
        tp.FillStroke();
        tp.SetColorFill(BaseColor.BLUE);
        ColumnText.ShowTextAligned(tp, Element.ALIGN_CENTER, new Phrase("Firme Aquí"), 36, 24, 25);
        field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);

El campo se me agrega en esta ubicación de la última página, pero en esta ubicación ((72, 732, 144, 780)), desearía que se agregara después de algún elemento del formulario html, con eso obviar estos valores (72, 732, 144, 780).

Agradezco toda ayuda que me puedan brindar.

1 respuesta 1

0

Buena tarde comparto la solución para otros programadores a futuro, les puede servir, el ejemplo esta para agregar 3 campos de firmas, igual solo necesitaba uno. Agrego estos dos métodos en la misma clase que estoy trabajando.

 public void CreateForm(Document document, PdfWriter writer)
{

    Document document = new Document();
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FORM, FileMode.Create));
    //document.Open();
    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 30;
    table.AddCell("Firma aprobación Supervisor");
    table.AddCell(CreateSignatureFieldCell(writer, "sig1"));
    table.AddCell("Signer 2: Bob");
    table.AddCell(CreateSignatureFieldCell(writer, "sig2"));
    table.AddCell("Signer 3: Carol");
    table.AddCell(CreateSignatureFieldCell(writer, "sig3"));
    document.Add(table);
   // document.Close();
}

protected PdfPCell CreateSignatureFieldCell(PdfWriter writer, String name)
{
    PdfPCell cell = new PdfPCell();
    cell.MinimumHeight = 50;
    PdfFormField field = PdfFormField.CreateSignature(writer);
    field.FieldName = name;
    field.Flags = PdfAnnotation.FLAGS_PRINT;
    cell.CellEvent = new MySignatureFieldEvent(field);
    return cell;
}

Y agrego esto en la misma clase, como clase aparte.

public class MySignatureFieldEvent : IPdfPCellEvent

{ public PdfFormField field;

public MySignatureFieldEvent(PdfFormField field)
{
    this.field = field;
}

public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases)
{
    PdfWriter writer = canvases[0].PdfWriter;
    field.SetPage();
    field.SetWidget(position, PdfAnnotation.HIGHLIGHT_INVERT);
    writer.AddAnnotation(field);
}}

Ya con eso solucione el campo de firma que necesitaba agregar.

Dejo el enlace que me sirvió de base para adaptarlo a mi requerimiento. Enlace de ayuda, ejercicio 10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.