CSharp: Select.Pdf Free Html To Pdf Converter for .NET

    /// <summary>
    /// SelectPdf edit: geovindu,Geovin Du
    ///https://github.com/selectpdf/
    ///https://www.nuget.org/packages/Select.Pdf/
    ///https://github.com/selectpdf/selectpdf-free-html-to-pdf-converter
    /// </summary>
    public partial class pdf_converter_viewer_preferences : System.Web.UI.Page
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // read parameters from the webpage  www.dusystem.com
            string url = TxtUrl.Text;

            string page_layout = DdlPageLayout.SelectedValue;
            PdfViewerPageLayout pageLayout = (PdfViewerPageLayout)Enum.Parse(
                typeof(PdfViewerPageLayout), page_layout, true);

            string page_mode = DdlPageMode.SelectedValue;
            PdfViewerPageMode pageMode = (PdfViewerPageMode)Enum.Parse(
                typeof(PdfViewerPageMode), page_mode, true);

            bool centerWindow = ChkCenterWindow.Checked;
            bool displayDocTitle = ChkDisplayDocTitle.Checked;
            bool fitWindow = ChkFitWindow.Checked;
            bool hideMenuBar = ChkHideMenuBar.Checked;
            bool hideToolbar = ChkHideToolbar.Checked;
            bool hideWindowUI = ChkHideWindowUI.Checked;

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.ViewerPreferences.CenterWindow = centerWindow;
            converter.Options.ViewerPreferences.DisplayDocTitle = displayDocTitle;
            converter.Options.ViewerPreferences.FitWindow = fitWindow;
            converter.Options.ViewerPreferences.HideMenuBar = hideMenuBar;
            converter.Options.ViewerPreferences.HideToolbar = hideToolbar;
            converter.Options.ViewerPreferences.HideWindowUI = hideWindowUI;

            converter.Options.ViewerPreferences.PageLayout = pageLayout;
            converter.Options.ViewerPreferences.PageMode = pageMode;
            converter.Options.ViewerPreferences.NonFullScreenPageMode =
                PdfViewerFullScreenExitMode.UseNone;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(url);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }

  

 

 生成的PDF文档

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SelectPdf;


namespace WebAppPdfDemo
{

    /// <summary>
    /// 
    /// </summary>
    public partial class convert_html_code_to_pdf : System.Web.UI.Page
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TxtHtmlCode.Text = @"<html>
                     <body>
                      Hello World from dusystem.com. 捷为工作室
                      <br/>
                      <p>common sense is the collection of prejudices acquired by age eighteen。 常识就是人到十八岁为止所累积的各种偏见。--阿尔伯特·爱因斯坦(Albert Einstein,1879年3月14日-1955年4月18日)</p>
                      <h1>Knowledge crunching is an exploration, and you can't know where you will end up.
知识消化是一种探索,它永无止境。
  ---《Domain-Driven Design --Tackling Complexity in the Heart of Software》By Eric Evans</h1>
                      <div>【深圳市人民医院】推荐孙思邈养生12法:1、发常梳;2、目常运;3、齿常叩;4、漱玉津;5、耳常鼓;6、面常洗;7、头常摇;8、腰常摆;9、腹常揉;10、摄谷道(即吸气时提肛);11、膝常扭;12、常散步。深圳市人民医院</div>
                      <p>故曰:“仓廪实而知礼节,衣食足而知荣辱。”礼生于有而废于无。故君子富,好行其德;小人富,以适其力。渊深而鱼生之,山深而兽往之,人富而仁义附焉。富者得势益彰,失势则客无所之,以而不乐。夷狄益甚。谚曰:“千金之子,不死于市。”此非空言也。故曰:“天下熙熙,皆为利来;天下攘攘,皆为利往。”夫千乘之王,万家之侯,百室之君,尚犹患贫,而况匹夫编户之民乎!《史记•货殖列传第六十九》
【注】①挽,同“晚”。②刍豢:指牲畜的肉。③眇:同“妙”。④虞:掌管山林川泽出产的官,此指开发山林川泽的人。⑤三宝:食、事、财。绝:不流通。⑥潟卤:盐碱地,不适宜耕种。

译文:所以说:“仓库充实了,百姓才能懂得礼节,衣食丰富了,百姓才知道荣耀与耻辱。”礼仪产生于富有而废弃于贫穷。所以,君子富有了,喜欢行仁德之事,小人富有了,就把力量用在适当的地方。潭渊深了,里面就会有鱼,山林深了,野兽就会到那里去,人民富了,仁义也就归附于他们了。富有者得势,越加显赫;失势了,客人也就没有去处,因此也就心情不快。谚语说:“千金之家的子弟就不会因犯法而死于市井。”这并不是空话。所以说:“天下之人,熙熙壤壤;为利而来,为利而往。”即使有千乘兵车的天子,有万家封地的诸侯,有百室封邑的大夫,尚且担心贫穷,何况编在户口册子上的普通百姓呢!</p>
                     </body>
                    </html>
                    ";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // read parameters from the webpage
            string htmlString = TxtHtmlCode.Text;
            string baseUrl = TxtBaseUrl.Text;

            string pdf_page_size = DdlPageSize.SelectedValue; //"A4"
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                pdf_page_size, true);

            string pdf_orientation = DdlPageOrientation.SelectedValue;  //"Landscape" // PdfPageOrientation.Landscape
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32(TxtWidth.Text);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32(TxtHeight.Text);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

            // save pdf document
            doc.Save(Response, false, "geovindu.pdf");

            // close pdf document
            doc.Close();
        }
    }
}

  

生成PDF:

 

 

posted @ 2023-02-21 11:40  ®Geovin Du Dream Park™  阅读(95)  评论(0)    收藏  举报