Create a PDF Signature Field using iText7 v8.0.3

42 Views Asked by At

I'm trying to create a PDF Signature Field on a blank PDF using iText7 version 8.0.3

It seems that the .CreateSignature methods aren't there anymore, so I'm trying to figure out how do to this.

Can anyone help me out?

Here's what I was trying:

public async Task<FileStreamResult> DownloadPropertyList(LDSC_DownloadParams downloadParams)
{
    Instance instance = await GetInstance(downloadParams.ImplementationId);

    // Create a new PDF document
    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = new PdfWriter(memoryStream);
    writer.SetCloseStream(false);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);

    // Create a table with a column for each property attribute
    Table table = new Table(UnitValue.CreatePercentArray(new float[] { 20, 20, 20, 10, 10, 20 }))
        .UseAllAvailableWidth();

    // Add headers to the table
    table.AddHeaderCell("Property Name");
    table.AddHeaderCell("Address");
    table.AddHeaderCell("City");
    table.AddHeaderCell("State");
    table.AddHeaderCell("Zip Code");
    table.AddHeaderCell("Contact");

    // Iterate over ScreeningProperties and add rows to the table
    foreach (var property in instance.ScreeningProperties)
    {
        table.AddCell(property.PropertyName ?? "");
        table.AddCell(property.Address ?? "");
        table.AddCell(property.City ?? "");
        table.AddCell(property.State?.ToString() ?? "");
        table.AddCell(property.ZipCode ?? "");
        table.AddCell($"{property.ContactPerson ?? ""}\n{property.PhoneNumber ?? ""}");
    }

    // Add the table to the document
    document.Add(table);

    // Add signature field
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
    PdfFormField signature = PdfFormField.CreateSignature(pdf, new Rectangle(100, 100, 200, 50));
    signature.SetFieldName("Signature");
    form.AddField(signature);

    // Close the document
    document.Close(); // This finalizes the document and writes it to the memory stream

    // Prepare the memory stream to be returned
    memoryStream.Position = 0;

    // Return the FileStreamResult
    // Adjust the file name as needed
    return new FileStreamResult(memoryStream, "application/pdf")
    {
        FileDownloadName = "PropertyList.pdf"
    };
}

The issue is that PdfFormField.CreateSignature no longer exists in 8.0.3 and I'm not sure how to do this.

1

There are 1 best solutions below

0
user1274820 On

Got it figured out - it can be generated like this:

public async Task<FileStreamResult> DownloadPropertyList(LDSC_DownloadParams downloadParams)
{
    // ...

    MemoryStream memoryStream = new MemoryStream();
    PdfWriter writer = new PdfWriter(memoryStream);
    writer.SetCloseStream(false);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);

    PdfPage page = pdf.AddNewPage();

    Table table = new Table(UnitValue.CreatePercentArray(new float[] { 20, 20, 20, 10, 10, 20 }))
        .UseAllAvailableWidth();

    Style headerStyle = new Style()
        .SetBold()
        .SetTextAlignment(TextAlignment.CENTER)
        .SetVerticalAlignment(VerticalAlignment.MIDDLE)
        .SetFontSize(10);

    Style cellStyle = new Style()
        .SetTextAlignment(TextAlignment.CENTER)
        .SetVerticalAlignment(VerticalAlignment.MIDDLE)
        .SetFontSize(7);

    String[] headers = { "Property Name", "Address", "City", "State", "Zip Code", "Contact" };
    foreach (var header in headers)
    {
        table.AddHeaderCell(new Cell().Add(new Paragraph(header)).AddStyle(headerStyle));
    }

    foreach (var property in instance.ScreeningProperties)
    {
        table.AddCell(new Cell().Add(new Paragraph(property.PropertyName ?? "")).AddStyle(cellStyle));
        table.AddCell(new Cell().Add(new Paragraph(property.Address ?? "")).AddStyle(cellStyle));
        table.AddCell(new Cell().Add(new Paragraph(property.City ?? "")).AddStyle(cellStyle));
        table.AddCell(new Cell().Add(new Paragraph(property.State?.ToString() ?? "")).AddStyle(cellStyle));
        table.AddCell(new Cell().Add(new Paragraph(property.ZipCode ?? "")).AddStyle(cellStyle));
        table.AddCell(new Cell().Add(new Paragraph($"{property.ContactPerson ?? ""}\n{property.PhoneNumber ?? ""}")).AddStyle(cellStyle));
    }

    document.Add(table);

    // Add signature field
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
    PdfWidgetAnnotation widget = new(new Rectangle(70, 20, 150, 25));
    PdfSignatureFormField signature = new PdfFormFactory().CreateSignatureFormField(widget, pdf);
    widget.SetPage(pdf.GetLastPage());
    widget.SetVisibility(4);
    signature.SetFieldName("Signature");
    form.AddField(signature);

    // Add a label for the signature
    Paragraph signatureLabel = new Paragraph("Signature:")
        .SetFixedPosition(pdf.GetNumberOfPages(), 22, 18, 150)
        .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
        .SetFontSize(10);
    document.Add(signatureLabel);

    // Draw a line for the signature
    PdfCanvas canvas = new PdfCanvas(pdf.GetLastPage());
    canvas
        .BeginText()
        .MoveText(70, 20)
        .SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 10)
        .EndText()
        .SetLineWidth(0.5f)
        .MoveTo(70, 20)
        .LineTo(220, 20)
        .Stroke();

    document.Close();

    memoryStream.Position = 0;

    return new FileStreamResult(memoryStream, "application/pdf")
    {
        FileDownloadName = "TestGeneration.pdf"
    };
}

Signature Screenshot