最近由於項目需要,需要用C++編輯Json數據為pdf並導出,記錄一下學習過程。
p.set_option(L"searchpath=../PDFlib-CMap-4.0/resource/cmap");
// testpdf.cpp : 定義控制台應用程序的入口點。 #include "stdafx.h" #include <iostream> #include "include/pdflib.h" #include "include/pdflib.hpp" using namespace std; using namespace pdflib; int main(void) { try { PDFlib p; // 打開文檔 if (p.begin_document(L"test.pdf", L"") == -1) { wcerr << "Error: " << p.get_errmsg() << endl; return 2; } //配置cmap,用於支持中文字體 p.set_option(L"searchpath=../PDFlib-CMap-4.0/resource/cmap"); p.set_info(L"Creator", L"oneclick"); p.set_info(L"Author", L"Thomas Merz"); p.set_info(L"Title", L"test"); p.begin_page_ext(a4_width, a4_height, L""); //設置pdf大小為A4 // 設置中文字體並檢查 int font = p.load_font(L"STSong-Light", L"UniGB-UCS2-H", L"");; if (font == -1) { wcerr << L"Error: " << p.get_errmsg() << endl; return(2); } //輸出標題 p.setfont(font, 24); p.set_text_pos(200, 800); p.show(L"Hello world"); p.continue_text(L"你好,世界"); p.end_page_ext(L""); p.end_document(L""); } catch (PDFlib::Exception &ex) { wcerr << L"PDFlib exception occurred in hello sample: " << endl << L"[" << ex.get_errnum() << L"] " << ex.get_apiname() << L": " << ex.get_errmsg() << endl; return 2; } return 0; }
代碼運行成功就會生成一個test.pdf文件,內容如下:
pdflib和Cmaps分享:
鏈接:https://pan.baidu.com/s/1WPz3pNMtszG5M-gFKQ51-Q 提取碼:z53r