CSharp: Select.Pdf Free Html To Pdf Converter for .NET
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /// <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文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
pdf
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2014-02-21 sql and csharp: Split Function
2014-02-21 Colorbox - a jQuery lightbox
2012-02-21 csharp 在万年历中计算显示农历日子出错