MFC uses Haru free PDF lib to generate PDF files

Source: Internet
Author: User
Tags haru

MFC uses Haru free PDF lib to generate PDF files

Let's start by creating a project Mfcpdfdemo and adding an edit box and a button:

Haru free PDF Class Library

Here directly download the DLL, if you want to use the static library, you have to download the source code to compile itself.
Then add this DLL to the project, how to add not to say it, or to say it, in case a novice can not understand it.
After the decompression we need to use three things:
Libhpdf.dll
Libhpdf.lib
Include entire folder
After decompression, copy the Libhpdf.dll to the release and debug directory of the project;
The libhpdf.lib and include folders are copied directly to the source file directory;

Add the following two sentences to the MFCPDFDemoDlg.cpp:

" include/hpdf.h " #pragma comment (lib, "Libhpdf.lib")

Add the following code to the button's handler function:

voidCmfcpdfdemodlg::onbnclickedbtnsave () {updatedata (TRUE); Const Char*page_title ="This title is centered";    Hpdf_doc PDF;    Hpdf_font Font;    Hpdf_page Page; Charfname[ the]; strcpy (fname,"e:\\");    strcat (fname, CT2A (M_strpath)); strcat (fname,". pdf"); PDF=hpdf_new (nullptr, nullptr); if(!pdf) {MessageBox (_t ("error:cannot Create Pdfdoc object\n")); return; }    /*Set Compression mode*/Hpdf_setcompressionmode (PDF, Hpdf_comp_all);    Hpdf_status STATUS;    Hpdf_usecnsfonts (PDF);    Hpdf_usecnsencodings (PDF); /*Create Default-font*/Font= Hpdf_getfont (PDF,"SimSun","gb-euc-h"); /*Add a new Page object.*/page=Hpdf_addpage (PDF); Status= Hpdf_page_setfontandsize (Page, Font, -); floatTW =hpdf_page_textwidth (Page, page_title); Status=Hpdf_page_begintext (Page); Status= Hpdf_page_textout (page, Hpdf_page_getwidth (page)-TW)/2, Hpdf_page_getheight (Page)- -, Page_title); Status=Hpdf_page_endtext (Page); /*Save the document to a file*/Hpdf_savetofile (PDF, fname); /* Clean Up*/Hpdf_free (pdf);}

Compile the program after you add the code and add the compile option if you are prompted to strcpy unsafe:

Note that the option to add is/d _crt_secure_no_warnings

Then compile and run, enter test, and click Save PDF:

Then under E: You can find the Test.pdf file, which opens as follows:

Complete the full text!

MFC uses Haru free PDF lib to generate PDF files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.