使用pdflib分割pdf

最近有个分割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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值