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();
}
}
}
分享到:
相关推荐
PDF文件是目前比较流行的电子文档格式,在办公自动化(OA)等软件的开发中,经常要用到该格式,但介绍如 何制作PDF格式文件的资料非常少,在网上搜来搜去,都转贴的是同一段“暴力”破解的方法
C#导出PDF案例,导出PDF和拼HTML表格一样简单。
改例子可以将C# 数据集 数据导出pdf 也可以将代码提取出代码 放入 winfrom程序中 仅供参考
C#导出PDF 工具类,功能齐全,此版本符合网上大部分教程源码功能。
C#导出PDF格式
C#程序将查询结果,表格等信息导出成Excel或者导出PDF格式文档
C# NPOI导出Excel,Words转PDF,无水印Aspose.Words,Words模版导出PDF
用wkhtmltopdf把网页导为PDF文件,代码测试过了,下载用VS2010可以直接运行使用。
主要给大家介绍了关于C#导出pdf的实现方法,实现后浏览器不预览就可以直接下载,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
c# winform Itext 实现PDF导出简单demo,适合初学者学习
C# 生成PDF源码,如何导入图片 定位 中文英文文本定位 布局
C# 导入excel 导出excel 导出pdf格式 添加图片导出pdf 导出pdf格式 添加图片导出pdf
C#版本Itextsharp根据模板导出pdf报表
c#导出excel,word,pdf代码,实现将图片导出到excel,word和pdf中。
利用c#生成pdf文件,比较实用
基于C#语言WPF框架下,引用itextsharp.dll来实现
winform中将Datatable数据导出到PDF文件中,该项目包含了例子,能使个人能更加了解winform将datatable数据导出到PDF的整个流程,本资源也可拿来即用,只需要稍加修改即可。
C#.net导出PDF文件全攻略(包含全部所需dll文件)
C# asp.net html 导出 pdf 打印 引用 wkhtmltopdf.exe,简单明了,不用编译那么多代码,直接调用wkhtmltopdf.exe把html格式的导出pdf ,经过本人测试时ok的!