zoukankan      html  css  js  c++  java
  • PDFlib创建pdf文档

    PDFlib这个库可以创建pdf文档,写文字,加图片加附件,还可以画线画图形,但是它是收费的,官网https://www.pdflib.com/download/pdflib-product-family/

    在CSDN上也有破解版下载https://download.csdn.net/download/sayyoume8220202/2975682?utm_source=iteye_new

    只能用win32项目,不能x64

    下载下来后是这样一个压缩包

     把它解压出来,里面有几个目录和licensekey,readme

     bind文件夹里面有不同语言的例子可以看

     doc下是一些帮助文档

     pdflib下是动态库和静态库还有注册表

     这些都可以打开自己看一看。

    我看的是它C++的例子,直接拿VS打开就行了

    去bind-cpp-examples_cpp.sln

     这里面有很多的例子,api用法。一开始看这个hello就行了。

    你们可以会跟我一样遇到,编译可以通过,但是执行exe的时候pdf会是错误的,这主要是找不到宋体,中文字体的问题。

    还有就是不写文字,创建出来的PDF也是有水印的。这个字体问题,我研究了一下午,谁成想它的库文件不支持中文字体,还要下载其他文件来加载中文

    使用pdflib主要就是这两个问题:

    1.pdf输出中文字体

    2.pdf去掉水印

    解决办法:

    1.想输出中文字体,是需要去百度下载PDFlib-CMap这个文件的。(百度搜下就有了)

     在hello.cpp这个例子中加入代码,写入cmap的路径,就可以找到中文编码的字体了

    pdf.set_parameter("SearchPath", "C:\Program Files\PDFlib\PDFlib 7.0.3\resource\cmap");

     其实这个地方,它的注册表文件里已经写路径了,但是把cmap放过去也不管用,字体加载不上去,必须要代码去写上去,才管用。还有lic那个地方也是,注册表不管用

     2.想去除水印方法

    加上这段代码,也就是它上面注册表里的lic

    pdf.set_parameter("license", "w700602-009100-731090-Y6WPH2-5SE4A2");

     然后问题就可以解决了,编译代码执行exe就行了,(还有其他问题,自己单步调试去找把)

    相关参考资料

    https://blog.csdn.net/Mr_oOo_/article/details/100581088

    https://www.jb51.net/shouce/php5/zh/function.pdf-findfont.html

    https://wenku.baidu.com/view/d77e40f804a1b0717fd5dd9d.html

    https://blog.csdn.net/c_base_jin/article/details/80905788

    // $Id: hello.cpp,v 1.22 2006/10/01 19:55:41 rjs Exp $
    //
    // PDFlib client: hello example in C++
    //
    
    #include <iostream>
    
    #include "pdflib.hpp"
    
    using namespace std;
    
    int main(void)
    {
        try
        {
            PDFlib pdf;
    
            pdf.set_parameter("license", "w700602-009100-731090-Y6WPH2-5SE4A2");
            pdf.set_parameter("SearchPath", "C:\Program Files\PDFlib\PDFlib 7.0.3\resource\cmap");
    
            // 设置兼容参数
            pdf.set_parameter("compatibility", "1.4");    // 兼容Acrobat 5
    
    
            // 打开文档
            if(pdf.begin_document("hello.pdf","") == -1)
                throw("打开文件出错!");
    
            // 设置文档信息
            pdf.set_info("Creator", "PDF Creator");
            pdf.set_info("Author", "WangJun");
            pdf.set_info("Title", "Convert to PDF");
            pdf.set_info("Subject", "PDF Creator");
            pdf.set_info("Keywords", "vckbase.com");
    
            // 开始A4页面
            pdf.begin_page(a4_width, a4_height);
    
            // 设置字体为12号宋体
            int font_song = pdf.findfont("STSong-Light", "GB-EUC-H", 0);
    
            pdf.setfont(font_song, 12);
    
            // 设置起始点
            pdf.set_text_pos(50, a4_height - 50);
    
            // 设置颜色为蓝色
            pdf.setcolor("fill", "rgb", 0, 0, 1, 0);
    
            // 输出文字
            pdf.show("VCKBASE.COM欢迎您!");
    
            pdf.setcolor("fill", "rgb", 0, 0, 0, 0);
            pdf.setfont(font_song, 24);
            pdf.continue_text("在线杂志");
    
            // 画两根绿线
            pdf.setcolor("stroke", "rgb", 0.24f, 0.51f, 0.047f, 0);
            pdf.moveto(50, a4_height - 80);
            pdf.lineto(a4_width - 50, a4_height - 80);
            pdf.moveto(50, a4_height - 78);
            pdf.lineto(a4_width - 50, a4_height - 78);
            pdf.stroke();
    
            // 填充一个蓝色方框
            pdf.setcolor("fill", "rgb", 0.04f, 0.24f, 0.62f, 0);
            pdf.rect(50, 50, a4_width - 100, 70);
            pdf.fill();
    
            // 在指定位置输出文字
            pdf.setcolor("fill", "rgb", 0, 1, 1, 0);
            pdf.setfont(font_song, 16);
            pdf.show_xy("版权所有 VCKBASE", a4_width - 280, 60);
    
            // 打开并显示一个图像
            int img = pdf.open_image_file("jpeg", "vckbase.jpg", "", 0);
            pdf.place_image(img, 200, 400, 1);
            pdf.close_image(img);
    
            ////添加附件
            //pdf.attach_file(a4_width - 50, 0, 0, a4_height - 150,
            //        "vckbase.zip", "VCKBASE", "wj", "zip", "paperclip");
    
            // 结束本页
            pdf.end_page();
    
            // 关闭PDF文件
            pdf.close();
    
        }
        catch(PDFlib::Exception &ex)
        {
    //        cerr << "错误信息:" << ex.get_message() << endl;
            return -1;
        }
        catch(char *pStrErr)
        {
            cerr << pStrErr << endl;
            return -1;
        }
        catch(...)
        {
            cerr << "发生未知异常!" << endl;
            return -1;
        }
    
        return 0;
    }
    /*
    
    int
    main(void)
    {
        try {
        int font;
        PDFlib p;
    
        //  This means we must check return values of load_font() etc.
        p.set_parameter("errorpolicy", "return");
    
        // This line is required to avoid problems on Japanese systems
        p.set_parameter("hypertextencoding", "host");
    
        if (p.begin_document("hello.pdf", "") == -1) {
            cerr << "Error: " << p.get_errmsg() << endl;
            return 2;
        }
    
        p.set_info("Creator", "hello.cpp");
        p.set_info("Author", "Thomas Merz");
        p.set_info("Title", "Hello, world (C++)!");
    
        p.begin_page_ext(a4_width, a4_height, "");
    
        // Change "host" encoding to "winansi" or whatever you need!
        font = p.load_font("SIMKAI", "host", "");
        if (font == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return(2);
        }
        p.setfont(font, 24);
        p.set_text_pos(50, 700);
        p.show("你好!");
        p.continue_text("(says C++)");
        p.end_page_ext("");
    
        p.end_document("");
        }
    
        catch (PDFlib::Exception &ex) {
        cerr << "PDFlib exception occurred in hello sample: " << endl;
        cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
            << ": " << ex.get_errmsg() << endl;
        return 2;
        }
    
        return 0;
    }
    */

    这是它的例子,下面我们自己新建个控制台项目,调用SDK,在做个例子

    只能用win32项目,不能x64

    控制台项目直接搭建调用,MFC项目的话,搭建完之后,还需要把pdflib.cpp的属性设置不使用预编译头

    1.新建个控制台项目

     2.把pdflib需要的头文件源文件动态库静态库那些都考到我们的项目下

     

    添加pdflib的头文件和库文件到VS里

     设置附加包含目录

     设置附加库目录

     添加附加依赖项

     在项目里写代码,可以直接把它例子里的部分代码考过来,加过去

    #include <stdio.h>
    #include <iostream>
    #include "pdflib.hpp"
    
    using namespace std;
    
    int main()
    {
        try
        {
            PDFlib pdf;
    
            pdf.set_parameter("license", "w700602-009100-731090-Y6WPH2-5SE4A2");
            pdf.set_parameter("SearchPath", "C:\Program Files\PDFlib\PDFlib 7.0.3\resource\cmap");
    
            // 设置兼容参数
            pdf.set_parameter("compatibility", "1.4");    // 兼容Acrobat 5
    
    
            // 打开文档
            if (pdf.begin_document("hello.pdf", "") == -1)
                throw("打开文件出错!");
    
            // 设置文档信息
            pdf.set_info("Creator", "PDF Creator");
            pdf.set_info("Author", "WangJun");
            pdf.set_info("Title", "Convert to PDF");
            pdf.set_info("Subject", "PDF Creator");
            pdf.set_info("Keywords", "vckbase.com");
    
            // 开始A4页面
            pdf.begin_page(a4_width, a4_height);
    
            // 设置字体为12号宋体
            int font_song = pdf.findfont("STSong-Light", "GB-EUC-H", 0);
    
            pdf.setfont(font_song, 12);
    
            // 设置起始点
            pdf.set_text_pos(50, a4_height - 50);
    
            // 设置颜色为蓝色
            pdf.setcolor("fill", "rgb", 0, 0, 1, 0);
    
            // 输出文字
            pdf.show("VCKBASE.COM欢迎您!");
    
            pdf.setcolor("fill", "rgb", 0, 0, 0, 0);
            pdf.setfont(font_song, 24);
            pdf.continue_text("在线杂志");
    
            // 画两根绿线
            pdf.setcolor("stroke", "rgb", 0.24f, 0.51f, 0.047f, 0);
            pdf.moveto(50, a4_height - 80);
            pdf.lineto(a4_width - 50, a4_height - 80);
            pdf.moveto(50, a4_height - 78);
            pdf.lineto(a4_width - 50, a4_height - 78);
            pdf.stroke();
    
            // 填充一个蓝色方框
            pdf.setcolor("fill", "rgb", 0.04f, 0.24f, 0.62f, 0);
            pdf.rect(50, 50, a4_width - 100, 70);
            pdf.fill();
    
            // 在指定位置输出文字
            pdf.setcolor("fill", "rgb", 0, 1, 1, 0);
            pdf.setfont(font_song, 16);
            pdf.show_xy("版权所有 VCKBASE", a4_width - 280, 60);
    // 结束本页
            pdf.end_page();
    
            // 关闭PDF文件
            pdf.close();
    
        }
        catch (PDFlib::Exception &ex)
        {
            cerr << "错误信息:" << ex.get_errmsg()<< endl;
            return -1;
        }
        catch (char *pStrErr)
        {
            cerr << pStrErr << endl;
            return -1;
        }
        catch (...)
        {
            cerr << "发生未知异常!" << endl;
            return -1;
        }
    
    
        return 0;
    }

    编译项目生成功能,在把pdflib.dll动态库放到exe目录里就行了,总体来说使用起来还是比较简单的

     参考资料https://www.cnblogs.com/honernan/p/12991640.html

    程序员阿飞

    2021年4月24日

    作者: 阿飞

    出处: https://www.cnblogs.com/nxopen2018/>

    关于作者:专注NX开发、VC++开发、数据库、三维建模领域,请多多赐教!

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言(博客文章底部留言)咨询.

  • 相关阅读:
    第10组 Beta冲刺 (3/5)
    第10组 Beta冲刺 (2/5)
    第10组 Beta冲刺 (1/5)
    软工实践个人总结
    第03组 每周小结(3/3)
    第03组 每周小结(2/3)
    第03组 每周小结(1/3)
    第03组 Beta冲刺 总结
    第03组 Beta冲刺 (5/5)
    第03组 Beta冲刺 (4/5)
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/14697634.html
Copyright © 2011-2022 走看看