ITextSharp生成PDF
ITextSharp就不多介紹了,下面就把遇到的坑一一記錄下來,希望能夠幫助到正在使用它的開發(fā)者們。操作pdf的方法都被作者封裝好了,只是沒有注釋和說明,不過大部分的方法屬性還是能看懂的,看不懂的可以反編譯一下。
gethub下載dll地址:https://github.com/itext/itextsharp/tags
1.輸入文字不顯示中文,文字換行
2.文字加顏色、字體大小、加粗、斜體、居中等騷操作
3.表格行合并、表格列合并
4.添加新頁(yè)面
5.圖片等比縮放、頁(yè)面中心顯示
下面代碼演示:
首先添加幾個(gè)dll
using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; private void SavePDF(HttpContext context){Document document = new Document();//中文字體string chinese = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "KAIU.TTF");BaseFont baseFont = BaseFont.CreateFont(chinese, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//文字大小12,文字樣式Font cn = new Font(baseFont, 12, Font.NORMAL);PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"D:\temp.pdf", FileMode.Create));document.Open();//最后一個(gè)參數(shù)是顏色,這里可以是rgb格式,也可以是默認(rèn)定義的var title = new Paragraph("\n 這是一條標(biāo)題0.0 hello ", new Font(baseFont, 14, Font.BOLD, BaseColor.RED));//居中title.Alignment = Element.ALIGN_CENTER;document.Add(title);Paragraph p = new Paragraph(" \n this is Second title \n ", cn);//Phrase p = new Phrase("這是一條標(biāo)題0.0 hello", cn);p.Alignment = Element.ALIGN_CENTER;document.Add(p);//添加表格PdfPTable table = new PdfPTable(3);PdfPCell cell = new PdfPCell();table.AddCell("Row");cell = new PdfPCell(new Phrase("Cell"));cell.Colspan = 2;table.AddCell(cell);table.AddCell("row");cell = new PdfPCell(new Phrase("Cell"));cell.Colspan = 2;table.AddCell(cell);cell = new PdfPCell(new Phrase("Row"));cell.Rowspan = 2;table.AddCell(cell);table.AddCell("Cell");table.AddCell("Cell");table.AddCell("Cell");table.AddCell("Cell");table.HorizontalAlignment = Element.ALIGN_CENTER;document.Add(table);//新頁(yè)面document.NewPage();document.Add(new Paragraph("Second page pic", cn));Image img = Image.GetInstance("E:/VsTest/testWeb/testWeb/Files/ts20171204.002.jpeg");float percentage = 1;//這里都是圖片最原始的寬度與高度 float resizedWidht = img.Width;float resizedHeight = img.Height;//這時(shí)判斷圖片寬度是否大于頁(yè)面寬度減去也邊距,如果是,那么縮小,如果還大,繼續(xù)縮小, //這樣這個(gè)縮小的百分比percentage會(huì)越來越小 while (resizedWidht > (document.PageSize.Width - document.LeftMargin - document.RightMargin) * 0.8){percentage = percentage * 0.9f;resizedHeight = img.Height * percentage;resizedWidht = img.Width * percentage;}while (resizedHeight > (document.PageSize.Height - document.TopMargin - document.BottomMargin) * 0.8){percentage = percentage * 0.9f;resizedHeight = img.Height * percentage;resizedWidht = img.Width * percentage;}//這里用計(jì)算出來的百分比來縮小圖片 img.ScalePercent(percentage * 100);//圖片定位,頁(yè)面總寬283,高416;這里設(shè)置0,0的話就是頁(yè)面的左下角 讓圖片的中心點(diǎn)與頁(yè)面的中心店進(jìn)行重合 img.SetAbsolutePosition(document.PageSize.Width / 2 - resizedWidht / 2, document.PageSize.Height / 2 - resizedHeight / 2);writer.DirectContent.AddImage(img);document.Close();}最后看看效果
下面是該dll里面的字體和顏色集合
//public const int NORMAL = 0;//public const int BOLD = 1;//public const int ITALIC = 2;//public const int UNDERLINE = 4;//public const int STRIKETHRU = 8;//public const int BOLDITALIC = 3;//public const int UNDEFINED = -1;//public const int DEFAULTSIZE = 12;//public static readonly BaseColor WHITE;//public static readonly BaseColor BLUE;//public static readonly BaseColor CYAN;//public static readonly BaseColor MAGENTA;//public static readonly BaseColor GREEN;//public static readonly BaseColor ORANGE;//public static readonly BaseColor YELLOW;//public static readonly BaseColor RED;//public static readonly BaseColor BLACK;//public static readonly BaseColor DARK_GRAY;//public static readonly BaseColor GRAY;//public static readonly BaseColor LIGHT_GRAY;//public static readonly BaseColor PINK;這里說說表格里面的PdfPTable,這個(gè)東西只能初始化他的列,表格里面add的時(shí)候是從左到右一行一行里面的單元格添加的,所以你添加的時(shí)候可以想象成輸出乘法表那樣。這里合并行的方法就是Colspan,列就是Rowspan,但是這里是屬性。。。int類型數(shù)字幾就是合并幾行或者幾列這樣。。。其實(shí)一開始我以為不管是行合并列合并都是合并,應(yīng)該有一個(gè)cell.row.merge(2)什么的,雖然不人性化但是習(xí)慣就好。
圖片image對(duì)象就有意思了,它有寬和長(zhǎng)度屬性,但是長(zhǎng)度是只讀的,而且設(shè)置了寬度程序運(yùn)行的時(shí)候會(huì)出錯(cuò),哈哈哈。。。。這就尷尬了,所以最后用image的ScalePercent方法。。
總結(jié)
以上是生活随笔為你收集整理的ITextSharp生成PDF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博客统计:腾讯分析这些数据哪儿来的?
- 下一篇: oracle备份