LibHaru编译使用

时间:2023-03-09 03:17:15
LibHaru编译使用

最近公司准备在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/

下载好后,最好放到一个单独的文件夹下,LibHaru编译使用

进入目录  D:\Users\PDF\lpng\projects\visualc71 里面有VS的工程,根据自己的需求编译DLL或者LIb选择不同的编译选项,

LibHaru编译使用

设置libpng工程的zlib头文件路径,zlib工程和上图一样,编译后,生成libpng.lib和zlib.lib,2个静态库。

为了方便编译Libharu,将zlib和libpng的头文件与静态库,单独存放到各自的include和lib文件夹里。

LibHaru编译使用

LibHaru编译使用

修改D:\Users\PDF\libharu\script\Makefile.msvc文件,如下图所示的2处

LibHaru编译使用

管理员 运行 “VS2008命令提示” ,

cd /D D:\Users\PDF\libharu

nmake script/Makefile.msvc

LibHaru编译使用

至此,LibHaru编译完成。

创建win32命令行工程测试下。

 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "hpdf.h" #ifndef HPDF_NOPNGLIB jmp_buf env; #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);
longjmp(env, );
} void
draw_image (HPDF_Doc pdf,
const char *filename,
float x,
float y,
const char *text)
{
#ifdef __WIN32__
const char* FILE_SEPARATOR = "\\";
#else
const char* FILE_SEPARATOR = "/";
#endif
char filename1[]; HPDF_Page page = HPDF_GetCurrentPage (pdf);
HPDF_Image image; strcpy(filename1, "pngsuite");
strcat(filename1, FILE_SEPARATOR);
strcat(filename1, filename); image = HPDF_LoadPngImageFromFile (pdf, filename1); /* Draw image to the canvas. */
HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
HPDF_Image_GetHeight (image)); /* Print the text. */
HPDF_Page_BeginText (page);
HPDF_Page_SetTextLeading (page, );
HPDF_Page_MoveTextPos (page, x, y);
HPDF_Page_ShowTextNextLine (page, filename);
HPDF_Page_ShowTextNextLine (page, text);
HPDF_Page_EndText (page);
} int main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Font font;
HPDF_Page page;
char fname[];
HPDF_Destination dst; strcpy (fname, argv[]);
strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return ;
} /* error-handler */
if (setjmp(env)) {
HPDF_Free (pdf);
return ;
} 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, );
HPDF_Page_SetHeight (page, ); dst = HPDF_Page_CreateDestination (page);
HPDF_Destination_SetXYZ (dst, , HPDF_Page_GetHeight (page), );
HPDF_SetOpenAction(pdf, dst); HPDF_Page_BeginText (page);
HPDF_Page_SetFontAndSize (page, font, );
HPDF_Page_MoveTextPos (page, , HPDF_Page_GetHeight (page) - );
HPDF_Page_ShowText (page, "PngDemo");
HPDF_Page_EndText (page); HPDF_Page_SetFontAndSize (page, font, ); draw_image (pdf, "hand.png", , /*HPDF_Page_GetHeight (page)*/,
"1bit grayscale."); /* save the document to a file */
HPDF_SaveToFile (pdf, fname); /* clean up */
HPDF_Free (pdf); return ;
} #else /* HPDF_NOPNGLIB */ int main()
{
printf("WARNING: if you want to run this demo, \n"
"make libhpdf with HPDF_USE_PNGLIB option.\n");
return ;
} #endif /* HPDF_NOPNGLIB */

LibHaru编译使用

工程头文件目录

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