最近有个分割pdf的需求,现成的软件比如迅捷pdf编辑器、2345pdf等都是收费的,忍不了,大概在网络上扫了一圈,觉得pdflib这个库很不错,虽然收费,但有crack版本,用完直观感受:速度刚刚的。另外,它的license机制一直在升级,以前很多的license都失效了。
#include <iostream>
#include "pdflib.hpp"
using namespace std;
using namespace pdflib;
int
main(void)
{
try {
const wstring searchpath = L"[this is your pdf directory]";
PDFlib p;
const wstring pdffiles[] =
{
L"xx.pdf",
};
const int filecount = sizeof(pdffiles) / sizeof(pdffiles[0]);
int i;
const std::wstring out_file = L"xx_splitted.pdf";
const std::wstring out_title = L"you name it";
wostringstream optlist;
// This means we must check return values of load_font() etc.
p.set_option(L"errorpolicy=return");
// Set the search path for fonts and PDF files
optlist.str(L"");
optlist << L"searchpath={{" << searchpath << L"}}";
p.set_option(optlist.str());
if (p.begin_document(out_file.c_str(), L"") == -1) {
wcerr << L"Error: " << p.get_errmsg() << endl;
return 2;
}
p.set_info(L"Creator", L"PDFlib starter sample");
p.set_info(L"Title", out_title.c_str());
for (i = 0; i < filecount; i++) {
int indoc, endpage, pageno, page;
const int startpage = 150; // start page
endpage = 296; // end page;
/* Open the input PDF */
indoc = p.open_pdi_document(pdffiles[i], L"");
if (indoc == -1) {
wcerr << L"Error: " << p.get_errmsg() << endl;
return 2;
}
/* Loop over all pages of the input document */
for (pageno = startpage; pageno <= endpage; ++pageno)
{
page = p.open_pdi_page(indoc, pageno, L"");
if (page == -1) {
wcerr << L"Error: " << p.get_errmsg() << endl;
continue;
}
/* Dummy page size; will be adjusted later */
p.begin_page_ext(10, 10, L"");
/* Create a bookmark with the file name at the beginning*/
// if (pageno == 1)
// p.create_bookmark(pdffiles[i], L"");
/* Place the imported page on the output page, and
* adjust the page size
*/
p.fit_pdi_page(page, 0, 0, L"adjustpage");
p.close_pdi_page(page);
p.end_page_ext(L"");
}
p.close_pdi_document(indoc);
}
p.end_document(L"");
}
catch (PDFlib::Exception &ex) {
wcerr << L"PDFlib exception occurred:" << endl
<< L"[" << ex.get_errnum() << L"] " << ex.get_apiname()
<< L": " << ex.get_errmsg() << endl;
return 2;
}
return 0;
}