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