Examples / collections2 / c

Collections2

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

Demonstrates Core SDK

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

examples/c/collections2/collections2.c 77 lines
/* ============================================================================
 *  collections2 -- C (x64/MSVC) port of examples\Vb6\collections2\collections2.bas
 *  Like collections, but adds sortable collection fields and per-item field
 *  values, then validates the collection.
 * ========================================================================= */
#include <stdio.h>
#include <string.h>
#include "lumaspdf.h"

static void exedir(const char* a0, char* out, size_t n) {
    char* s;
    strncpy(out, a0, n - 1); out[n - 1] = 0;
    s = strrchr(out, '\\'); if (!s) s = strrchr(out, '/');
    if (s) *s = 0; else strcpy(out, ".");
}

static SI32 PDF_CALL ErrProc(void* Data, SI32 ErrCode, const char* ErrMessage, SI32 ErrType) {
    (void)Data; (void)ErrCode; (void)ErrType;
    printf("%s\n", ErrMessage);
    return 0;
}

int main(int argc, char** argv) {
    SI32 ef;
    PPDF pdf;
    char dir[1024], outFile[1100];

    pdf = pdfNewPDF();
    pdfCreateNewPDFA(pdf, "");
    pdfSetOnErrorProc(pdf, 0, ErrProc);

    pdfSetImportFlags(pdf, ifImportAll | ifImportAsPage);
    if (pdfOpenImportFileA(pdf, "../../test_files/collection_en.pdf", ptOpen, "") < 0) {
        pdfDeletePDF(pdf);
        printf("Input file \"../../test_files/collection_en.pdf\" not found!\n");
        return 0;
    }
    pdfImportPDFFile(pdf, 1, 1.0, 1.0);
    pdfCloseImportFile(pdf);
    pdfCreateCollection(pdf, civTile);

    /* User defined field Index so the list can be sorted in any order. */
    ef = pdfCreateCollectionFieldA(pdf, cisCustomNumber, 0, "File index", "Index", 0, 1);
    pdfSetColSortField(pdf, ef, 1);

    pdfCreateCollectionFieldA(pdf, cisFileName, 1, "File name", "", 1, 1);
    pdfCreateCollectionFieldA(pdf, cisSize, 2, "File size", "", 1, 0);
    pdfCreateCollectionFieldA(pdf, cisModDate, 3, "Modification date", "", 1, 0);

    ef = pdfAttachFileA(pdf, "../../test_files/taxform.pdf", "A PDF file...", 1);
    pdfSetColDefFile(pdf, ef);
    pdfCreateColItemNumber(pdf, ef, "Index", 0.0, "");

    ef = pdfAttachFileA(pdf, "../../test_files/fulltest.emf", "An EMF file...", 1);
    pdfCreateColItemNumber(pdf, ef, "Index", 1.0, "");

    ef = pdfAttachFileA(pdf, "../../test_files/sample.txt", "A text file...", 1);
    pdfCreateColItemNumber(pdf, ef, "Index", 2.0, "");

    pdfCheckCollection(pdf);

    outFile[0] = 0;
    if (pdfHaveOpenDoc(pdf) != 0) {
        exedir(argv[0], dir, sizeof(dir));
        sprintf(outFile, "%s\\out.pdf", dir);
        if (pdfOpenOutputFileA(pdf, outFile) == 0) {
            pdfDeletePDF(pdf);
            return 0;
        }
    }
    pdfCloseFile(pdf);
    printf("PDF Collection \"%s\" successfully created!\n", outFile);
    pdfDeletePDF(pdf);
    (void)argc;
    return 0;
}

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