最近公司准備在PDF方面發力了,我也要行動起來,就找到了LibHaru這個開源庫
編譯Libharu需要用到zlib庫和libpng庫,libpng庫又依賴zlib庫.
zlib 下載地址:http://www.zlib.net/
libpng下載地址:http://www.libpng.org/pub/png/libpng.html
libharu下載地址:http://libharu.org/
下載好后,最好放到一個單獨的文件夾下,
進入目錄 D:\Users\PDF\lpng\projects\visualc71 里面有VS的工程,根據自己的需求編譯DLL或者LIb選擇不同的編譯選項,
設置libpng工程的zlib頭文件路徑,zlib工程和上圖一樣,編譯后,生成libpng.lib和zlib.lib,2個靜態庫。
為了方便編譯Libharu,將zlib和libpng的頭文件與靜態庫,單獨存放到各自的include和lib文件夾里。
修改D:\Users\PDF\libharu\script\Makefile.msvc文件,如下圖所示的2處
管理員 運行 “VS2008命令提示” ,
cd /D D:\Users\PDF\libharu
nmake script/Makefile.msvc
至此,LibHaru編譯完成。
創建win32命令行工程測試下。
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <setjmp.h> 5 #include "hpdf.h" 6 7 #ifndef HPDF_NOPNGLIB 8 9 jmp_buf env; 10 11 #ifdef HPDF_DLL 12 void __stdcall 13 #else 14 void 15 #endif 16 error_handler (HPDF_STATUS error_no, 17 HPDF_STATUS detail_no, 18 void *user_data) 19 { 20 printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no, 21 (HPDF_UINT)detail_no); 22 longjmp(env, 1); 23 } 24 25 void 26 draw_image (HPDF_Doc pdf, 27 const char *filename, 28 float x, 29 float y, 30 const char *text) 31 { 32 #ifdef __WIN32__ 33 const char* FILE_SEPARATOR = "\\"; 34 #else 35 const char* FILE_SEPARATOR = "/"; 36 #endif 37 char filename1[255]; 38 39 HPDF_Page page = HPDF_GetCurrentPage (pdf); 40 HPDF_Image image; 41 42 strcpy(filename1, "pngsuite"); 43 strcat(filename1, FILE_SEPARATOR); 44 strcat(filename1, filename); 45 46 image = HPDF_LoadPngImageFromFile (pdf, filename1); 47 48 /* Draw image to the canvas. */ 49 HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image), 50 HPDF_Image_GetHeight (image)); 51 52 /* Print the text. */ 53 HPDF_Page_BeginText (page); 54 HPDF_Page_SetTextLeading (page, 16); 55 HPDF_Page_MoveTextPos (page, x, y); 56 HPDF_Page_ShowTextNextLine (page, filename); 57 HPDF_Page_ShowTextNextLine (page, text); 58 HPDF_Page_EndText (page); 59 } 60 61 62 int main (int argc, char **argv) 63 { 64 HPDF_Doc pdf; 65 HPDF_Font font; 66 HPDF_Page page; 67 char fname[256]; 68 HPDF_Destination dst; 69 70 strcpy (fname, argv[1]); 71 strcat (fname, ".pdf"); 72 73 pdf = HPDF_New (error_handler, NULL); 74 if (!pdf) { 75 printf ("error: cannot create PdfDoc object\n"); 76 return 1; 77 } 78 79 /* error-handler */ 80 if (setjmp(env)) { 81 HPDF_Free (pdf); 82 return 1; 83 } 84 85 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); 86 87 /* create default-font */ 88 font = HPDF_GetFont (pdf, "Helvetica", NULL); 89 90 /* add a new page object. */ 91 page = HPDF_AddPage (pdf); 92 93 HPDF_Page_SetWidth (page, 550); 94 HPDF_Page_SetHeight (page, 650); 95 96 dst = HPDF_Page_CreateDestination (page); 97 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1); 98 HPDF_SetOpenAction(pdf, dst); 99 100 HPDF_Page_BeginText (page); 101 HPDF_Page_SetFontAndSize (page, font, 20); 102 HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70); 103 HPDF_Page_ShowText (page, "PngDemo"); 104 HPDF_Page_EndText (page); 105 106 HPDF_Page_SetFontAndSize (page, font, 12); 107 108 draw_image (pdf, "hand.png", 50, 50/*HPDF_Page_GetHeight (page)*/, 109 "1bit grayscale."); 110 111 112 /* save the document to a file */ 113 HPDF_SaveToFile (pdf, fname); 114 115 /* clean up */ 116 HPDF_Free (pdf); 117 118 return 0; 119 } 120 121 #else /* HPDF_NOPNGLIB */ 122 123 int main() 124 { 125 printf("WARNING: if you want to run this demo, \n" 126 "make libhpdf with HPDF_USE_PNGLIB option.\n"); 127 return 0; 128 } 129 130 #endif /* HPDF_NOPNGLIB */
工程頭文件目錄
D:\Users\PDF\libharu\include;
D:\Users\PDF\lpng\include;
D:\Users\PDF\zlib\include;
D:\Users\PDF\libharu\win32\include
工程庫文件目錄
D:\Users\PDF\libharu;
D:\Users\PDF\lpng\lib
工程使用庫
libhpdf.lib libpng.lib zlib.lib