亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#使用itextsharp生成PDF文件的實現(xiàn)代碼

 更新時間:2013年07月31日 09:27:30   作者:  
以下是對在C#中使用itextsharp生成PDF文件的實現(xiàn)代碼進(jìn)行了詳細(xì)分析介紹,需要的朋友可以過來參考下
項目需求需要生成一個PDF文檔,使用的是VS2010,ASP.NET。
網(wǎng)絡(luò)上多次搜索沒有自己想要的,于是硬著頭皮到itextpdf官網(wǎng)看英文文檔,按時完成任務(wù),以實用為主,共享一下:
使用HTML文件創(chuàng)建PDF模板:
使用自定義字體的一種方法:
復(fù)制代碼 代碼如下:

                FontFactory.Register(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\Fonts\\RAGE.TTF", "myFont");
                Font myFont = FontFactory.GetFont("myFont");
                BaseFont bf = myFont.BaseFont;

其中RAGE.TTF是微軟操作系統(tǒng)自帶的字體,目錄在C:\Windows\Fonts,建議將需要的字體拷貝到項目中使用,否則會出現(xiàn)引用不到的情況。
使用自定義樣式:
復(fù)制代碼 代碼如下:

                StyleSheet css = new StyleSheet();
                Dictionary<String, String> dict= new Dictionary<string, string>();
                dict.Add(HtmlTags.BGCOLOR, "#01366C");
                dict.Add(HtmlTags.COLOR, "#000000");
                dict.Add(HtmlTags.SIZE,"25");
                css.LoadStyle("css1", dict);

這里既可以使用了StyleSheet的LoadStyle方法。
注意itextsharp對HTML元素的支持很弱,像label、div等元素的對齊、背景顏色等屬性支持不好,建議使用table標(biāo)簽。
重寫Font的GetFont方法:
復(fù)制代碼 代碼如下:

public  class MyFontFactory : IFontProvider
        {
            public  Font GetFont(String fontname,String encoding, Boolean embedded, float size,int style, BaseColor color)
            {
                if (fontname == "微軟雅黑")
                {
                    string fontpath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\Fonts\\MSYH.ttf";
                    BaseFont bf3 = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontContent = new Font(bf3,size,style,color);
                    return fontContent;
                }
                else {
                    Font fontContent = FontFactory.GetFont(fontname, size, style, color);
                    return fontContent;
                }
            }
            public  Boolean IsRegistered(String fontname)
            {
                return false;
            }
        }

這里要想使用自定義字體需要繼承IFontProvider接口,并重寫Font的GetFont方法。
將自定義字體和樣式表加入到文檔:
復(fù)制代碼 代碼如下:

                Dictionary<String, Object> font = new Dictionary<string, object>();
                font.Add(HTMLWorker.FONT_PROVIDER,new MyFontFactory());

                List<IElement> p = HTMLWorker.ParseToList(new StreamReader(html), css,font);

使用PdfContentByte為元素加背景顏色:
復(fù)制代碼 代碼如下:

                PdfContentByte pcb = writer.DirectContentUnder;
                pcb.SetRGBColorFill(0, 255, 0);
                pcb.SetRGBColorFill(1, 54, 108);
                pcb.Rectangle(20, 413, 800, 42);
                pcb.Fill();

缺點(diǎn)顯而易見,就是需要絕對坐標(biāo),小弟學(xué)疏才淺,再加時間緊迫,只能如此。如果大牛知道更好的方法,還望不吝賜教。
完整代碼:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.html;
/// <summary>
///CreatePDF 的摘要說明
/// </summary>
namespace WSE.LCPI
{
    public class CreatePDF
    {
        public CreatePDF()
        {
            //
            //TODO: 在此處添加構(gòu)造函數(shù)邏輯
            //
        }
       public  class MyFontFactory : IFontProvider
        {
            public  Font GetFont(String fontname,String encoding, Boolean embedded, float size,int style, BaseColor color)
            {
                if (fontname == "微軟雅黑")
                {
                    string fontpath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\LCPI\\Fonts\\MSYH.ttf";
                    BaseFont bf3 = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontContent = new Font(bf3,size,style,color);
                    return fontContent;
                }
                else {
                    Font fontContent = FontFactory.GetFont(fontname, size, style, color);
                    return fontContent;
                }
            }
            public  Boolean IsRegistered(String fontname)
            {
                return false;
            }
        }
        /// <summary>
        /// 生成PDF
        /// </summary>
        /// <param name="html"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Boolean HTMLToPDF(string html, String fileName)
        {
            Boolean isOK = false;
            try
            {
                TextReader reader = new StringReader(html);
                // step 1: creation of a document-object
                Document document = new Document(PageSize.A4.Rotate(), 30, 30, 30, 30);
                // step 2:
                // we create a writer that listens to the document
                // and directs a XML-stream to a file
                fileName = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\PDF\\" + fileName+".pdf";
              FileStream fs=new FileStream(fileName, FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
                PdfWriter writer = PdfWriter.GetInstance(document,fs );
                HTMLWorker worker = new HTMLWorker(document);
                document.Open();
                worker.StartDocument();
                StyleSheet css = new StyleSheet();
                Dictionary<String, Object> font = new Dictionary<string, object>();
                font.Add(HTMLWorker.FONT_PROVIDER,new MyFontFactory());
                Dictionary<String, String> dict= new Dictionary<string, string>();
                dict.Add(HtmlTags.BGCOLOR, "#01366C");
                dict.Add(HtmlTags.COLOR, "#000000");
                dict.Add(HtmlTags.SIZE,"25");
                css.LoadStyle("css", dict);

                List<IElement> p = HTMLWorker.ParseToList(new StreamReader(html), css,font);
                for (int k = 0; k < p.Count; k++)
                {
                    document.Add((IElement)p[k]);
                }
                PdfContentByte pcb = writer.DirectContentUnder;
                pcb.SetRGBColorFill(0, 255, 0);
                pcb.SetRGBColorFill(1, 54, 108);
                pcb.Rectangle(20, 413, 800, 42);
                pcb.Fill();
                worker.EndDocument();
                worker.Close();              
                document.Close();
                reader.Close();
                isOK = true;
            }
            catch (Exception ex)
            {
                isOK = false;
            }
            finally {

            }
            return isOK;
        }
    }
}

相關(guān)文章

  • 關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介紹方法

    關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.

    本篇文章,小編為大家介紹關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介紹方法,有需要的朋友可以參考一下
    2013-04-04
  • C#的winform控件命名規(guī)范

    C#的winform控件命名規(guī)范

    這篇文章主要介紹了C#的winform控件命名規(guī)范,對各種常用控件的命名規(guī)范做了較為詳細(xì)的講解,需要的朋友可以參考下
    2015-01-01
  • C#控制臺程序的開發(fā)與打包為一個exe文件實例詳解

    C#控制臺程序的開發(fā)與打包為一個exe文件實例詳解

    所謂控制臺程序,就是沒有界面,運(yùn)行程序后只有一個黑色的類似cmd窗口,通過這個窗口進(jìn)行交互,下面這篇文章主要給大家介紹了關(guān)于C#控制臺程序的開發(fā)與打包為一個exe文件的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • C#實現(xiàn)控制臺飛行棋小游戲

    C#實現(xiàn)控制臺飛行棋小游戲

    這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)控制臺飛行棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下的相關(guān)資料
    2021-07-07
  • 如何利用C#打印九九乘法表

    如何利用C#打印九九乘法表

    這篇文章主要給大家介紹了關(guān)于如何利用C#打印九九乘法表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • C#利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)

    C#利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了C#潤滑利用TreeView控件實現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動手嘗試一下
    2022-07-07
  • unityZXing二維碼的生成與掃描

    unityZXing二維碼的生成與掃描

    這篇文章主要為大家詳細(xì)介紹了unityZXing二維碼的生成與掃描,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 無法從 int? 轉(zhuǎn)換為 int 運(yùn)行時出現(xiàn)錯誤

    無法從 int? 轉(zhuǎn)換為 int 運(yùn)行時出現(xiàn)錯誤

    無法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時會出現(xiàn)錯誤,通過強(qiáng)制類型轉(zhuǎn)換(int)便可解決
    2014-05-05
  • C#利用ReportViewer生成報表

    C#利用ReportViewer生成報表

    這篇文章主要為大家詳細(xì)介紹了C#利用ReportViewer生成報表的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • c#中文gbk編碼查詢示例代碼

    c#中文gbk編碼查詢示例代碼

    c#中文gbk編碼查詢示例,大家參考使用吧
    2013-12-12

最新評論