PDFio is Copyright \[co] 2021 by Michael R Sweet and is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software. See the files "LICENSE" and "NOTICE" for more information.
.SSRequirements
.PP
PDFio requires the following to build the software:
.IP\(bu5
.PP
A C99 compiler such as Clang, GCC, or MS Visual C
.IP\(bu5
.PP
A POSIX\-compliant make program
.IP\(bu5
.PP
ZLIB (https://www.zlib.net) 1.0 or higher
.PP
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
.SSInstallingpdfio
.PP
PDFio comes with a portable makefile that will work on any POSIX\-compliant system with ZLIB installed. To make it, run:
.nf
make all
.fi
.PP
To test it, run:
.nf
make test
.fi
.PP
To install it, run:
.nf
make install
.fi
.PP
If you want a shared library, run:
.nf
make all\-shared
make install\-shared
.fi
.PP
The default installation location is "/usr/local". Pass the prefix variable to make to install it to another location:
.nf
make install prefix=/some/other/directory
.fi
.PP
The makefile installs the pdfio header to "${prefix}/include", the library to "${prefix}/lib", the pkg\-config file to "${prefix}/lib/pkgconfig", the man page to "${prefix}/share/man/man3", and the documentation to "${prefix}/share/doc/pdfio".
.PP
The makefile supports the following variables that can be specified in the make command or as environment variables:
.IP\(bu5
.PP
AR: the library archiver (default "ar")
.IP\(bu5
.PP
ARFLAGS: options for the library archiver (default "cr")
.IP\(bu5
.PP
CC: the C compiler (default "cc")
.IP\(bu5
.PP
CFLAGS: options for the C compiler (default "")
.IP\(bu5
.PP
CODESIGN_IDENTITY: the identity to use when code signing the shared library on macOS (default "Developer ID")
.IP\(bu5
.PP
COMMONFLAGS: options for the C compiler and linker (typically architecture and optimization options, default is "\-Os \-g")
.IP\(bu5
.PP
CPPFLAGS: options for the C preprocessor (default "")
There is also an Xcode project ("pdfio.xcodeproj") you can use on macOS which generates a static library that will be installed under "/usr/local" with:
.nf
sudo xcodebuild install
.fi
.PP
You can reproduce this with the makefile using:
.nf
sudo make 'COMMONFLAGS="\-Os \-mmacosx\-version\-min=10.14 \-arch x86_64 \-arch arm64"' install
.fi
.SSDetectingPDFio
.PP
PDFio can be detected using the pkg\-config command, for example:
.nf
if pkg\-config \-\-exists pdfio; then
...
fi
.fi
.PP
In a makefile you can add the necessary compiler and linker options with:
where the three arguments to the function are the filename ("myinputfile.pdf"), an optional error callback function (error_cb), and an optional pointer value for the error callback function (error_data). The error callback is called for both errors and warnings and accepts the pdfio_file_t pointer, a message string, and the callback pointer value, for example:
The default error callback (NULL) does the equivalent of the above.
.PP
Each PDF file contains one or more pages. The pdfioFileGetNumPages function returns the number of pages in the file while the pdfioFileGetPage function gets the specified page in the PDF file:
.nf
pdfio_file_t *pdf; // PDF file
size_t i; // Looping var
size_t count; // Number of pages
pdfio_obj_t *page; // Current page
// Iterate the pages in the PDF file
for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
{
page = pdfioFileGetPage(pdf, i);
// do something with page
}
.fi
.PP
Each page is represented by a "page tree" object (what pdfioFileGetPage returns) that specifies information about the page and one or more "content" objects that contain the images, fonts, text, and graphics that appear on the page.
.PP
The pdfioFileClose function closes a PDF file and frees all memory that was used for it:
.nf
pdfioFileClose(pdf);
.fi
.SSWritingPDFFiles
.PP
You create a new PDF file using the pdfioFileCreate function:
.nf
pdfio_rect_t media_box = { 0.0, 0.0, 612.0, 792.0 }; // US Letter
where the six arguments to the function are the filename ("myoutputfile.pdf"), PDF version ("2.0"), media box (media_box), crop box (crop_box), an optional error callback function (error_cb), and an optional pointer value for the error callback function (error_data). The units for the media and crop boxes are points (1/72nd of an inch).
Once the file is created, use the pdfioFileCreateObj, pdfioFileCreatePage, and pdfioPageCopy functions to create objects and pages in the file.
.PP
Finally, the pdfioFileClose function writes the PDF cross\-reference and "trailer" information, closes the file, and frees all memory that was used for it.
PDF objects are identified using two numbers \- the object number (1 to N) and the object generation (0 to 65535) that specifies a particular version of an object. An object's numbers are returned by the pdfioObjGetNumber and pdfioObjGetGeneration functions. You can find a numbered object using the pdfioFileFindObj function.
.PP
Objects contain values (typically dictionaries) and usually an associated data stream containing images, fonts, ICC profiles, and page content. PDFio provides several accessor functions to get the value(s) associated with an object:
.IP\(bu5
.PP
pdfioObjGetArray returns an object's array value, if any
.IP\(bu5
.PP
pdfioObjGetDict returns an object's dictionary value, if any
.IP\(bu5
.PP
pdfioObjGetLength returns the length of the data stream, if any
.IP\(bu5
.PP
pdfioObjGetSubtype returns the sub\-type name of the object, for example "Image" for an image object.
.IP\(bu5
.PP
pdfioObjGetType returns the type name of the object, for example "XObject" for an image object.
Some PDF objects have an associated data stream, such as for pages, images, ICC color profiles, and fonts. You access the stream for an existing object using the pdfioObjOpenStream function:
The first argument is the object pointer. The second argument is a boolean value that specifies whether you want to decode (typically decompress) the stream data or return it as\-is.
.PP
Once you have the stream open, you can use one of several functions to read from it:
.IP\(bu5
.PP
pdfioStreamConsume reads and discards a number of bytes in the stream
.IP\(bu5
.PP
pdfioStreamGetToken reads a PDF token from the stream
.IP\(bu5
.PP
pdfioStreamPeek peeks at the next stream data without advancing or "consuming" it
.IP\(bu5
.PP
pdfioStreamRead reads a buffer of data
.PP
When you are done reading from the stream, call the pdfioStreamClose function:
.nf
pdfioStreamClose(st);
.fi
.PP
To create a stream for a new object, call the pdfioObjCreateStream function:
The first argument is the newly created object. The second argument is either PDFIO_FILTER_NONE to specify that any encoding is done by your program or PDFIO_FILTER_FLATE to specify that PDFio should Flate compress the stream.
.PP
Once you have created the stream, use any of the following functions to write to the stream:
.IP\(bu5
.PP
pdfioStreamPrintf writes a formatted string to the stream
.IP\(bu5
.PP
pdfioStreamPutChar writes a single character to the stream
.IP\(bu5
.PP
pdfioStreamPuts writes a C string to the stream
.IP\(bu5
.PP
pdfioStreamWrite writes a buffer of data to the stream
.PP
The PDF content helper functions provide additional functions for writing specific PDF page stream commands.
.PP
When you are done writing the stream, call pdfioStreamCLose to close both the stream and the object.
PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into ??? categories:
.IP\(bu5
.PP
Color Space Functions
.IP\(bu5
.PP
Font Object Functions
.IP\(bu5
.PP
Image Object Functions
.IP\(bu5
.PP
Page Stream Functions
.IP\(bu5
.PP
Page Dictionary Functions
.PP
Color Space Functions
.PP
PDF color spaces are specified using well\-known names like "DeviceCMYK", "DeviceGray", and "DeviceRGB" or using arrays that define so\-called calibrated color spaces. PDFio provides several functions for embedding ICC profiles and creating color space arrays:
.IP\(bu5
.PP
pdfioArrayCreateColorFromICCObj creates a color array for an ICC color profile object
.IP\(bu5
.PP
pdfioArrayCreateColorFromMatrix creates a color array using a CIE XYZ color transform matrix, a gamma value, and a CIE XYZ white point
.IP\(bu5
.PP
pdfioArrayCreateColorFromPalette creates an indexed color array from an array of sRGB values
.IP\(bu5
.PP
pdfioArrayCreateColorFromPrimaries creates a color array using CIE XYZ primaries and a gamma value
PDF supports many kinds of fonts, including PostScript Type1, PDF Type3, TrueType/OpenType, and CID. PDFio provides two functions for creating font objects. The first is pdfioFileCreateFontObjFromBase which creates a font object for one of the base PDF fonts:
.IP\(bu5
.PP
"Courier"
.IP\(bu5
.PP
"Courier\-Bold"
.IP\(bu5
.PP
"Courier\-BoldItalic"
.IP\(bu5
.PP
"Courier\-Italic"
.IP\(bu5
.PP
"Helvetica"
.IP\(bu5
.PP
"Helvetica\-Bold"
.IP\(bu5
.PP
"Helvetica\-BoldOblique"
.IP\(bu5
.PP
"Helvetica\-Oblique"
.IP\(bu5
.PP
"Symbol"
.IP\(bu5
.PP
"Times\-Bold"
.IP\(bu5
.PP
"Times\-BoldItalic"
.IP\(bu5
.PP
"Times\-Italic"
.IP\(bu5
.PP
"Times\-Roman"
.IP\(bu5
.PP
"ZapfDingbats"
.PP
PDFio always uses the Windows CP1252 subset of Unicode for these fonts.
.PP
The second function is pdfioFileCreateFontObjFromFile which creates a font object from a TrueType/OpenType font file, for example:
will embed an OpenSans Regular TrueType font using the Windows CP1252 subset of Unicode. Pass true for the third argument to embed it as a Unicode CID font instead, for example:
PDF supports images with many different color spaces and bit depths with optional transparency. PDFio provides two helper functions for creating image objects that can be referenced in page streams. The first function is pdfioFileCreateImageObjFromData which creates an image object from data in memory, for example:
will create an object for a 1024x1024 RGBA image in memory, using the default color space for 3 colors ("DeviceRGB"). We can use one of the color space functions to use a specific color space for this image, for example:
The "interpolate" argument specifies whether the colors in the image should be smoothed/interpolated when scaling. This is most useful for photographs but should be false for screenshot and barcode images.
.PP
If you have a JPEG or PNG file, use the pdfioFileCreateImageObjFromFile function to copy the image into a PDF image object, for example:
PDF pages each have an associated dictionary to specify the images, fonts, and color spaces used by the page. PDFio provides functions to add these resources to the dictionary:
.IP\(bu5
.PP
pdfioPageDictAddColorSpace adds a named color space to the page dictionary
.IP\(bu5
.PP
pdfioPageDictAddFont adds a named font to the page dictionary
.IP\(bu5
.PP
pdfioPageDictAddImage adds a named image to the page dictionary
PDF page streams contain textual commands for drawing on the page. PDFio provides many functions for writing these commands with the correct format and escaping, as needed:
.IP\(bu5
.PP
pdfioContentClip clips future drawing to the current path
.IP\(bu5
.PP
pdfioContentDrawImage draws an image object
.IP\(bu5
.PP
pdfioContentFill fills the current path
.IP\(bu5
.PP
pdfioContentFillAndStroke fills and strokes the current path
.IP\(bu5
.PP
pdfioContentMatrixConcat concatenates a matrix with the current transform matrix
.IP\(bu5
.PP
pdfioContentMatrixRotate concatenates a rotation matrix with the current transform matrix
.IP\(bu5
.PP
pdfioContentMatrixScale concatenates a scaling matrix with the current transform matrix
.IP\(bu5
.PP
pdfioContentMatrixTranslate concatenates a translation matrix with the current transform matrix
.IP\(bu5
.PP
pdfioContentPathClose closes the current path
.IP\(bu5
.PP
pdfioContentPathCurve appends a Bezier curve to the current path
.IP\(bu5
.PP
pdfioContentPathCurve13 appends a Bezier curve with 2 control points to the current path
.IP\(bu5
.PP
pdfioContentPathCurve23 appends a Bezier curve with 2 control points to the current path
.IP\(bu5
.PP
pdfioContentPathLineTo appends a line to the current path
.IP\(bu5
.PP
pdfioContentPathMoveTo moves the current point in the current path
.IP\(bu5
.PP
pdfioContentPathRect appends a rectangle to the current path
.IP\(bu5
.PP
pdfioContentRestore restores a previous graphics state
.IP\(bu5
.PP
pdfioContentSave saves the current graphics state
.IP\(bu5
.PP
pdfioContentSetDashPattern sets the line dash pattern
.IP\(bu5
.PP
pdfioContentSetFillColorDeviceCMYK sets the current fill color using a device CMYK color
.IP\(bu5
.PP
pdfioContentSetFillColorDeviceGray sets the current fill color using a device gray color
.IP\(bu5
.PP
pdfioContentSetFillColorDeviceRGB sets the current fill color using a device RGB color
.IP\(bu5
.PP
pdfioContentSetFillColorGray sets the current fill color using a calibrated gray color
.IP\(bu5
.PP
pdfioContentSetFillColorRGB sets the current fill color using a calibrated RGB color
.IP\(bu5
.PP
pdfioContentSetFillColorSpace sets the current fill color space
.IP\(bu5
.PP
pdfioContentSetFlatness sets the flatness for curves
.IP\(bu5
.PP
pdfioContentSetLineCap sets how the ends of lines are stroked
.IP\(bu5
.PP
pdfioContentSetLineJoin sets how connections between lines are stroked
.IP\(bu5
.PP
pdfioContentSetLineWidth sets the width of stroked lines
.IP\(bu5
.PP
pdfioContentSetMiterLimit sets the miter limit for stroked lines
.IP\(bu5
.PP
pdfioContentSetStrokeColorDeviceCMYK sets the current stroke color using a device CMYK color
.IP\(bu5
.PP
pdfioContentSetStrokeColorDeviceGray sets the current stroke color using a device gray color
.IP\(bu5
.PP
pdfioContentSetStrokeColorDeviceRGB sets the current stroke color using a device RGB color
.IP\(bu5
.PP
pdfioContentSetStrokeColorGray sets the current stroke color using a calibrated gray color
.IP\(bu5
.PP
pdfioContentSetStrokeColorRGB sets the current stroke color using a calibrated RGB color
.IP\(bu5
.PP
pdfioContentSetStrokeColorSpace sets the current stroke color space
.IP\(bu5
.PP
pdfioContentSetTextCharacterSpacing sets the spacing between characters for text
.IP\(bu5
.PP
pdfioContentSetTextFont sets the font and size for text
.IP\(bu5
.PP
pdfioContentSetTextLeading sets the line height for text
.IP\(bu5
.PP
pdfioContentSetTextMatrix concatenates a matrix with the current text matrix
.IP\(bu5
.PP
pdfioContentSetTextRenderingMode sets the text rendering mode
.IP\(bu5
.PP
pdfioContentSetTextRise adjusts the baseline for text
.IP\(bu5
.PP
pdfioContentSetTextWordSpacing sets the spacing between words for text
.IP\(bu5
.PP
pdfioContentSetTextXScaling sets the horizontal scaling for text
.IP\(bu5
.PP
pdfioContentStroke strokes the current path
.IP\(bu5
.PP
pdfioContentTextBegin begins a block of text
.IP\(bu5
.PP
pdfioContentTextEnd ends a block of text
.IP\(bu5
.PP
pdfioContentTextMoveLine moves to the next line with an offset in a text block
.IP\(bu5
.PP
pdfioContentTextMoveTo moves within the current line in a text block
.IP\(bu5
.PP
pdfioContentTextNextLine moves to the beginning of the next line in a text block
.IP\(bu5
.PP
pdfioContentTextShow draws a literal string in a text block
.IP\(bu5
.PP
pdfioContentTextShowf draws a formatted string in a text block
.IP\(bu5
.PP
pdfioContentTextShowJustified draws an array of literal strings with offsets between them