最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2013年07月31日 09:27:30   作者:  
以下是對在C#中使用itextsharp生成PDF文件的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)分析介紹,需要的朋友可以過來參考下
項(xiàng)目需求需要生成一個(gè)PDF文檔,使用的是VS2010,ASP.NET。
網(wǎng)絡(luò)上多次搜索沒有自己想要的,于是硬著頭皮到itextpdf官網(wǎng)看英文文檔,按時(shí)完成任務(wù),以實(shí)用為主,共享一下:
使用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àng)目中使用,否則會(huì)出現(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é)疏才淺,再加時(shí)間緊迫,只能如此。如果大牛知道更好的方法,還望不吝賜教。
完整代碼:
復(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)文章

  • C#常用的字符串?dāng)U展方法匯總

    C#常用的字符串?dāng)U展方法匯總

    這篇文章主要介紹了C#常用的字符串?dāng)U展方法匯總,包括了常見的字符串操作與數(shù)據(jù)類型轉(zhuǎn)換等,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C#在新建線程中使用Timer無效問題及解決

    C#在新建線程中使用Timer無效問題及解決

    這篇文章主要介紹了C#在新建線程中使用Timer無效問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C#實(shí)現(xiàn)將字符串轉(zhuǎn)化為日期格式的方法詳解

    C#實(shí)現(xiàn)將字符串轉(zhuǎn)化為日期格式的方法詳解

    這篇文章主要為大家詳細(xì)介紹了C#如何使用DateTime結(jié)構(gòu)的ParseExact方法和Parse方法分別將字符串轉(zhuǎn)化為日期格式,有需要的小伙伴可以了解一下
    2024-01-01
  • C#操作Word打印的示例

    C#操作Word打印的示例

    這篇文章主要介紹了C#操作Word打印的示例,幫助大家利用c#打印文件,提高辦公效率,感興趣的朋友可以了解下
    2020-10-10
  • C#實(shí)現(xiàn)圖像銳化的方法

    C#實(shí)現(xiàn)圖像銳化的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)圖像銳化的方法,涉及C#操作圖像的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C# 中 “$” 符號(hào)的作用以及用法詳解

    C# 中 “$” 符號(hào)的作用以及用法詳解

    這篇文章主要介紹了C# 中 “$” 符號(hào)的作用以及用法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • C#操作目錄與文件的方法步驟

    C#操作目錄與文件的方法步驟

    本篇文章是對C#操作目錄與文件的方法步驟進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C# 郵箱mail 發(fā)送類

    C# 郵箱mail 發(fā)送類

    此類的功能包括發(fā)送郵件,郵箱格式是否正確,和在不發(fā)送郵件的情況下判斷郵箱用戶名和密碼是否正確,鑒于POP檢查郵箱用戶名和密碼出現(xiàn)錯(cuò)誤情況返回結(jié)果的延遲,用異步線程解決此問題,見代碼
    2015-06-06
  • 關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法

    關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法

    本篇文章小編為大家介紹,關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法。需要的朋友參考下
    2013-04-04
  • 利用C#實(shí)現(xiàn)記事本的功能的示例代碼

    利用C#實(shí)現(xiàn)記事本的功能的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)簡單的記事本的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12

最新評論

隆子县| 正宁县| 凭祥市| 德庆县| 泗水县| 南华县| 屏东县| 镶黄旗| 格尔木市| 仪征市| 古田县| 辽源市| 台湾省| 信丰县| 尤溪县| 巨鹿县| 唐河县| 塔城市| 安图县| 库车县| 新巴尔虎左旗| 延边| 蒲江县| 晋城| 阳新县| 晋城| 河东区| 新兴县| 库车县| 涿州市| 博乐市| 呼图壁县| 集贤县| 汝阳县| 石阡县| 壤塘县| 南涧| 宁蒗| 左权县| 翼城县| 德昌县|