ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PDFlib创建pdf文档

2021-04-24 18:34:06  阅读:322  来源: 互联网

标签:set PDFlib 50 height 文档 a4 pdf


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

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

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

 把它解压出来,里面有几个目录和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://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,在做个例子

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目录里就行了,总体来说使用起来还是比较简单的

 

程序员阿飞

2021年4月24日

 

标签:set,PDFlib,50,height,文档,a4,pdf
来源: https://www.cnblogs.com/nxopen2018/p/14697634.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有