Recently we posted about how to convert PDF to JPEG and converting multi-page TIFF to PDF. A customer recently asked how one might add multiple images to a PDF. Luckily, LEADTOOLS supports more than 150 raster, vector, and document file formats including SVG, JPEG, PNG and more. So let's dive in and show you how simple it is!
// Create file with first page
try {
BITMAPHANDLE page1 = { 0 };
L_INT return_code = L_LoadBitmap(page1_file, &page1, sizeof BITMAPHANDLE, 24, ORDER_BGR, NULL, NULL);
if(return_code < 1) throw(return_code);
page1.XResolution = page1.YResolution = BITMAPHEIGHT(&page1) / 11; //set the DPI to cause 11 inch height.
return_code = L_SaveBitmap(outputPdf_file, &page1, FILE_RAS_PDF_LZW, 24, 0, NULL);
if(return_code < 1) throw(return_code);
L_FreeBitmap(&page1);
}
catch (L_INT err) {
cout << "Error Code: " << err;
}
If you wish to append or replace pages using L_SaveBitmap(), the existing PDF file must be a raster-based PDF similar to the output of L_SaveBitmap() itself. L_SaveBitmap() also returns a success or error code to aid debugging.
Next, load an image and append it as a second page to the same PDF file:
// Append second page
try {
BITMAPHANDLE page2 = { 0 };
L_INT return_code = L_LoadBitmap(page2_file, &page2, sizeof BITMAPHANDLE, 24, ORDER_BGR, NULL, NULL);
if(return_code < 1) throw(return_code);
SAVEFILEOPTION SaveOptions = { 0 };
return_code = L_GetDefaultSaveFileOption(&SaveOptions, sizeof SAVEFILEOPTION);
if(return_code < 1) throw(return_code);
SaveOptions.PageNumber = 2;
page2.XResolution = page2.YResolution = BITMAPHEIGHT(&page2) / 11; //set the DPI to cause 11 inch height.
return_code = L_SaveBitmap(outputPdf_file, &page2, FILE_RAS_PDF_LZW, 24, 0, &SaveOptions);
if(return_code < 1) throw(return_code);
L_FreeBitmap(&page2);
}
catch (L_INT err) {
cout << "Error Code: " << err;
}
Finally, load two images, combine them into one image, and replace the first page with the image combined from the newly loaded 2 images:
BITMAPHANDLE page2_1 = { 0 }, page2_2 = { 0 };
// Load 2 images for one page
try {
L_INT return_code = L_LoadBitmap(page2_1_file, &page2_1, sizeof BITMAPHANDLE, 24, ORDER_BGR, NULL, NULL);
if(return_code < 1) throw(return_code);
return_code = L_LoadBitmap(page2_2_file, &page2_2, sizeof BITMAPHANDLE, 24, ORDER_BGR, NULL, NULL);
if(return_code < 1) throw(return_code);
L_UINT w = max(BITMAPWIDTH(&page2_1), BITMAPWIDTH(&page2_2));
L_UINT h = BITMAPHEIGHT(&page2_1) + BITMAPHEIGHT(&page2_2);
BITMAPHANDLE combinedPage = { 0 };
// Create empty bitmap
return_code = L_CreateBitmap(&combinedPage, sizeof BITMAPHANDLE, TYPE_CONV, w, h, 24, ORDER_BGR, NULL, BOTTOM_LEFT, NULL, 0);
if(return_code < 1) throw(return_code);
// Copy the first image into the empty bitmap
return_code = L_CombineBitmap(&combinedPage, 0, 0, BITMAPWIDTH(&page2_1), BITMAPHEIGHT(&page2_1), &page2_1, 0, 0, CB_DST_0 | CB_OP_ADD | CB_RAWCOMBINE, 0);
if(return_code < 1) throw(return_code);
L_FreeBitmap(&page2_1);
// Copy the second image below the first image
return_code = L_CombineBitmap(&combinedPage, 0, BITMAPHEIGHT(&page2_1), BITMAPWIDTH(&page2_2), BITMAPHEIGHT(&page2_2), &page2_2, 0, 0, CB_DST_0 | CB_OP_ADD | CB_RAWCOMBINE, 0);
if(return_code < 1) throw(return_code);
L_FreeBitmap(&page2_2);
SaveOptions.PageNumber = 1;
SaveOptions.Flags |= ESO_REPLACEPAGE; // add the replace flag to put the combined image instead of the old page1
combinedPage.XResolution = combinedPage.YResolution = BITMAPHEIGHT(&combinedPage) / 11; //set the DPI to cause 11 inch height.
return_code = L_SaveBitmap(outputPdf_file, &combinedPage, FILE_RAS_PDF_LZW, 24, 0, &SaveOptions);
if(return_code < 1) throw(return_code);
L_FreeBitmap(&combinedPage);
}
catch (L_INT err) {
cout << "Error Code: " << err;
}
For more information see the documentation for PDFFile class and PDFDocument class.
Try for Free
Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days, and comes with free chat and email support.
Any Questions?
Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at 704-332-5532.