Examples / signature_ap / c

Signature Ap

A complete, runnable c program — 80 lines, shipped in your download.

Demonstrates Digital Signatures

Same example, other languages: activex_cpp cpp cpp_linux csharp python vbnet

examples/c/signature_ap/signature_ap.c 80 lines
/* signature_ap -- C port of examples\Vb6\signature_ap
   Builds a page with a digitally-signed signature field whose appearance
   template is drawn with normal PDF functions, then signs the file with a
   self-signed certificate. */
#include <stdio.h>
#include "lumaspdf.h"

#define RGB(r,g,b) ((UI32)(((UI8)(r))|((UI16)((UI8)(g))<<8)|((UI32)((UI8)(b))<<16)))

static SI32 PDF_CALL ErrProc(void* Data, SI32 ErrCode, const char* ErrMessage, SI32 ErrType) {
    (void)Data; (void)ErrCode; (void)ErrType;
    if (ErrMessage) printf("%s\n", ErrMessage);
    return 0;   /* try to continue if an error occurs */
}

int main(void) {
    PPDF pdf = pdfNewPDF();
    SI32 sigField, sh;
    const char* outFile = "out.pdf";
    const char* body =
        "This file is digitally signed with a self sign certificate. "
        "The appearance of the signature field is created with normal classic-API functions. However, it "
        "would also be possible to import a PDF page, an EMF file, or an image into the "
        "appearance template.\n\n"
        "When creating an individual signature appearance make sure to place the validation icon "
        "properly with PlaceSigFieldValidateIcon(). The appearance of the validation icon "
        "depends on the Acrobat version with which the file is opened. However, the unscaled size "
        "of that icon is always 100.0 x 100.0 Units. It can be scaled to every size you want "
        "but it is usually best to preserve the aspect ratio and the icon must be placed fully "
        "inside the appearance template.";

    pdfCreateNewPDFA(pdf, "");            /* The output file is opened later */
    pdfSetOnErrorProc(pdf, 0, ErrProc);

    pdfAppend(pdf);
    pdfSetFontA(pdf, "Arial", fsNone, 14.0, 1, cp1252);
    pdfWriteFTextA(pdf, taLeft, body);

    /* ---------------------- Signature field appearance ---------------------- */
    sigField = pdfCreateSigField(pdf, "Signature", -1, 200.0, 500.0, 200.0, 80.0);
    pdfSetFieldColor(pdf, sigField, fcBorderColor, csDeviceRGB, NO_COLOR);
    pdfPlaceSigFieldValidateIcon(pdf, sigField, 0.0, 15.0, 50.0, 50.0);
    pdfCreateSigFieldAP(pdf, sigField);

    pdfSaveGraphicState(pdf);
    pdfRectangle(pdf, 0.0, 0.0, 200.0, 80.0, fmNoFill);
    pdfClipPath(pdf, cmWinding, fmNoFill);
    sh = pdfCreateAxialShading(pdf, 0.0, 0.0, 200.0, 0.0, 0.5, RGB(120,120,220), RGB(255,255,255), 1, 1);
    pdfApplyShading(pdf, sh);
    pdfRestoreGraphicState(pdf);

    pdfSaveGraphicState(pdf);
    pdfEllipse(pdf, 50.5, 1.0, 148.5, 78.0, fmNoFill);
    pdfClipPath(pdf, cmWinding, fmNoFill);
    sh = pdfCreateAxialShading(pdf, 0.0, 0.0, 0.0, 78.0, 2.0, RGB(255,255,255), RGB(120,120,220), 1, 1);
    pdfApplyShading(pdf, sh);
    pdfRestoreGraphicState(pdf);

    pdfSetFontA(pdf, "Arial", fsBold | fsUnderlined, 11.0, 1, cp1252);
    pdfSetFillColor(pdf, RGB(120,120,220));
    pdfWriteFTextExA(pdf, 50.0, 60.0, 150.0, -1.0, taCenter, "Digitally signed by:");
    pdfSetFontA(pdf, "Arial", fsBold | fsItalic, 18.0, 1, cp1252);
    pdfSetFillColor(pdf, RGB(100,100,200));
    pdfWriteFTextExA(pdf, 50.0, 45.0, 150.0, -1.0, taCenter, "LumasPDF");

    pdfEndTemplate(pdf);                  /* Close the appearance template. */
    /* ------------------------------------------------------------------------ */

    pdfEndPage(pdf);

    if (pdfHaveOpenDoc(pdf) != 0) {
        if (pdfOpenOutputFileA(pdf, outFile) == 0) { pdfDeletePDF(pdf); return 1; }
    }
    if (pdfCloseAndSignFile(pdf, "test_cert.pfx", "123456", "Test", "") != 0)
        printf("PDF file \"%s\" successfully created!\n", outFile);

    pdfDeletePDF(pdf);
    return 0;
}

This file is in the SDK at examples/c/signature_ap/signature_ap.c. The build fails if this page and that file ever differ.