using System;
using System.IO;
using System.Text;
using System.Collections;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Windows.Forms;
namespace ExportTest
{
public class ExportPdf
{
#region 初始化,定义pdf文件页面大小
/// <summary>
/// pdf文件宽度
/// </summary>
static float pageWidth = 594.0f;
/// <summary>
/// pdf文件高度
/// </summary>
static float pageDepth = 828.0f;
/// <summary>
/// 文本与边界距离
/// </summary>
static float pageMargin = 30.0f;
/// <summary>
/// 字体大小
/// </summary>
static float fontSize = 10.0f;
/// <summary>
/// 页眉大小
/// </summary>
static float leadSize = 10.0f;
#endregion
/// <summary>
/// 在磁盘指定位置创建一个PDF文件
/// </summary>
StreamWriter pPDF;
/// <summary>
/// 在内存中创建文件缓冲区
/// </summary>
MemoryStream mPDF;
public ExportPdf(string fileName)
{
//pPDF = new StreamWriter(fileName);
//mPDF = new MemoryStream();
}
/// <summary>
/// 将文本文件数据转换成pdf格式并写入文件缓冲区的函数
/// </summary>
/// <param name="strMsg">文本</param>
private void ConvertToByteAndAddtoStream(string strMsg)
{
Byte[] buffer = null;
buffer = ASCIIEncoding.ASCII.GetBytes(strMsg);
mPDF.Write(buffer, 0, buffer.Length);
buffer = null;
}
/// <summary>
/// 将数据长度格式信息格式化为外部参照格式
/// </summary>
/// <param name="xValue"></param>
/// <returns></returns>
private string xRefFormatting(long xValue)
{
string strMsg = xValue.ToString();
int iLen = strMsg.Length;
if (iLen < 10)
{
StringBuilder s = new StringBuilder(); //StringBuilder是修改字符串的类
int i = 10 - iLen;
s.Append('0', i);
strMsg = s.ToString() + strMsg;
}
return strMsg;
}
/// <summary>
/// 导出pdf文件
/// </summary>
public void ExportToPdf()
{
//为pdf文件的外部格式创建一个队列列表
ArrayList xRefs = new ArrayList();
float yPos = 0f;
long streamStart = 0;
long streamEnd = 0;
long streamLen = 0;
string strPDFMessage = null; //PDF文件的头信息
strPDFMessage = "%PDF-1.1\n";
ConvertToByteAndAddtoStream(strPDFMessage); //将头信息转换成字节并加入文件缓冲区
//开始新的一页
xRefs.Add(mPDF.Length);
strPDFMessage = "1 0 obj\n"; //ID 1 For Containt
ConvertToByteAndAddtoStream(strPDFMessage); //write the Text
strPDFMessage = "<< /Length 2 0 R >>\n"; //ID 2 For Length of the Stream
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = "stream\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//得到缓冲区的开始
streamStart = mPDF.Length;
strPDFMessage = "BT\n/F0 " + fontSize + " Tf\n";
ConvertToByteAndAddtoStream(strPDFMessage);
yPos = pageDepth - pageMargin;
strPDFMessage = pageMargin + " " + yPos + " Td\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = leadSize + " TL\n";
ConvertToByteAndAddtoStream(strPDFMessage);
for (int i = 0; i < 300; i++)
{
//输入文本信息到文件缓冲区
strPDFMessage = "(It is a new test!)Tj\n";//
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = "\n";
ConvertToByteAndAddtoStream(strPDFMessage);
}
strPDFMessage = "ET\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//得到缓冲区的结尾
streamEnd = mPDF.Length;
//得到缓冲区的长度
streamLen = streamEnd - streamStart;
strPDFMessage = "endstream\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//将第二个目标加入外部参照格式
xRefs.Add(mPDF.Length);
strPDFMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
ConvertToByteAndAddtoStream(strPDFMessage);
//将页信息加入到外部参照模式
xRefs.Add(mPDF.Length);
strPDFMessage = "3 0 obj\n<</Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//建立这个页
xRefs.Add(mPDF.Length);
strPDFMessage = "4 0 obj\n<</Type /Pages /Count 1\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = "/Kids[\n3 0 R\n]\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = "/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >>\n";
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + "]\n>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//将字体信息加入到外部参照模式
xRefs.Add(mPDF.Length);
strPDFMessage = "5 0 obj\n<</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//将目录信息加入到外部参照模式
xRefs.Add(mPDF.Length);
strPDFMessage = "6 0 obj\n<</Type/Catalog/Pages 4 0 R>>\nendobj\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//外部参照模式入口
streamStart = mPDF.Length;
strPDFMessage = "xref\n0 7\n0000000000 65535 f \n";
for (int i = 0; i < xRefs.Count; i++)
{
strPDFMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
}
ConvertToByteAndAddtoStream(strPDFMessage);
//PDF位置信息
strPDFMessage = "trailer\n<<\n/Size " + (xRefs.Count + 1) + "\n/Root 6 0 R\n>>\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//外部参照模式本地入口
strPDFMessage = "startxref\n" + streamStart + "\n%%EOF\n";
ConvertToByteAndAddtoStream(strPDFMessage);
//将文件缓冲区的内容写入到文件
mPDF.WriteTo(pPDF.BaseStream);
//关闭缓冲区
mPDF.Close();
pPDF.Close();
}
/// <summary>
/// 使用itextSharp导出pdf
/// </summary>
/// <param name="fileName">导出到的pdf文件名</param>
/// <param name="picName">导出到pdf的图片名</param>
public static void ExportToPdf(string fileName, string picName)
{
Document document = new Document();
FileStream fs = System.IO.File.Open(fileName, FileMode.OpenOrCreate);
PdfWriter.GetInstance(document, fs);
document.Open();
BaseFont bfChinese = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12);
Image img = Image.GetInstance(picName);
document.Add(img);
Table aTable = new Table(1, 3);
aTable.Border = 0;
Cell cell = new Cell();
aTable.AddCell(new Paragraph(" 卡极度疯狂江户时代开发计划 \n ", fontChinese));
aTable.AddCell(new Paragraph("阿克苏叫的话发看见是大方好\n ", fontChinese));
document.Add(aTable);
aTable = new Table(2, 3);
cell = new Cell(new Paragraph("\n\n基础能力评估", fontChinese));
cell.Rowspan = 3;
cell.Border = 0;
aTable.AddCell(cell);
aTable.AddCell(new Paragraph("评分:" + "4.5 分\n ", fontChinese));
cell = new Cell(new Paragraph("描述:龙卡及是肯定发贺卡吉林省地方看见了哈深刻理解对方哈可是敦伦尽分贺卡老实交代回复刻录机啊是大方科技哈萨克断金零粉哈卡睡懒觉等会风口浪尖阿斯顿后付款\n ", fontChinese));
cell.Rowspan = 2;
aTable.AddCell(cell);
document.Add(aTable);
aTable = new Table(2, 3);
cell = new Cell(new Paragraph("\n\n阿里看见是大家发看见好", fontChinese));
cell.Rowspan = 3;
cell.Border = 0;
aTable.AddCell(cell);
aTable.AddCell(new Paragraph("评分:" + "3 分\n ", fontChinese));
cell = new Cell(new Paragraph("描述:龙卡及是肯定发贺卡吉林省地方看见了哈深刻理解对方哈可是敦伦尽分贺卡老实交代回复刻录机啊是大方科技哈萨克断金零粉哈卡睡懒觉等会风口浪尖阿斯顿后付款\n ", fontChinese));
cell.Rowspan = 2;
aTable.AddCell(cell);
document.Add(aTable);
aTable = new Table(2, 3);
cell = new Cell(new Paragraph("\n\n龙卡及地方看见", fontChinese));
cell.Rowspan = 3;
cell.Border = 0;
aTable.AddCell(cell);
aTable.AddCell(new Paragraph("评分:" + "4 分\n ", fontChinese));
cell = new Cell(new Paragraph("描述:龙卡及是肯定发贺卡吉林省地方看见了哈深刻理解对方哈可是敦伦尽分贺卡老实交代回复刻录机啊是大方科技哈萨克断金零粉哈卡睡懒觉等会风口浪尖阿斯顿后付款 \n ", fontChinese));
cell.Rowspan = 2;
aTable.AddCell(cell);
document.Add(aTable);
aTable = new Table(2, 3);
cell = new Cell(new Paragraph("\n\n可连接的说法:", fontChinese));
cell.Rowspan = 3;
cell.Border = 0;
aTable.AddCell(cell);
aTable.AddCell(new Paragraph("评分:" + "3.5 分\n ", fontChinese));
cell = new Cell(new Paragraph("描述:会风口浪尖阿可垃圾的附件哈开始就大方好可怜见哈师大开发计划撒打开了附件斯顿后付款 \n", fontChinese));
cell.Rowspan = 2;
aTable.AddCell(cell);
document.Add(aTable);
aTable = new Table(2, 1);
aTable.AddCell(new Paragraph("评审人:Prewin \n ", fontChinese));
aTable.AddCell(new Paragraph("评审时间:2008-10-23 \n ", fontChinese));
cell.Border = 0;
document.Add(aTable);
//备注
aTable = new Table(1, 1);
cell = new Cell(new Paragraph(" 备注 \n 会风口浪尖阿可垃圾的附件哈开始就大方好可kljhskjalhf ksajldfh看了就大方了哈师大看见阿拉沙德卡机是怜见哈师大开发计划撒打开了附件斯顿后付款 \n ", fontChinese));
aTable.AddCell(cell);
document.Add(aTable);
document.Close();
}
}
}