- 146
- 0
- 约9.35千字
- 约 13页
- 2021-11-16 发布于湖北
- 举报
用iTextSharp生成PDF文档,其实很简单
最近由于项目需要,在研究PDF文档的生成,发现用iTextSharp生成PDF文档时非常简单的一件事情;
之前在博客园中发现不少人都写了这方面的文章
),(
但是对于最新的版本没有更新,大部分代码是copy示例中的,导致我机器上没有办法运行,也许是版本问题吧,下面是我根据实际情况编写的代码,包含如下:
1、段落
2、表格
3、字体格式化
运行此代码需要的类库:
itextsharp.dll,itext-hyph-xml.dll,iTextAsian.dll和
ICSharpCode.SharpZipLib.dll itextsharp项目地址:
示例代码,/Files/hudonglin/PDFMake.rar示例代码也贴在下面了 本代码支持中文字符,下面直接上代码
namespace PDFMake
{
using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public class PDFITEXTSharp
{
private string _pdfSavePath;
/// summary
/// pdf文件保存地址
/// /summary
public string PDFSavePath
{
get
{
if (string.IsNullOrEmpty(_pdfSavePath))
{
return AppDomain.CurrentDomain.BaseDirectory +
Guid.NewGuid().ToString() + .pdf;
}
return _pdfSavePath;
}
set { _pdfSavePath = value; }
}
/// summary
/// 通过函数深圳pdf保存地址
/// /summary
/// param name=pdfSavePath/param
public void SetPDFSavePath(string pdfSavePath) { this.PDFSavePath = pdfSavePath; }
#region Demo region
/// summary
/// HelloWorld示例程序
/// /summary
public void HelloWorld()
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(PDFSavePath, FileMode.Create));
document.Open();
document.Add(new Paragraph(Hello World));
document.Close();
}
/// summary
/// PDF生成(重载)
/// /summary
/// param name=pdfSavePath/param
public void MakePDF(string pdfSavePath) { this.PDFSavePath = pdfSavePath; MakePDF(); }
/// summary
/// PDF生成
/// /summary
public void MakePDF()
{
Document document = new Document(PageSize.A4);//创建一个
Document实例
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PDFSavePath, FileMode.Create));//创建Writer实例
document.Open();
#region 写入一些数据,包括:作者、标题、摘要、关键词、创
建者、生产者、创建时间、头信息
document.AddAuthor(你的名字);
document.AddCreationDate();
document.AddCreator(公司名称);
document.AddHeader(QQ, 你的QQ);
document.AddHeader(Email, 您的邮箱);
document.AddKeywords(物联网);
document.AddProducer();
document.AddSubject(VIP观众入场凭券!);
document.AddTitle(标题);
原创力文档

文档评论(0)