
LibHaru
Free C++ Library to Create Encrypted PDF Files
A Powerful Open Source C++ PDF Library that Enables Software Developers to Generate, Read, Manipulate & Convert PDF Files, Draw Shapes and Add Images to It.
What is LibHaru?
In the world of software development, the ability to generate documents programmatically is a crucial requirement. From generating invoices and reports to creating complex forms, Portable Document Format (PDF) remains the gold standard for consistent, portable, and reliable document sharing. If you're a C or C++ developer looking for a lightweight, powerful, and free library to create PDFs from scratch, Libharu is a tool you need to know.
LibHaru is an open source C++ library that enables software developers to generate PDF file format, currently, the API doesn't allow editing existing PDF documents. Using the API you can generate PDF file - add text, lines, and annotations int it. Furthermore, you can also add images of PNG and JPEG format in the document. LibHaru also allows compressing the PDF document with the deflate-decode format and generates encrypted PDF documents. LibHaru is written ANSI-C and can work both as static-library and shared-library. To use it with a C++ program you can compile it with any C++ compiler and use it as a static-library.
Getting Started with LibHaru
LibHaru is written in ANSI-C and to use it with C++, you can compile it with any compliant C++ compiler. First of all, you can download and extract the latest version of the API. There are several kinds of makefile, for every compiler, in the script directory. Build the library with an appropriate makefile.
There are several kinds of makefile, for every compiler, in the script directory. Build the library with an appropriate makefile.
Build the Library for Compiler
//Microsoft VC++ Compiler
NMAKE -f script/Makefile.msvc
//Borland C++ Compiler
make -f script/Makefile.BCC
C++ Library to Generate PDF File Format
LibHaru has provided a set of features that enables software developers to generate PDF file format. Using the API you can create a new PDF document, set document object attributes, create a new page, set page object, set page description and save the document to a file ora memory stream.
Embed Images in PDF using C++ API
LibHaru enables software developers to embed JPEG and PNG images in PDF documents. Using the API you can get image size, width, height, bits per component and color space. Furthermore, you can set a color mask and mask image for the embedded image. Here is a simple example that allows software developers to create a single-page PDF with a title and a "Hello World" message.
How to Create a Simple PDF File with "Hello Word" Messages via C++ API?
#include
#include
#include
// Error handler function
void error_handler (HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data) {
printf ("ERROR: error_no=%04X, detail_no=%d\n", (HPDF_UINT)error_no, (HPDF_INT)detail_no);
exit(1);
}
int main (int argc, char **argv) {
HPDF_Doc pdf;
HPDF_Page page;
HPDF_Font font;
char fname[256];
// Step 1: Create a PDF document object
pdf = HPDF_New(error_handler, NULL);
if (!pdf) {
printf("Error: cannot create PdfDoc object\n");
return 1;
}
// Step 2: Set document metadata (Optional but good practice)
HPDF_SetInfoAttr(pdf, HPDF_INFO_CREATOR, "My Libharu Application");
// Step 3: Add a new page to the document
page = HPDF_AddPage(pdf);
// Step 4: Set the page size and drawing mode
HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
// Step 5: Get a font and set its size
font = HPDF_GetFont(pdf, "Helvetica", NULL);
HPDF_Page_SetFontAndSize(page, font, 24);
// Step 6: Begin writing text to the page
HPDF_Page_BeginText(page);
// Step 7: Move the text cursor to a position (x, y from bottom-left)
HPDF_Page_MoveTextPos(page, 50, HPDF_Page_GetHeight(page) - 70);
// Step 8: Show the text
HPDF_Page_ShowText(page, "My First PDF with Libharu");
// Step 9: Move down and show another text in a smaller font
HPDF_Page_SetFontAndSize(page, font, 18);
HPDF_Page_MoveTextPos(page, 0, -30); // Move down 30 units
HPDF_Page_ShowText(page, "Hello, World!");
// Step 10: End the text object
HPDF_Page_EndText(page);
// Step 11: Save the document to a file
strcpy(fname, "hello_world.pdf");
HPDF_SaveToFile(pdf, fname);
// Step 12: Clean up
HPDF_Free(pdf);
printf("PDF successfully created: %s\n", fname);
return 0;
}
Create Encrypted PDF Files using C++ API
Encryption is a very useful mechanism that allows of encoding information into secret code that hides the information's true meaning. The Open source library LibHaru enables software developers to create encrypted PDF files without any external dependencies.
How to Create Encrypted PDF Files via C++?
const static char* text = "This is an encrypt document example.";
const static char* owner_passwd = "owner";
const static char* user_passwd = "user";
jmp_buf env;
#ifdef HPDF_DLL
void __stdcall
#else
void
#endif
error_handler (HPDF_STATUS error_no,
HPDF_STATUS detail_no,
void *user_data)
{
printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
(HPDF_UINT)detail_no);
longjmp(env, 1);
}
int
main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[256];
HPDF_REAL tw;
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
pdf = HPDF_New (error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
/* create default-font */
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* add a new page object. */
page = HPDF_AddPage (pdf);
HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE);
HPDF_Page_BeginText (page);
HPDF_Page_SetFontAndSize (page, font, 20);
tw = HPDF_Page_TextWidth (page, text);
HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth (page) - tw) / 2,
(HPDF_Page_GetHeight (page) - 20) / 2);
HPDF_Page_ShowText (page, text);
HPDF_Page_EndText (page);
HPDF_SetPassword (pdf, owner_passwd, user_passwd);
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
Draw Shapes or Add Images to PDF via C++
Libharu is an excellent choice for C/C++ developers who need a no-nonsense, performant library for generating PDFs with precise control. The library allows software developers to draw shapes or add images to PDF files inside C++ apps. Shapes are created by defining a path (Rectangle, MoveTo/LineTo) and then either stroking (drawing the outline) or filling it. Here is a useful example that allows to create a more complex PDF with graphics inside C++ apps.
How to Draw Shapes or Add an Image to PDF via C++ API?
#include
#include
#include
// ... (error_handler function from previous example)
int main() {
HPDF_Doc pdf;
HPDF_Page page;
HPDF_Image image;
pdf = HPDF_New(error_handler, NULL);
page = HPDF_AddPage(pdf);
HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
// --- DRAWING A RECTANGLE ---
// Set color for stroke (the border) and fill
HPDF_Page_SetRGBStroke(page, 0.2, 0.4, 0.8); // Blue border
HPDF_Page_SetRGBFill(page, 0.8, 0.9, 1.0); // Light blue fill
// Draw a rectangle (x, y, width, height) and fill it
HPDF_Page_Rectangle(page, 100, 600, 200, 100);
HPDF_Page_FillStroke(page); // Fills and strokes the path
// --- DRAWING A LINE ---
HPDF_Page_SetRGBStroke(page, 1.0, 0.0, 0.0); // Red line
HPDF_Page_SetLineWidth(page, 3); // Set line thickness
HPDF_Page_MoveTo(page, 100, 500);
HPDF_Page_LineTo(page, 300, 500);
HPDF_Page_Stroke(page); // Draws the line
// --- ADDING AN IMAGE ---
// Load a JPEG image (ensure 'sample.jpg' exists)
image = HPDF_LoadJpegImageFromFile(pdf, "sample.jpg");
// Draw the image at a specified location and size
HPDF_Page_DrawImage(page, image, 150, 300, HPDF_Image_GetWidth(image) / 2, HPDF_Image_GetHeight(image) / 2);
// Save the document
HPDF_SaveToFile(pdf, "graphics_demo.pdf");
HPDF_Free(pdf);
printf("PDF with graphics created: graphics_demo.pdf\n");
return 0;
}
