C#使用iTextSharp封裝的PDF文件操作類(lèi)實(shí)例
本文實(shí)例講述了C#使用iTextSharp封裝的PDF文件操作類(lèi)。分享給大家供大家參考。具體分析如下:
這個(gè)C#代碼主要講iTextSharp中用于操作PDF文件的方法進(jìn)行了再次封裝,可以更加方便的訪問(wèn)PDF文檔,可以動(dòng)態(tài)生成PDF文件、添加內(nèi)容、設(shè)置段落、設(shè)置字體等。
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DotNet.Utilities
{
/// <summary>
/// PDF文檔操作類(lèi)
/// </summary>
//------------------調(diào)用--------------------------
//PDFOperation pdf = new PDFOperation();
//pdf.Open(new FileStream(path, FileMode.Create));
//pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
//pdf.AddParagraph("測(cè)試文檔(生成時(shí)間:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
//pdf.Close();
//-------------------------------
public class PDFOperation
{
#region 構(gòu)造函數(shù)
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public PDFOperation()
{
rect = PageSize.A4;
document = new Document(rect);
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="type">頁(yè)面大小(如"A4")</param>
public PDFOperation(string type)
{
SetPageSize(type);
document = new Document(rect);
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="type">頁(yè)面大小(如"A4")</param>
/// <param name="marginLeft">內(nèi)容距左邊框距離</param>
/// <param name="marginRight">內(nèi)容距右邊框距離</param>
/// <param name="marginTop">內(nèi)容距上邊框距離</param>
/// <param name="marginBottom">內(nèi)容距下邊框距離</param>
public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
SetPageSize(type);
document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
}
#endregion
#region 私有字段
private Font font;
private Rectangle rect; //文檔大小
private Document document;//文檔對(duì)象
private BaseFont basefont;//字體
#endregion
#region 設(shè)置字體
/// <summary>
/// 設(shè)置字體
/// </summary>
public void SetBaseFont(string path)
{
basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
/// <summary>
/// 設(shè)置字體
/// </summary>
/// <param name="size">字體大小</param>
public void SetFont(float size)
{
font = new Font(basefont, size);
}
#endregion
#region 設(shè)置頁(yè)面大小
/// <summary>
/// 設(shè)置頁(yè)面大小
/// </summary>
/// <param name="type">頁(yè)面大小(如"A4")</param>
public void SetPageSize(string type)
{
switch (type.Trim())
{
case "A4":
rect = PageSize.A4;
break;
case "A8":
rect = PageSize.A8;
break;
}
}
#endregion
#region 實(shí)例化文檔
/// <summary>
/// 實(shí)例化文檔
/// </summary>
/// <param name="os">文檔相關(guān)信息(如路徑,打開(kāi)方式等)</param>
public void GetInstance(Stream os)
{
PdfWriter.GetInstance(document, os);
}
#endregion
#region 打開(kāi)文檔對(duì)象
/// <summary>
/// 打開(kāi)文檔對(duì)象
/// </summary>
/// <param name="os">文檔相關(guān)信息(如路徑,打開(kāi)方式等)</param>
public void Open(Stream os)
{
GetInstance(os);
document.Open();
}
#endregion
#region 關(guān)閉打開(kāi)的文檔
/// <summary>
/// 關(guān)閉打開(kāi)的文檔
/// </summary>
public void Close()
{
document.Close();
}
#endregion
#region 添加段落
/// <summary>
/// 添加段落
/// </summary>
/// <param name="content">內(nèi)容</param>
/// <param name="fontsize">字體大小</param>
public void AddParagraph(string content, float fontsize)
{
SetFont(fontsize);
Paragraph pra = new Paragraph(content, font);
document.Add(pra);
}
/// <summary>
/// 添加段落
/// </summary>
/// <param name="content">內(nèi)容</param>
/// <param name="fontsize">字體大小</param>
/// <param name="Alignment">對(duì)齊方式(1為居中,0為居左,2為居右)</param>
/// <param name="SpacingAfter">段后空行數(shù)(0為默認(rèn)值)</param>
/// <param name="SpacingBefore">段前空行數(shù)(0為默認(rèn)值)</param>
/// <param name="MultipliedLeading">行間距(0為默認(rèn)值)</param>
public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
{
SetFont(fontsize);
Paragraph pra = new Paragraph(content, font);
pra.Alignment = Alignment;
if (SpacingAfter != 0)
{
pra.SpacingAfter = SpacingAfter;
}
if (SpacingBefore != 0)
{
pra.SpacingBefore = SpacingBefore;
}
if (MultipliedLeading != 0)
{
pra.MultipliedLeading = MultipliedLeading;
}
document.Add(pra);
}
#endregion
#region 添加圖片
/// <summary>
/// 添加圖片
/// </summary>
/// <param name="path">圖片路徑</param>
/// <param name="Alignment">對(duì)齊方式(1為居中,0為居左,2為居右)</param>
/// <param name="newWidth">圖片寬(0為默認(rèn)值,如果寬度大于頁(yè)寬將按比率縮放)</param>
/// <param name="newHeight">圖片高</param>
public void AddImage(string path, int Alignment, float newWidth, float newHeight)
{
Image img = Image.GetInstance(path);
img.Alignment = Alignment;
if (newWidth != 0)
{
img.ScaleAbsolute(newWidth, newHeight);
}
else
{
if (img.Width > PageSize.A4.Width)
{
img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
}
}
document.Add(img);
}
#endregion
#region 添加鏈接、點(diǎn)
/// <summary>
/// 添加鏈接
/// </summary>
/// <param name="Content">鏈接文字</param>
/// <param name="FontSize">字體大小</param>
/// <param name="Reference">鏈接地址</param>
public void AddAnchorReference(string Content, float FontSize, string Reference)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Reference = Reference;
document.Add(auc);
}
/// <summary>
/// 添加鏈接點(diǎn)
/// </summary>
/// <param name="Content">鏈接文字</param>
/// <param name="FontSize">字體大小</param>
/// <param name="Name">鏈接點(diǎn)名</param>
public void AddAnchorName(string Content, float FontSize, string Name)
{
SetFont(FontSize);
Anchor auc = new Anchor(Content, font);
auc.Name = Name;
document.Add(auc);
}
#endregion
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
本文主要介紹了C#中ftp檢測(cè)目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#程序啟動(dòng)項(xiàng)的設(shè)置方法
這篇文章主要為大家詳細(xì)介紹了C#程序啟動(dòng)項(xiàng)的設(shè)置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
C#學(xué)習(xí)進(jìn)階Hello World的17種寫(xiě)法代碼分享
本文針對(duì)不同階段、不同程度的C#學(xué)習(xí)者,介紹了C# Hello World的17種不同寫(xiě)法,C# Hello World寫(xiě)法入門(mén)、C# Hello World寫(xiě)法進(jìn)階、C# Hello World的特別寫(xiě)法三種角度進(jìn)行推進(jìn)2013-12-12
基于WPF實(shí)現(xiàn)簡(jiǎn)單的下拉篩選控件
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡(jiǎn)單的下拉篩選控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2023-04-04
C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解
這篇文章主要介紹了C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
C#數(shù)據(jù)結(jié)構(gòu)之隊(duì)列(Quene)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之隊(duì)列(Quene),結(jié)合實(shí)例形式較為詳細(xì)的講述了隊(duì)列的功能、原理與C#實(shí)現(xiàn)隊(duì)列的相關(guān)技巧,需要的朋友可以參考下2015-11-11
C# menuStrip控件實(shí)現(xiàn)鼠標(biāo)滑過(guò)自動(dòng)彈出功能
MenuStrip 控件是 Visual Studio 和 .NET Framework 中的功能。使用該控件,可以輕松創(chuàng)建 Microsoft Office 中那樣的菜單。本文給大家分享menuStrip鼠標(biāo)滑過(guò)自動(dòng)彈出效果2021-07-07

