c++ 生成pdf

PDFLib介绍函数

  PDFLib是用于建立PDF文档的开发库,提供了简单易用的API,隐藏了建立PDF的复杂细节且不须要第3方软件的支持。PDFLib库对于我的是免费的,对于商业产品须要购买许可, 您能够到VC知识库的工具与资源栏目下载:http://www.vckbase.com/tools/。工具

3、在VC++中使用PDFLib字体

  本文例子中使用的PDFLib是4.0.2版本,与5.0版本差很少。5.0免费版本中有一个WWW.PDFLIB.COM的水印,4.0中没有。编码

  3.1 前期准备spa

  创建工程后,将except.cpp,except.h,pdflib.cpp,pdflib.h,pdflib.dll,pdflib.lib拷贝到工程目录。code

  3.2 编码ip

  3.2.1 添加对头文件和库的引用#include "PDFLib.hpp"
#pragma comment(lib, "PDFLib.lib")
3.2.2生成PDF文档的过程
资源

  生成PDF文档的过程很是简单,请看以下编码:int main(void)
{
  try
  {
    PDFlib pdf;
    // 设置兼容参数
    pdf.set_parameter("compatibility", "1.4");  // 兼容Acrobat 5
    // 打开文档
    if(pdf.open("vckbase.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;
}
PDFLIB还有许多功能,好比书签、PDF导入等功能,具体能够参考PDFLIB函数手册(能够到VC知识库中下载pdflib5.0,里面包含了该手册)。
开发

相关文章
相关标签/搜索