C + + PDF document related operations

Source: Internet
Author: User
Tags pdflib text to pdf

Life is always in the rush of one time after another until the final deadline.

Recently, basically did not update the blog, is to prepare IELTS to watch English and adjust the old look at English after the irritability of the mood. It was thought that after the completion of research and the final exam no longer disappeared, this idea is Too young,too naive. Previously done a project, is about the Windows platform in C + + implementation of the PDF document production of related exercises , a little mood today to summarize.

About the PDF Library In fact there are many, I started with the artifact search is pdflib, and use this library to do. This is the so-called fate it.

    Pdflib is not an open source library, you need to pay for the use, otherwise there will be a watermark that people don't want in the back of the page. Of course, my big celestial never need to ignore these, there will always be the oppression of Lei Feng stand out to make a cracked version, the library is no exception, there is a need to the Lei Feng's blog location to download. If it is lazy can download my demo project, which has been configured in the library, but because I am in the spirit of tossing to death of the mentality of programmers, the project's environment is the latest VS2015, if your version is not the latest, download please own silence, very I will not return the haha.

    Pdflib is a standard dynamic link library , as is the case with other dynamic-link libraries, put Pdflib.lib, Pdflib.dll and pdflib.h put it in the project directory and put pdflib.lib This additional dependency, included in the project pdflib.h This existing item is configured even if it is completed. The next step is how to use the library, initially containing pdflib.h

specific use, there are some ready-made examples on the net, but have not seen can solve good Chinese and English mixed together problem. Because on-line 80% or 90% are suggested with Pdflib to bring the font, and it is regrettable that these fonts on the Chinese language support is not good and the font is so few, in general can be separated to call Chinese and English also forget, however, in some Chinese and English at the same time the situation is only the layout of the gap problem to kneel, To write the program to go through the characters and then distinguish the treatment, costly, desperate. The job I did, and then inadvertently found some blog accidentally mentioned can call the local font, so determined to try. Really do not know, a try a mouthful of old blood almost spit out, the local font is perfect to support Chinese and English do not say, fonts are what have what ... Sometimes some blogs really mislead the dead ... Therefore, I strongly recommend this library to use the program ape directly to C:\Windows\Fonts This path to find the font you like to get out, and then put it in your project directory to call, what comes with the font is totally pit Dad!!! Below the code directly, the output of a page is "will Libai" PDF file. (Build a Win32 console blank project and Configure the environment to copy the following code in theory, you can also run. )

#include "pdflib.h" #include <afx.h> void String2text (PDF *p, int &textflow, CString &mpagenameanddate, int fontsize) {char Characteristic_cn[100];int mpagenameanddatesize = Mpagenameanddate.getlength ();//String length pdf_set_ Parameter (p, "Fontoutline", "A=./font/stxingka". TTF ");//Here's"./font/stxingka. TTF "is the font file storage path, that is, you need to put the library file in the appropriate location, otherwise it will cause error sprintf_s (CHARACTERISTIC_CN," fontname=a:0 fontsize =%d encoding = Unicode Textlen =%d ", FontSize, Mpagenameanddatesize * 2);//Chinese characters pdf_add_textflow (p, TextFlow, (char *) (mpagenameanddate). AllocSysString (), Mpagenameanddatesize * 2, CHARACTERISTIC_CN);//Add character segment content}void outputstringtopdf (PDF *p, CString & mpagenameanddate, int left, int &top, int. right, int Reduction, int fontsize)//Data output {//The function outputs data to PDF, It is important to note that when the output to the page is not in the self-paging if (top<130) {Top = 740; Pdf_end_page_ext (P, ""); Close the page and open the new page Pdf_begin_page_ext (p, A4_width, A4_height, "");} int TextFlow = Pdf_create_textflow (P, "", 0, "");//create character segment String2text (P, TextFlow, Mpagenameanddate, fontsize);while (true) {const char *returnstring = Pdf_fit_textflow (p, TextFlow, left, Top, right, Top-reduction, ""); Top-= Reduction;if (strcmp (returnstring, "_boxfull")! = 0)//object full {break;} if (top<130) {Top = 740; Pdf_end_page_ext (P, ""); Close the page and open the new page Pdf_begin_page_ext (p, A4_width, A4_height, "");}} Pdf_delete_textflow (P, textflow);//Free String Object}void main () {PDF *p = Null;int left = $, Top = 740, right = A4_width-85;//pdf Object creation if ((P = pdf_new ()) = = (PDF *) 0) {printf ("couldn ' t create Pdflib object (out of memory)!n");} Pdf_try (p) {//Create PDF document first, named Outout.pdfif (Pdf_begin_document (P, "Output.pdf", 0, "") = =-1) {printf ("Error:%sn", pdf_get_ ErrMsg (P));} Then create a PDF document inside the page, and fill in the content of the page, here I encapsulated it into two functions Pdf_begin_page_ext (p, A4_width, A4_height, ""); CString mpagenameanddate = l "June do not see the Yellow River water days come up to the sea no Return \"; Mpagenameanddate + = l "June don't see Gao Tang mirror sad white hair into the snow"; Mpagenameanddate + = L "life  Proud to unfocused the Golden bottle empty to the Moon \ n "; Mpagenameanddate + + L" Born of my material will be useful daughter scattered all the same "; Mpagenameanddate + = L" Cook sheep and slaughter cows for pleasure will have to drink 300 cups \ n "; Mpagenameanddate + = L "Zendzi Danchusheng will enter the wine glass mo stop \"; mPagEnameanddate + = L "and June song please June for me eg listen to \ \"; Mpagenameanddate + = l "zhong gu meal jade not enough you wish long drunk not wake up \"; Mpagenameanddate + = L "All the saints are lonely but the drinkers leave their name \ n"; MP Agenameanddate + = l "Chen Wang feast Pingle 10,000 pleases Huan flagellation \ n"; Mpagenameanddate + = L "Master What is said to be a small sum of money must be put to the King"; Mpagenameanddate + = l "Five flowers Ma Chi fur \ n"; Mpagen Ameanddate + = L "will be out for the sake of the wine with the same sales of eternal sorrow";//output text to PDF, the third parameter is the left margin of the output, the fourth parameter is the height of the output, the fifth parameter is the right margin of the output, the sixth parameter is the line spacing, The seventh one is the size of the font outputstringtopdf (p, Mpagenameanddate, left, Top, right, 45, 24);//Page End Pdf_end_page_ext (p, "");//Document End Pdf_end _document (P, "");} Pdf_catch (p)//error message output {printf ("[%d]%s:%sn", Pdf_get_errnum (p), Pdf_get_apiname (p), pdf_get_errmsg (p)); Pdf_delete (P);} Pdf_delete (P);//Open Pdfsystem ("Output.pdf");}
The effect is as follows:
because I do the project only use the output PDF file that part, so about to read the PDF file part is not written, and later if you do it will be put up. For more details, refer to Pdflib's manual, which I will also pack with my demo, which is the demo address.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + PDF document related operations

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.