詳解開(kāi)源免費(fèi)且穩(wěn)定實(shí)用的.NET PDF打印組件itextSharp(.NET組件介紹之八)
在這個(gè).NET組件的介紹系列中,受到了很多園友的支持,一些園友(如:數(shù)據(jù)之巔、 [秦時(shí)明月]等等這些大神 )也給我提出了對(duì)應(yīng)的建議,我正在努力去改正,有不足之處還望大家多多包涵。在傳播一些簡(jiǎn)單的知識(shí)的同時(shí),我自己也得到了一些提升,這個(gè)是我感覺(jué)到的最大的益處。知識(shí)需要傳播,在傳播的過(guò)程中去讓學(xué)習(xí)的人去提升,在交流中的過(guò)程中去讓思考的人去展望,我希望我也能在這個(gè)傳播的過(guò)程中出一份力。由于自身能力有限,在編寫(xiě)博文時(shí)出現(xiàn)的錯(cuò)誤和一些不到位的講解,還望大家多多見(jiàn)諒。
上面賣(mài)完情懷,下面就該切入正題了。
提到打印,恐怕對(duì)于很多人都不會(huì)陌生,無(wú)論是開(kāi)發(fā)者,還是非計(jì)算機(jī)專業(yè)的人員都會(huì)接觸到打印。對(duì)于項(xiàng)目開(kāi)發(fā)中使用到打印的地方會(huì)非常多,在.NET項(xiàng)目中,選擇打印的方式比較多,例如原始的IE網(wǎng)頁(yè)打印、水晶報(bào)表、JS插件實(shí)現(xiàn)打印、導(dǎo)出文檔打印,以及今天提到的使用itextSharp組件實(shí)現(xiàn)PDF打印等等。
在.NET中實(shí)現(xiàn)PDF打印的組件比較多,例如PDFsharp、Report.NET、sharpPDF、itextSharp等等,今天主要簡(jiǎn)單的介紹itextSharp組件。
一.itextSharp組件概述:
1.iText的是PDF庫(kù),它允許你創(chuàng)建,調(diào)整,檢查和維護(hù)的可移植文檔格式文件(PDF):
(1).基于從XML文件或數(shù)據(jù)庫(kù)中的數(shù)據(jù)生成文件和報(bào)告。
(2).創(chuàng)建地圖和書(shū)籍,利用眾多的互動(dòng)在PDF可用的功能。
(3).添加書(shū)簽,頁(yè)碼,水印等功能,以現(xiàn)有的PDF文件。
(4).從現(xiàn)有PDF文件拆分或連接頁(yè)面;填寫(xiě)交互式表單。
(5).即成動(dòng)態(tài)生成或操縱PDF文檔到Web瀏覽器。
iText所使用的的Java,.NET,Android和GAE開(kāi)發(fā)人員加強(qiáng)與PDF功能的應(yīng)用程序。iTextSharp的是.NET端口。
2.itextSharp的一些特征:
(1).PDF生成。
(2).PDF操作(沖壓水印,合并/拆分PDF文件,...)。
(3).PDF表單填寫(xiě)。
(4).XML功能。
(5).數(shù)字簽名。
以上是對(duì)itextSharp組件的一些特性的簡(jiǎn)單介紹,如果需要更加深入的了解itextSharp組件的相關(guān)信息,可以細(xì)致的查看API文檔和itextSharp產(chǎn)品介紹。https://sourceforge.net/projects/itextsharp/#overview。
二.itextSharp組件核心類和方法:
談到打印,在我們的項(xiàng)目中需要首先考慮的是我們需要打印的東西是什么。在大腦里面應(yīng)該首先有一個(gè)文檔的概念,在我們編程的過(guò)程中,“文檔”這個(gè)詞無(wú)處不在,這個(gè)可以是一個(gè)寬泛的概念,也可以是一個(gè)狹窄的概念,寬泛的“文檔”是指容器,用以存放一些元素;狹窄的“文檔”是指實(shí)際的文件類型。
對(duì)于打印的“文檔”,具體看一下寬泛的概念,文檔包含元素和節(jié)點(diǎn)等等。在組織打印的時(shí)候,我們需要?jiǎng)?chuàng)建文檔,寫(xiě)入元素和節(jié)點(diǎn)等信息,最后組合成為我們需要打印的內(nèi)容。itextSharp組件可以插入段落、表格、圖片等等信息,可以很方便的完成我們需要完成的功能。
Paragraph:報(bào)表中的文本;Image:報(bào)表中的圖片;PdfPTable:表格;PdfPCell:?jiǎn)卧瘛?/p>
1.Document類Open()方法:打開(kāi)文檔對(duì)象。
public virtual void Open() { if (!this.close) { this.open = true; } foreach (IDocListener listener in this.listeners) { listener.SetPageSize(this.pageSize); listener.SetMargins(this.marginLeft, this.marginRight, this.marginTop, this.marginBottom); listener.Open(); } }
以上的代碼可以看到,我們?cè)诖蜷_(kāi)文檔的時(shí)候,會(huì)設(shè)置文檔大小,文檔頁(yè)邊距等信息。
2.Paragraph類Add()方法:向段落添加元素。
public override bool Add(IElement o) { if (o is List) { List element = (List) o; element.IndentationLeft += this.indentationLeft; element.IndentationRight = this.indentationRight; base.Add(element); return true; } if (o is Image) { base.AddSpecial((Image) o); return true; } if (o is Paragraph) { base.Add(o); IList<Chunk> chunks = this.Chunks; if (chunks.Count > 0) { Chunk chunk = chunks[chunks.Count - 1]; base.Add(new Chunk("\n", chunk.Font)); } else { base.Add(Chunk.NEWLINE); } return true; } base.Add(o); return true; }
public interface IElement { // Methods bool IsContent(); bool IsNestable(); bool Process(IElementListener listener); string ToString(); // Properties IList<Chunk> Chunks { get; } int Type { get; } }
以上的add()方法是向段落添加元素,我們可以看到參數(shù)是個(gè)接口“IElement”,我們接下來(lái)看一下這個(gè)接口,接口主要元素是塊。我們看到在向段落添加元素時(shí),可以添加List,Image,Paragraph,Chunk。
3.Image.GetInstance()獲取圖片實(shí)例。
public static Image GetInstance(Image image) { if (image == null) { return null; } return (Image) image.GetType().GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(Image) }, null).Invoke(new object[] { image }); } public static Image GetInstance(byte[] imgb) { int num = imgb[0]; int num2 = imgb[1]; int num3 = imgb[2]; int num4 = imgb[3]; if (((num == 0x47) && (num2 == 0x49)) && (num3 == 70)) { GifImage image = new GifImage(imgb); return image.GetImage(1); } if ((num == 0xff) && (num2 == 0xd8)) { return new Jpeg(imgb); } if (((num == 0) && (num2 == 0)) && ((num3 == 0) && (num4 == 12))) { return new Jpeg2000(imgb); } if (((num == 0xff) && (num2 == 0x4f)) && ((num3 == 0xff) && (num4 == 0x51))) { return new Jpeg2000(imgb); } if (((num == PngImage.PNGID[0]) && (num2 == PngImage.PNGID[1])) && ((num3 == PngImage.PNGID[2]) && (num4 == PngImage.PNGID[3]))) { return PngImage.GetImage(imgb); } if ((num == 0xd7) && (num2 == 0xcd)) { return new ImgWMF(imgb); } if ((num == 0x42) && (num2 == 0x4d)) { return BmpImage.GetImage(imgb); } if ((((num == 0x4d) && (num2 == 0x4d)) && ((num3 == 0) && (num4 == 0x2a))) || (((num == 0x49) && (num2 == 0x49)) && ((num3 == 0x2a) && (num4 == 0)))) { RandomAccessFileOrArray s = null; try { s = new RandomAccessFileOrArray(imgb); Image tiffImage = TiffImage.GetTiffImage(s, 1); if (tiffImage.OriginalData == null) { tiffImage.OriginalData = imgb; } return tiffImage; } finally { if (s != null) { s.Close(); } } } throw new IOException(MessageLocalization.GetComposedMessage("the.byte.array.is.not.a.recognized.imageformat")); }
該方法根據(jù)參數(shù)獲取圖片實(shí)例的方式比較多,例如:Image,PdfTemplate,PRIndirectReference,byte[],Stream,string ,Uri等等,以上給出了根據(jù)Image和byte[]獲取ItextSharp的image實(shí)例。
4.Image的ScaleAbsolute():設(shè)置圖片信息。
public void ScaleAbsolute(float newWidth, float newHeight) { this.plainWidth = newWidth; this.plainHeight = newHeight; float[] matrix = this.Matrix; this.scaledWidth = matrix[6] - matrix[4]; this.scaledHeight = matrix[7] - matrix[5]; this.WidthPercentage = 0f; }
以上代碼可以看出,設(shè)置圖片的信息主要包括高度、寬度、排列等信息。
5.Anchor類的Process()方法:重寫(xiě)鏈接的處理方法。
public override bool Process(IElementListener listener) { try { bool flag = (this.reference != null) && this.reference.StartsWith("#"); bool flag2 = true; foreach (Chunk chunk in this.Chunks) { if (((this.name != null) && flag2) && !chunk.IsEmpty()) { chunk.SetLocalDestination(this.name); flag2 = false; } if (flag) { chunk.SetLocalGoto(this.reference.Substring(1)); } else if (this.reference != null) { chunk.SetAnchor(this.reference); } listener.Add(chunk); } return true; } catch (DocumentException) { return false; } }
以上方法可以看到,該方法是在本類中被重寫(xiě),用以處理鏈接的相關(guān)信息。
6.PageSize:設(shè)置紙張的類型。
public class PageSize { // Fields public static readonly Rectangle _11X17; public static readonly Rectangle A0; public static readonly Rectangle A1; public static readonly Rectangle A10; public static readonly Rectangle A2; public static readonly Rectangle A3; public static readonly Rectangle A4; public static readonly Rectangle A4_LANDSCAPE; public static readonly Rectangle A5; public static readonly Rectangle A6; public static readonly Rectangle A7; public static readonly Rectangle A8; public static readonly Rectangle A9; public static readonly Rectangle ARCH_A; public static readonly Rectangle ARCH_B; public static readonly Rectangle ARCH_C; public static readonly Rectangle ARCH_D; public static readonly Rectangle ARCH_E; public static readonly Rectangle B0; public static readonly Rectangle B1; public static readonly Rectangle B10; public static readonly Rectangle B2; public static readonly Rectangle B3; public static readonly Rectangle B4; public static readonly Rectangle B5; public static readonly Rectangle B6; public static readonly Rectangle B7; public static readonly Rectangle B8; public static readonly Rectangle B9; public static readonly Rectangle CROWN_OCTAVO; public static readonly Rectangle CROWN_QUARTO; public static readonly Rectangle DEMY_OCTAVO; public static readonly Rectangle DEMY_QUARTO; public static readonly Rectangle EXECUTIVE; public static readonly Rectangle FLSA; public static readonly Rectangle FLSE; public static readonly Rectangle HALFLETTER; public static readonly Rectangle ID_1; public static readonly Rectangle ID_2; public static readonly Rectangle ID_3; public static readonly Rectangle LARGE_CROWN_OCTAVO; public static readonly Rectangle LARGE_CROWN_QUARTO; public static readonly Rectangle LEDGER; public static readonly Rectangle LEGAL; public static readonly Rectangle LEGAL_LANDSCAPE; public static readonly Rectangle LETTER; public static readonly Rectangle LETTER_LANDSCAPE; public static readonly Rectangle NOTE; public static readonly Rectangle PENGUIN_LARGE_PAPERBACK; public static readonly Rectangle PENGUIN_SMALL_PAPERBACK; public static readonly Rectangle POSTCARD; public static readonly Rectangle ROYAL_OCTAVO; public static readonly Rectangle ROYAL_QUARTO; public static readonly Rectangle SMALL_PAPERBACK; public static readonly Rectangle TABLOID; // Methods static PageSize(); public PageSize(); public static Rectangle GetRectangle(string name); }
以上的類中,我們可以看到我們可以設(shè)置需要打印的紙張類型,根據(jù)實(shí)際情況可以選擇。在最下面我們看到了兩種方法,一個(gè)是PageSize()設(shè)置紙張大小,一個(gè)是GetRectangle()繪制矩形。
以上是對(duì)itextSharp組件的一些類和方法的簡(jiǎn)單介紹,對(duì)于表格,單元格等等類的介紹就不再繼續(xù),有興趣的可以自己查看源代碼信息。
三.itextSharp組件實(shí)例:
上面介紹了itextSharp組件的背景、特性,以及組件的核心類和方法,在這里給出一個(gè)簡(jiǎn)單的itextSharp組件操作的實(shí)例,這個(gè)實(shí)例只是一個(gè)簡(jiǎn)單的介紹。
/// <summary> /// 字體 /// </summary> private Font _font; /// <summary> /// 文檔大小 /// </summary> private Rectangle _rect; /// <summary> /// 文檔對(duì)象 /// </summary> private readonly Document _document; /// <summary> /// 基礎(chǔ)字體 /// </summary> private BaseFont _basefont; /// <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) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(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) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(type); } SetPageSize(type); _document = new Document(_rect, marginLeft, marginRight, marginTop, marginBottom); } /// <summary> /// 設(shè)置字體 /// </summary> public void SetBaseFont(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(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); } /// <summary> /// 設(shè)置頁(yè)面大小 /// </summary> /// <param name="type">頁(yè)面大小(如"A4")</param> public void SetPageSize(string type) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(type); } switch (type.Trim()) { //枚舉需要的文檔紙張大小 case "A3": _rect = PageSize.A3; break; case "A4": _rect = PageSize.A4; break; case "A8": _rect = PageSize.A8; break; } } /// <summary> /// 實(shí)例化文檔 /// </summary> /// <param name="os">文檔相關(guān)信息(如路徑,打開(kāi)方式等)</param> public void GetInstance(Stream os) { if (os == null) { throw new ArgumentNullException("os"); } PdfWriter.GetInstance(_document, os); } /// <summary> /// 打開(kāi)文檔對(duì)象 /// </summary> /// <param name="os">文檔相關(guān)信息(如路徑,打開(kāi)方式等)</param> public void Open(Stream os) { if (os == null) { throw new ArgumentNullException("os"); } GetInstance(os); _document.Open(); } /// <summary> /// 關(guān)閉打開(kāi)的文檔 /// </summary> public void Close() { _document.Close(); } /// <summary> /// 添加段落 /// </summary> /// <param name="content">內(nèi)容</param> /// <param name="fontsize">字體大小</param> public void AddParagraph(string content, float fontsize) { SetFont(fontsize); var 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); var pra = new Paragraph(content, _font) { Alignment = alignment }; if (spacingAfter != 0) { pra.SpacingAfter = spacingAfter; } if (spacingBefore != 0) { pra.SpacingBefore = spacingBefore; } if (multipliedLeading != 0) { pra.MultipliedLeading = multipliedLeading; } _document.Add(pra); } /// <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) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } var img = Image.GetInstance(path); img.Alignment = alignment; // ReSharper disable once CompareOfFloatsByEqualityOperator 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); } /// <summary> /// 添加鏈接 /// </summary> /// <param name="content">鏈接文字</param> /// <param name="fontSize">字體大小</param> /// <param name="reference">鏈接地址</param> public void AddAnchorReference(string content, float fontSize, string reference) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } SetFont(fontSize); var auc = new Anchor(content, _font) { 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) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } SetFont(fontSize); var auc = new Anchor(content, _font) { Name = name }; _document.Add(auc); }
以上的實(shí)例比較的簡(jiǎn)單,主要是用作簡(jiǎn)單介紹組件的用法。如果需要將組件設(shè)計(jì)的更加通用,我們可以將組件的相關(guān)類和方法重寫(xiě),并且可以開(kāi)發(fā)一套cs或者bs程序,實(shí)現(xiàn)組件的圖形化操作,圖形化操作生成文件模板。文件模板可以將相關(guān)信息序列化(json或者二進(jìn)制),在項(xiàng)目中直接加載模型,并將數(shù)據(jù)綁定在模板中,實(shí)現(xiàn)pdf打印的動(dòng)態(tài)配置。
這個(gè)程序的開(kāi)發(fā)難度一般,如果有興趣的可以自行開(kāi)發(fā)一套工具,可以更好的實(shí)現(xiàn)我們的項(xiàng)目pdf打印功能。
四.總結(jié):
上面介紹了itextSharp組件的相關(guān)信息,在這個(gè)系列的組件介紹中,對(duì)于組件的介紹都是比較的簡(jiǎn)單,旨在向大家介紹這個(gè)組件,在實(shí)際的開(kāi)發(fā)中,我們可以根據(jù)實(shí)際情況自行選擇相應(yīng)的組件,組件沒(méi)有絕對(duì)的好壞,只有合適的場(chǎng)景。
以上講解若有錯(cuò)誤和不足之處,希望大家多多見(jiàn)諒和多多提出意見(jiàn)和建議。也希望大家多多支持腳本之家。
- .NET 開(kāi)源配置組件 AgileConfig的使用簡(jiǎn)介
- ASP.NET開(kāi)源導(dǎo)入導(dǎo)出庫(kù)Magicodes.IE完成Csv導(dǎo)入導(dǎo)出的方法
- 詳解一款開(kāi)源免費(fèi)的.NET文檔操作組件DocX(.NET組件介紹之一)
- 詳解免費(fèi)開(kāi)源的DotNet二維碼操作組件ThoughtWorks.QRCode(.NET組件介紹之四)
- 詳解免費(fèi)開(kāi)源的.NET多類型文件解壓縮組件SharpZipLib(.NET組件介紹之七)
- 詳解最好的.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip(.NET組件介紹之三)
- 詳解免費(fèi)開(kāi)源的DotNet任務(wù)調(diào)度組件Quartz.NET(.NET組件介紹之五)
- .NET中開(kāi)源文檔操作組件DocX的介紹與使用
- 基于.NET平臺(tái)常用的框架和開(kāi)源程序整理
- .NET 開(kāi)源項(xiàng)目Polly的簡(jiǎn)單介紹
相關(guān)文章
ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
這篇文章主要介紹了ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06net insert into語(yǔ)法錯(cuò)誤詳解
本文說(shuō)下net insert into語(yǔ)法錯(cuò)誤的原理和解決方法2013-11-11c# static 靜態(tài)數(shù)據(jù)成員
靜態(tài)成員屬于類所有,為各個(gè)類的實(shí)例所公用,無(wú)論類創(chuàng)建了幾多實(shí)例,類的靜態(tài)成員在內(nèi)存中只占同一塊區(qū)域。2009-06-06ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理
ASP.NET2.0服務(wù)器控件之自定義狀態(tài)管理...2006-09-09云服務(wù)器下搭建ASP.NET Core環(huán)境
本文給大家分享的是在云服務(wù)器上搭建ASP.NET Core環(huán)境以及成功運(yùn)行官網(wǎng)DEMO的教程,十分的細(xì)致全面,有需要的小伙伴可以參考下。2016-07-07ASP.NET Core SignalR中的流式傳輸深入講解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core SignalR中流式傳輸?shù)南嚓P(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11asp.net XML文件操作實(shí)現(xiàn)代碼
這幾天在項(xiàng)目中用到了XML文件配置存儲(chǔ)一些基本信息,如:參數(shù)、表格等一些信息存儲(chǔ)。由于記錄不是很多,所以用此文件來(lái)代替數(shù)據(jù)庫(kù)中設(shè)計(jì)的表結(jié)構(gòu)。2009-12-12