(一)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总结的更多相关文章

  1. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  2. html 生成pdf

    HTML生成PDF(c#) 最近因为工作需要,小小的研究了一下HTML生成PDF的方法,这方面的内容很多,但要么是不尽如人意的方法,要么就是那种收费的类库!为了广大.neter的福利,把自己的一点小小 ...

  3. iTextSharp生成pdf的一个简单例子

    效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...

  4. 生成 PDF 全攻略【2】在已有PDF上添加内容

    项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...

  5. PHP 生成PDF

    一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...

  6. 用js生成PDF的方案

    在java里,我们常用Itext来生成pdf,在pdf文件里组合图片,文字,画表格,画线等操作,还会遇到中文支持的问题. 那好,现在想直接在web前端就生成pdf怎么办,目前有以下几个解决方案 1:J ...

  7. 使用TCPDF插件生成pdf以及pdf的中文处理

    目录(?)[+] 多种多样的pdf开发库 WKHTMLTOPDF 2FPDF 3TCPDF 中文问题   做了这么多年项目,以前只是在别人的项目中了解过PHP生成pdf文件,知道并不难,但是涉及到了p ...

  8. linux下编译bib、tex生成pdf文件

    实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...

  9. 【Java】使用iText生成PDF文件

    iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  10. 生成PDF的新选择-Phantomjs

    最近在node.js项目开发中,遇见生成PDF的需求,当然生成PDF不是一个新意的需求:我可以选择利用开源的pdfkit或者其他node pdf模块,或者通过edge.js调用.net/python下 ...

随机推荐

  1. Redis事件管理(二)

    Redis的定时器是自己实现的,不是很复杂.说说具体的实现吧. 定时器的存储维护采用的是普通的单向链表结构,具体节点定义为: /*时间定时器结构体*/ typedef struct aeTimeEve ...

  2. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  3. Mysql之INFORMATION_SCHEMA解析1

    INFORMATION_SCHEMA库是Mysql提供的一个系统库,保存了数据库的原数据,方便用户监控与管理Msyql. 现在单说与INNODB相关的库:INNODB_SYS_TABLES,INNOD ...

  4. db2 bind on luw

    https://www.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.embed.doc/doc/c000556 ...

  5. TIME_WAIT连接过多解决办法

    问题起因: 自己开发了一个服务器和客户端,通过短连接的方式来进行通讯,由于过于频繁的创建连接,导致系统连接数量被占用,不能及时释放.看了一下18888,当时吓到了. 现象: 1.外部机器不能正常连接S ...

  6. KMP算法学习

    kmp算法完成的任务是:给定两个字符串O和f,长度分别为n和m,判断f是否在O中出现,如果出现则返回出现的位置.常规方法是遍历a的每一个位置,然后从该位置开始和b进行匹配,但是这种方法的复杂度是O(n ...

  7. React Native官方DEMO

    官方给我们提供了UIExplorer项目,这里边包含React Native的基本所有组件的使用介绍和方法. 运行官方DEMO步骤如下 安装react native环境 React Native项目源 ...

  8. MVC缓存03,扩展方法实现视图缓存

    关于缓存,先前尝试了: ● 在"MVC缓存01,使用控制器缓存或数据层缓存"中,分别在控制器和Data Access Layer实现了缓存 ● 在"MVC缓存02,使用数 ...

  9. 栈与队列:refresh的停车场

    数据结构实验之队列一:排队买饭 Time Limit: 1000MS Memory limit: 65536K 题目描述 中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接 ...

  10. CQRS及.NET中的参考资料

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:CQRS作为一种设计模式,其实一点都不新鲜了.不过今天有朋友感叹.NET朋友也关注CQ ...