int main (int argc, char **argv) { HPDF_Doc pdf; HPDF_Font font; HPDF_Page page; char fname[256]; HPDF_Destination dst; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } /* error-handler */ if (setjmp(env)) { HPDF_Free (pdf); return 1; } HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); /* create default-font */ font = HPDF_GetFont (pdf, "Helvetica", NULL); /* add a new page object. */ page = HPDF_AddPage (pdf); HPDF_Page_SetWidth (page, 650); HPDF_Page_SetHeight (page, 500); dst = HPDF_Page_CreateDestination (page); HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1); HPDF_SetOpenAction(pdf, dst); HPDF_Page_BeginText (page); HPDF_Page_SetFontAndSize (page, font, 20); HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70); HPDF_Page_ShowText (page, "JpegDemo"); HPDF_Page_EndText (page); HPDF_Page_SetFontAndSize (page, font, 12); draw_image (pdf, "rgb.jpg", 70, HPDF_Page_GetHeight (page) - 410, "24bit color image"); draw_image (pdf, "gray.jpg", 340, HPDF_Page_GetHeight (page) - 410, "8bit grayscale image"); /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }
int fileWriter(HPDF_Doc pdf, HPDF_Outline root, HPDF_Font font, const std::string curFile, const std::vector< std::string> vec) { auto func_footbar = [](std::string fileName)->std::string { std::ostringstream buffer; buffer << " File:" << fileName.c_str() << " Autor:XCL "; return buffer.str(); }; auto func_pageOutline = [&](HPDF_Page page, std::string curFile) { HPDF_Outline outline; HPDF_Destination dst; outline = HPDF_CreateOutline(pdf, root, curFile.c_str(), NULL); dst = HPDF_Page_CreateDestination(page); HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1);// 1 = 默认百分比100 HPDF_Outline_SetDestination(outline, dst); }; auto func_drawRow = [](HPDF_Page page,std::string rid,std::string row,int y ) { HPDF_Page_BeginText(page); HPDF_Page_MoveTextPos(page, config::ROWID_WIDTH, y - 2); HPDF_Page_ShowText(page, rid.c_str()); HPDF_Page_ShowText(page, row.c_str()); HPDF_Page_EndText(page); }; HPDF_Page page = nullptr; HPDF_REAL width,height; HPDF_UINT y = 0; // x =0, int currID = 0, pageID = 0, currRowCount; std::string ss, rid; for (int i = 0; i < vec.size(); i++) { if (i == 0 || currRowCount > config::PAGE_ROWNUM) { ////////////////////////////////////////////// page = HPDF_AddPage(pdf); width = HPDF_Page_GetWidth(page); height = HPDF_Page_GetHeight(page); // x = 0; y = height - config::ROW_HEIGHT; func_pageOutline(page, curFile); HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT); { HPDF_RGBColor c = HPDF_Page_GetRGBFill(page); HPDF_Page_SetRGBStroke(page, 0.0, 0.5, 0); HPDF_Page_SetFontAndSize(page, font, config::FOOTBAR_FONT_SIZE); HPDF_Page_SetLineWidth(page, 0.5); HPDF_Page_MoveTo(page, 0, 13); HPDF_Page_LineTo(page, width, 16); HPDF_Page_Stroke(page); HPDF_Page_SetRGBFill(page, 0.0, 0, 0.5); HPDF_Page_BeginText(page); HPDF_Page_MoveTextPos(page, 40, config::FOOTBAR_FONT_SIZE - 5); HPDF_Page_ShowText(page, func_footbar(curFile).c_str()); HPDF_Page_EndText(page); HPDF_Page_SetRGBFill(page, c.r, c.g, c.b); }; y = height - config::ROW_HEIGHT * 2; HPDF_Page_SetFontAndSize(page, font, config::PAGE_FONT_SIZE); pageID++; currRowCount = 1; ////////////////////////////////////////////// } /////////////////////////////////////////// ss = vec[i]; HPDF_REAL wrid = HPDF_Page_TextWidth(page, rid.c_str()); NEWROW: HPDF_REAL wss = HPDF_Page_TextWidth(page, ss.c_str()); rid = util::rowidWidth(currRowCount ); if (wrid + wss > width) { //简单处理:超过页宽,直接分成两行.足以应付大部份情况. int center = ss.length() / 2; func_drawRow(page, rid,ss.substr(0, center),y); y -= config::ROW_HEIGHT; currRowCount++; ss = ss.substr(center); goto NEWROW; } else { func_drawRow(page, rid, ss,y); y -= config::ROW_HEIGHT; currRowCount++; } ////////////////////////////////////////// } return 0; } //end func
int main(int argc, char **argv) { HPDF_Doc pdf; HPDF_Font font; HPDF_Page index_page; HPDF_Page page[9]; HPDF_Destination dst; char fname[256]; HPDF_Rect rect; HPDF_Point tp; HPDF_Annotation annot; HPDF_UINT i; const char *uri = "http://libharu.org"; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } if (setjmp(env)) { HPDF_Free (pdf); return 1; } /* create default-font */ font = HPDF_GetFont (pdf, "Helvetica", NULL); /* create index page */ index_page = HPDF_AddPage (pdf); HPDF_Page_SetWidth (index_page, 300); HPDF_Page_SetHeight (index_page, 220); /* Add 7 pages to the document. */ for (i = 0; i < 7; i++) { page[i] = HPDF_AddPage (pdf); print_page(page[i], font, i + 1); } HPDF_Page_BeginText (index_page); HPDF_Page_SetFontAndSize (index_page, font, 10); HPDF_Page_MoveTextPos (index_page, 15, 200); HPDF_Page_ShowText (index_page, "Link Annotation Demo"); HPDF_Page_EndText (index_page); /* * Create Link-Annotation object on index page. */ HPDF_Page_BeginText(index_page); HPDF_Page_SetFontAndSize (index_page, font, 8); HPDF_Page_MoveTextPos (index_page, 20, 180); HPDF_Page_SetTextLeading (index_page, 23); /* page1 (HPDF_ANNOT_NO_HIGHTLIGHT) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page1 (HilightMode=HPDF_ANNOT_NO_HIGHTLIGHT)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[0]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_NO_HIGHTLIGHT); /* page2 (HPDF_ANNOT_INVERT_BOX) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page2 (HilightMode=HPDF_ANNOT_INVERT_BOX)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[1]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BOX); /* page3 (HPDF_ANNOT_INVERT_BORDER) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page3 (HilightMode=HPDF_ANNOT_INVERT_BORDER)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[2]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BORDER); /* page4 (HPDF_ANNOT_DOWN_APPEARANCE) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page4 (HilightMode=HPDF_ANNOT_DOWN_APPEARANCE)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[3]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_DOWN_APPEARANCE); /* page5 (dash border) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page5 (dash border)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[4]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetBorderStyle (annot, 1, 3, 2); /* page6 (no border) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page6 (no border)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[5]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetBorderStyle (annot, 0, 0, 0); /* page7 (bold border) */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "Jump to Page7 (bold border)"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_MoveToNextLine (index_page); dst = HPDF_Page_CreateDestination (page[6]); annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst); HPDF_LinkAnnot_SetBorderStyle (annot, 2, 0, 0); /* URI link */ tp = HPDF_Page_GetCurrentTextPos (index_page); HPDF_Page_ShowText (index_page, "URI ("); HPDF_Page_ShowText (index_page, uri); HPDF_Page_ShowText (index_page, ")"); rect.left = tp.x - 4; rect.bottom = tp.y - 4; rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4; rect.top = tp.y + 10; HPDF_Page_CreateURILinkAnnot (index_page, rect, uri); HPDF_Page_EndText (index_page); /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }
int main (int argc, char **argv) { HPDF_Doc pdf; char fname[256]; HPDF_Font font; const char *font_name; int i = 0; HPDF_Outline root; const char *encodings[] = { "StandardEncoding", "MacRomanEncoding", "WinAnsiEncoding", "ISO8859-2", "ISO8859-3", "ISO8859-4", "ISO8859-5", "ISO8859-9", "ISO8859-10", "ISO8859-13", "ISO8859-14", "ISO8859-15", "ISO8859-16", "CP1250", "CP1251", "CP1252", "CP1254", "CP1257", "KOI8-R", "Symbol-Set", "ZapfDingbats-Set", NULL }; pdf = HPDF_NewEx (error_handler, NULL, NULL, 0, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } if (setjmp(env)) { HPDF_Free (pdf); return 1; } strcpy (fname, argv[0]); strcat (fname, ".pdf"); /* set compression mode */ HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); /* Set page mode to use outlines. */ HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE); /* get default font */ font = HPDF_GetFont (pdf, "Helvetica", NULL); /* load font object */ #ifdef __WIN32__ font_name = HPDF_LoadType1FontFromFile (pdf, "type1\\a010013l.afm", "type1\\a010013l.pfb"); #else font_name = HPDF_LoadType1FontFromFile (pdf, "type1/a010013l.afm", "type1/a010013l.pfb"); #endif /* create outline root. */ root = HPDF_CreateOutline (pdf, NULL, "Encoding list", NULL); HPDF_Outline_SetOpened (root, HPDF_TRUE); while (encodings[i]) { HPDF_Page page = HPDF_AddPage (pdf); HPDF_Outline outline; HPDF_Destination dst; HPDF_Font font2; HPDF_Page_SetWidth (page, PAGE_WIDTH); HPDF_Page_SetHeight (page, PAGE_HEIGHT); outline = HPDF_CreateOutline (pdf, root, encodings[i], NULL); dst = HPDF_Page_CreateDestination (page); HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1); /* HPDF_Destination_SetFitB(dst); */ HPDF_Outline_SetDestination(outline, dst); HPDF_Page_SetFontAndSize (page, font, 15); draw_graph (page); HPDF_Page_BeginText (page); HPDF_Page_SetFontAndSize (page, font, 20); HPDF_Page_MoveTextPos (page, 40, PAGE_HEIGHT - 50); HPDF_Page_ShowText (page, encodings[i]); HPDF_Page_ShowText (page, " Encoding"); HPDF_Page_EndText (page); if (strcmp (encodings[i], "Symbol-Set") == 0) font2 = HPDF_GetFont (pdf, "Symbol", NULL); else if (strcmp (encodings[i], "ZapfDingbats-Set") == 0) font2 = HPDF_GetFont (pdf, "ZapfDingbats", NULL); else font2 = HPDF_GetFont (pdf, font_name, encodings[i]); HPDF_Page_SetFontAndSize (page, font2, 14); draw_fonts (page); i++; } /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }