日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ITextSharp生成PDF

發布時間:2025/3/17 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ITextSharp生成PDF 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ITextSharp就不多介紹了,下面就把遇到的坑一一記錄下來,希望能夠幫助到正在使用它的開發者們。操作pdf的方法都被作者封裝好了,只是沒有注釋和說明,不過大部分的方法屬性還是能看懂的,看不懂的可以反編譯一下。

gethub下載dll地址:https://github.com/itext/itextsharp/tags

1.輸入文字不顯示中文,文字換行

2.文字加顏色、字體大小、加粗、斜體、居中等騷操作

3.表格行合并、表格列合并

4.添加新頁面

5.圖片等比縮放、頁面中心顯示

下面代碼演示:

首先添加幾個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();//最后一個參數是顏色,這里可以是rgb格式,也可以是默認定義的var title = new Paragraph("\n 這是一條標題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("這是一條標題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);//新頁面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;//這時判斷圖片寬度是否大于頁面寬度減去也邊距,如果是,那么縮小,如果還大,繼續縮小, //這樣這個縮小的百分比percentage會越來越小 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;}//這里用計算出來的百分比來縮小圖片 img.ScalePercent(percentage * 100);//圖片定位,頁面總寬283,高416;這里設置0,0的話就是頁面的左下角 讓圖片的中心點與頁面的中心店進行重合 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,這個東西只能初始化他的列,表格里面add的時候是從左到右一行一行里面的單元格添加的,所以你添加的時候可以想象成輸出乘法表那樣。這里合并行的方法就是Colspan,列就是Rowspan,但是這里是屬性。。。int類型數字幾就是合并幾行或者幾列這樣。。。其實一開始我以為不管是行合并列合并都是合并,應該有一個cell.row.merge(2)什么的,雖然不人性化但是習慣就好。

圖片image對象就有意思了,它有寬和長度屬性,但是長度是只讀的,而且設置了寬度程序運行的時候會出錯,哈哈哈。。。。這就尷尬了,所以最后用image的ScalePercent方法。。

總結

以上是生活随笔為你收集整理的ITextSharp生成PDF的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。