C#生成PDF总结
(一)C#生成PDF总结
(1)iTextSharp控件对iTextSharp研究还可以表格、文字、各种GDI对象,图片,水印,文字旋转
(2)aspose的控件
(3)PDF Library这个类库(只单纯是有文字的,表格和文字)http://www.codeproject.com/KB/dotnet/PdfLibrary.aspx
(4)直接用.net的RDLC report 就可以啦,to PDF效果很好,也可以对付用户有变数,可以to 其他格式.
(二)iTextSharp生成PDF示列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO; namespace PdfDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 我得第一个Pdf程序
/// </summary>
private void CreatePdf()
{
string fileName = string.Empty;
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "我的第一个PDF";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph("Hello World");
document.Add(paragraph);
document.Close();
}//end if
}
/// <summary>
/// 设置页面大小、作者、标题等相关信息设置
/// </summary>
private void CreatePdfSetInfo()
{
string fileName = string.Empty;
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "我的第一个PDF";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
//设置页面大小
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);
pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);
//设置边界
Document document = new Document(pageSize, 36f, 72f, 108f, 180f);
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
// 添加文档信息
document.AddTitle("PDFInfo");
document.AddSubject("Demo of PDFInfo");
document.AddKeywords("Info, PDF, Demo");
document.AddCreator("SetPdfInfoDemo");
document.AddAuthor("焦涛");
document.Open();
// 添加文档内容
for (int i = 0; i < 5; i++)
{
document.Add(new iTextSharp.text.Paragraph("Hello World! Hello People! " +
"Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));
}
document.Close();
}//end if
}
/// <summary>
/// 创建多个Pdf新页
/// </summary>
private void CreateNewPdfPage()
{
string fileName = string.Empty;
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "创建多个Pdf新页";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
Document document = new Document(PageSize.NOTE);
PdfWriter writer= PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
// 第一页
document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
// 添加新页面
document.NewPage();
// 第二页
// 添加第二页内容
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
// 添加新页面
document.NewPage();
// 第三页
// 添加新内容
document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
// 重新开始页面计数
document.ResetPageCount();
// 新建一页
document.NewPage();
// 第四页
// 添加第四页内容
document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
document.Close();
}//end if
}
/// <summary>
/// 生成图片pdf页(pdf中插入图片)
/// </summary>
public void ImageDirect()
{
string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.jpg"; //临时文件路径
string fileName = string.Empty;
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "我的第一个PDF";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
img.SetAbsolutePosition((PageSize.POSTCARD.Width - img.ScaledWidth) / 2, (PageSize.POSTCARD.Height - img.ScaledHeight) / 2);
writer.DirectContent.AddImage(img);
iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph("Foobar Film Festival", new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 22f));
p.Alignment = Element.ALIGN_CENTER;
document.Add(p);
document.Close();
}//end if
}
private void ReadPdf()
{
Console.WriteLine("读取PDF文档");
try
{
// 创建一个PdfReader对象
PdfReader reader = new PdfReader(@"D:\技术文档\sj\C#线程参考手册.pdf");
// 获得文档页数
int n = reader.NumberOfPages;
// 获得第一页的大小
iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
// 创建一个文档变量
Document document = new Document(psize, 50, 50, 50, 50);
// 创建该文档
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Read.pdf", FileMode.Create));
// 打开文档
document.Open();
// 添加内容
PdfContentByte cb = writer.DirectContent;
int i = 0;
int p = 0;
Console.WriteLine("一共有 " + n + " 页.");
while (i < n)
{
document.NewPage();
p++;
i++;
PdfImportedPage page1 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);
Console.WriteLine("处理第 " + i + " 页");
if (i < n)
{
i++;
PdfImportedPage page2 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2);
Console.WriteLine("处理第 " + i + " 页");
}
if (i < n)
{
i++;
PdfImportedPage page3 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0);
Console.WriteLine("处理第 " + i + " 页");
}
if (i < n)
{
i++;
PdfImportedPage page4 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0);
Console.WriteLine("处理第 " + i + " 页");
}
cb.SetRGBColorStroke(255, 0, 0);
cb.MoveTo(0, height / 2);
cb.LineTo(width, height / 2);
cb.Stroke();
cb.MoveTo(width / 2, height);
cb.LineTo(width / 2, 0);
cb.Stroke();
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.BeginText();
cb.SetFontAndSize(bf, 14);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0 ? 1 : 0)), width / 2, 40, 0);
cb.EndText();
}
// 关闭文档
document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message);
Console.Error.WriteLine(de.StackTrace);
}
} /// <summary>
/// 创建表格
/// </summary>
public void CreateFirstTable()
{
string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.pm"; //临时文件路径
string fileName = string.Empty;
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "我的第一个PDF";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell=new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
document.Add(table);
document.Close();
}//end if
} private void button1_Click(object sender, RoutedEventArgs e)
{
//CreatePdf();
//CreatePdfPageSize();
CreateNewPdfPage();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
CreateFirstTable();
} private void button3_Click(object sender, RoutedEventArgs e)
{
ImageDirect();
} private void button4_Click(object sender, RoutedEventArgs e)
{
ReadPdf();
}
}
}

(三)代码下载
(三)参考链接
http://www.cnbeta.com/articles/60484.htm 在线导出PDF的好去处
http://bbs.csdn.net/topics/310095053 PDF导出的讨论
http://www.cnblogs.com/EKPK/archive/2009/06/04/1495867.html 用C#制作PDF文件全攻略
http://blog.csdn.net/aasswwe/article/details/7639768
http://blog.sina.com.cn/s/blog_82662ce70100t0s6.html Pdf常见用法
http://www.tuicool.com/articles/nuyAFz HTML生成PDF(c#)
http://stackoverflow.com/questions/tagged/itextsharp itextsharp相关问题
http://www.itextpdf.com/book/examples.php 官方文档,虽然是Java版本的但类库略有不同,在java中一些getFunction和setFunction在C#转为属性,可以作为参考文档。
C#生成PDF总结的更多相关文章
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- html 生成pdf
HTML生成PDF(c#) 最近因为工作需要,小小的研究了一下HTML生成PDF的方法,这方面的内容很多,但要么是不尽如人意的方法,要么就是那种收费的类库!为了广大.neter的福利,把自己的一点小小 ...
- iTextSharp生成pdf的一个简单例子
效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...
- 生成 PDF 全攻略【2】在已有PDF上添加内容
项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...
- PHP 生成PDF
一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...
- 用js生成PDF的方案
在java里,我们常用Itext来生成pdf,在pdf文件里组合图片,文字,画表格,画线等操作,还会遇到中文支持的问题. 那好,现在想直接在web前端就生成pdf怎么办,目前有以下几个解决方案 1:J ...
- 使用TCPDF插件生成pdf以及pdf的中文处理
目录(?)[+] 多种多样的pdf开发库 WKHTMLTOPDF 2FPDF 3TCPDF 中文问题 做了这么多年项目,以前只是在别人的项目中了解过PHP生成pdf文件,知道并不难,但是涉及到了p ...
- linux下编译bib、tex生成pdf文件
实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...
- 【Java】使用iText生成PDF文件
iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- 生成PDF的新选择-Phantomjs
最近在node.js项目开发中,遇见生成PDF的需求,当然生成PDF不是一个新意的需求:我可以选择利用开源的pdfkit或者其他node pdf模块,或者通过edge.js调用.net/python下 ...
随机推荐
- IFC
IFC是设计师使用的软件,然后存储的格式. 这个适用于精细的设计.
- SQL语句简介
1.Top.Distinct Top 获取前几条数据,top一般都与order by连用 获得年纪最小的5个学生 select top 5 * from A order by classesId 获 ...
- MVC公开课 – 1.基础 (2013-3-15广州传智MVC公开课)
1.MVC设计模式 Model 是指 要处理的业务代码和数据操作代码 View 视图 主要是指的 跟用户打交道 并能够展示数据 Controller 看成是 Model和View的桥梁 优点: 1 ...
- Java代码实现excel数据导入到Oracle
1.首先需要两个jar包jxl.jar,ojdbc.jar(注意版本,版本不合适会报版本错误)2.代码: Java代码 import java.io.File; import java.io.Fi ...
- 攻城狮在路上(壹) Hibernate(七)--- 通过Hibernate操纵对象(下)
一.与触发器协同工作: 当Hibernate与数据库的触发器协同工作时,会出现以下两类问题: 1.触发器使Session缓存中的数据和数据库中的不一致: 出现此问题的原因是触发器运行在数据库内,它执行 ...
- 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)
原文 http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...
- php调用phpqrcode.php生成二维码
下载phpqrcode.php 下载地址: http://files.cnblogs.com/files/qhorse/phpqrcode.rar qrcode.php文件: <?php inc ...
- C字符数组赋值(转)
举例如下: char a[10];1.定义的时候直接用字符串赋值char a[10]="hello";注意:不能先定义再给它赋值,如 char a[10]; a[10]=" ...
- 1.CoreLocation 使用,获取当前位置
1. ios7只要开始定位,系统就会自动要求你对应用程序授权 ios8之后,必须要代码中实现要求用户对应用程序授权 ,在plist中添加以下两个属性 NSLocationWhenInUseDescri ...
- input实时监控和获取焦点的问题,oninput,ononfocus
1.input监控实时输入问题,google浏览器使用oninput,其他浏览器(IE6/7/8)使用onpropertychange var ie = !!window.ActiveXObject; ...