📄 firstpdf.cpp
字号:
// FirstPDF.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
/*
在新的C++标准中,生成新头文件的方法仅仅是将现有C++头文件名中的 .h 去掉。例如,<iostream.h>变成了<iostream>,<complex.h>变成了<complex>,等等。对于C头文件,采用同样的方法,但在每个名字前还要添加一个c。所以C的<string.h>变成了<cstring>,<stdio.h>变成了<cstdio>,等等。
旧的C++头文件是官方所反对使用的(即,明确列出不再支持),但旧的C头文件则没有(以保持对C的兼容性)。
下面是C++头文件的现状:
· 旧的C++头文件名如<iostream.h>将会继续被支持,尽管它们不在官方标准中。这些头文件的内容不在名字空间std中。
· 新的C++头文件如<iostream>包含的基本功能和对应的旧头文件相同,但头文件的内容在名字空间std中。(在标准化的过程中,库中有些部分的细节被修改了,所以旧头文件和新头文件中的实体不一定完全对应。)
· 标准C头文件如<stdio.h>继续被支持。头文件的内容不在std中。
· 具有C库功能的新C++头文件具有如<cstdio>这样的名字。它们提供的内容和相应的旧C头文件相同,只是内容在std中。
*/
#include "..\\PDFLib\\PDFLib.hpp"
//我们经常用到的是#pragma comment(lib,"*.lib")这类的。
//#pragma comment(lib,"Ws2_32.lib")表示链接Ws2_32.lib这个
//库。 和在工程设置里写上链入Ws2_32.lib的效果一样,不过
//这种方法写的 程序别人在使用你的代码的时候就不用再设置
//工程settings了
#pragma comment(lib,"..\\PDFLib\\PDFLib.lib")
int main()
{
try
{
PDFlib pdf;
pdf.set_parameter("compatibility","1.4");//兼容Acrobat 5
if(pdf.open("MyFirstPDF.pdf") == -1)
throw("Opening the file is wrong!");
pdf.set_info("Creator", "PDF Creator");
pdf.set_info("Author", "wangjt");
pdf.set_info("Title", "用VC++实现pDF的创建");
pdf.set_info("Subject", "PDF Creator");
pdf.set_info("Keywords", "pdf.lib pdf.dll");
pdf.begin_page(a4_width,a4_height);
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("欢迎您到来!");
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("版权所有 西北师范大学", 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, "explanation.txt", "VCKBASE", "wj", "zip", "paperclip");
// 结束本页
pdf.end_page();
// 关闭PDF文件
pdf.close();
cout << "MyFirstPDF.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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -