/* * << Haru Free PDF Library 2.0.0 >> -- font_demo.cpp * * Copyright (c) 1999-2006 Takeshi Kanno * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. * It is provided "as is" without express or implied warranty. * */ #include #include #include #include #include // #include #include "hpdf.h" #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); } // const char *font_list[] = {"SimHei", NULL}; // int main(int argc, char **argv) { // const char *page_title = "Font Demo"; // HPDF_Doc pdf; // char fname[256]; // HPDF_Page page; // HPDF_Font def_font; // HPDF_REAL tw; // HPDF_REAL height; // HPDF_REAL width; // HPDF_UINT i; // strcpy(fname, argv[0]); // strcat(fname, ".pdf"); // pdf = HPDF_New(error_handler, NULL); // if (!pdf) { // printf("error: cannot create PdfDoc object\n"); // return 1; // } // HPDF_STATUS statu = HPDF_UseCNSFonts(pdf); // printf("HPDF_UseCNSFonts: %x\n", statu); // statu = HPDF_UseCNTFonts(pdf); // printf("HPDF_UseCNTFonts: %x\n", statu); // statu = HPDF_UseCNTEncodings(pdf); // printf("HPDF_UseCNTEncodings: %x\n", statu); // statu = HPDF_UseCNSEncodings(pdf); // printf("HPDF_UseCNSEncodings: %x\n", statu); // try { // /* Add a new page object. */ // page = HPDF_AddPage(pdf); // height = HPDF_Page_GetHeight(page); // width = HPDF_Page_GetWidth(page); // /* Print the lines of the page. */ // HPDF_Page_SetLineWidth(page, 1); // HPDF_Page_Rectangle(page, 50, 50, width - 100, height - 110); // HPDF_Page_Stroke(page); // /* Print the title of the page (with positioning center). */ // def_font = HPDF_GetFont(pdf, "Helvetica", NULL); // HPDF_Page_SetFontAndSize(page, def_font, 24); // tw = HPDF_Page_TextWidth(page, page_title); // HPDF_Page_BeginText(page); // HPDF_Page_MoveTextPos(page, (width - tw) / 2, height - 50); // HPDF_Page_ShowText(page, page_title); // HPDF_Page_EndText(page); // /* output subtitle. */ // HPDF_Page_BeginText(page); // HPDF_Page_MoveTextPos(page, 60, height - 80); // HPDF_Page_SetFontAndSize(page, def_font, 16); // HPDF_Page_ShowText(page, ""); // HPDF_Page_EndText(page); // HPDF_Page_BeginText(page); // HPDF_Page_MoveTextPos(page, 60, height - 105); // i = 0; // while (font_list[i]) { // const char *samp_text = "abcd"; // HPDF_Font font = HPDF_GetFont(pdf, font_list[i], NULL); // if (!font) { // printf("error: cannot get %s font\n", font_list[i]); // return 1; // } // /* print a label of text */ // HPDF_Page_SetFontAndSize(page, def_font, 9); // HPDF_Page_ShowText(page, font_list[i]); // HPDF_Page_MoveTextPos(page, 0, -18); // /* print a sample text. */ // HPDF_Page_SetFontAndSize(page, font, 20); // HPDF_Page_ShowText(page, samp_text); // HPDF_Page_MoveTextPos(page, 0, -20); // i++; // } // HPDF_Page_EndText(page); // HPDF_SaveToFile(pdf, fname); // } catch (...) { // HPDF_Free(pdf); // return 1; // } // /* clean up */ // HPDF_Free(pdf); // return 0; // } using namespace std; string utf8_to_gb2312(const std::string& utf8_str) { // 打开转换描述符 iconv_t cd = iconv_open("GB2312", "UTF-8"); if (cd == (iconv_t)-1) { perror("iconv_open failed"); exit(EXIT_FAILURE); } // 输入字符串 const char* in_str = utf8_str.c_str(); size_t in_size = utf8_str.size(); // 输出缓冲区 size_t out_size = in_size * 2; // 假设输出的字节数不会超过输入的两倍 char* out_buf = new char[out_size]; char* out_str = out_buf; // 进行转换 if (iconv(cd, const_cast(&in_str), &in_size, &out_str, &out_size) == (size_t)-1) { perror("iconv failed"); iconv_close(cd); delete[] out_buf; exit(EXIT_FAILURE); } // 关闭转换描述符 iconv_close(cd); // 获取转换后的字符串 std::string gb2312_str(out_buf, out_str - out_buf); // 释放内存 delete[] out_buf; return gb2312_str; } int main() { HPDF_Doc pdf; HPDF_Font font; HPDF_Page szPage[10115]; HPDF_REAL tw; string strPdfContent = utf8_to_gb2312("这是一个测试,支持中文"); string strPdfName = "XXX.pdf"; pdf = HPDF_New(error_handler, NULL); HPDF_UseCNSFonts(pdf); HPDF_UseCNTFonts(pdf); HPDF_UseCNTEncodings(pdf); HPDF_UseCNSEncodings(pdf); // support Chinese Song (SimSun)and Chinese black (SimHei) font = HPDF_GetFont(pdf, "SimSun", "GBK-EUC-H"); if (!font) { printf("error: cannot get font\n"); return 1; } for (int i = 0; i < 10115; ++i) { szPage[i] = HPDF_AddPage(pdf); HPDF_Page_SetSize(szPage[i], HPDF_PAGE_SIZE_LETTER, HPDF_PAGE_PORTRAIT); HPDF_Page_BeginText(szPage[i]); HPDF_Page_SetFontAndSize(szPage[i], font, 20); tw = HPDF_Page_TextWidth(szPage[i], strPdfContent.c_str()); HPDF_Page_MoveTextPos(szPage[i], (HPDF_Page_GetWidth(szPage[i]) - tw) / 2, (HPDF_Page_GetHeight(szPage[i]) - 20) / 2); HPDF_Page_ShowText(szPage[i], strPdfContent.c_str()); HPDF_Page_EndText(szPage[i]); } HPDF_SaveToFile(pdf, strPdfName.c_str()); HPDF_Free(pdf); }