gusucode.com > 用PDFLib生成PDF文档C++源码程序 > 用PDFLib生成PDF文档/PDFLibTest/PDFLibTest/PDFlibTest/PDFlibTest.cpp

    // PDFlibTest.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include "..\PDFLib\PDFLib.hpp"

#pragma comment(lib, "..\\PDFLib\\PDFLib.lib")

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();

		cout << "vckbase.pdf成功生成!\r\n";

	}
	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;
}