ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Convert save Web Page to PDF in Folder (Directory) and Display in ASP.Net GridView using C# and VB.Net<p>Hi <a class="username" rel="KatieNgoc"> KatieNgoc</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DropDownList ID=&#34;FormList&#34; runat=&#34;server&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;Redirect&#34; runat=&#34;server&#34; Text=&#34;Load A Form&#34; OnClick=&#34;Next_Click&#34; /&gt; &lt;asp:Button ID=&#34;Button1&#34; Text=&#34;Display&#34; runat=&#34;server&#34; OnClick=&#34;Download&#34; /&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Text&#34; HeaderText=&#34;File Name&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Text; using System.IO; using iTextSharp.text.pdf; using iTextSharp.text; using iTextSharp.text.html.simpleparser;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Text Imports System.IO Imports iTextSharp.text.pdf Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { if (Session[&#34;ListItems&#34;] == null) { DataTable dt = DropDownItems(); FormList.DataSource = dt; FormList.DataTextField = &#34;Text&#34;; FormList.DataValueField = &#34;Value&#34;; FormList.DataBind(); Session[&#34;ListItems&#34;] = dt; } else { DataTable dt = (DataTable)Session[&#34;ListItems&#34;]; FormList.DataSource = dt; FormList.DataTextField = &#34;Text&#34;; FormList.DataValueField = &#34;Value&#34;; FormList.DataBind(); } } } private DataTable DropDownItems() { DataTable dt = new DataTable(); dt.Columns.Add(&#34;Text&#34;); dt.Columns.Add(&#34;Value&#34;); dt.Rows.Add(&#34;Select one item, click on [Load a Form] button&#34;, &#34;0&#34;); dt.Rows.Add(&#34;1. W-4 IRS Form&#34;, &#34;1&#34;); dt.Rows.Add(&#34;2. Sexual Harassment Policy&#34;, &#34;2&#34;); dt.Rows.Add(&#34;3. Arbitration Agreement&#34;, &#34;3&#34;); dt.Rows.Add(&#34;4. Disciplinary &amp; Attendance Policy&#34;, &#34;4&#34;); dt.Rows.Add(&#34;5. Paid Sick Leave&#34;, &#34;5&#34;); dt.Rows.Add(&#34;6. Authorization for Release of Information&#34;, &#34;6&#34;); dt.Rows.Add(&#34;7. General Code of Safe Practices&#34;, &#34;7&#34;); dt.Rows.Add(&#34;8. A Summary of Your Rights Under the Fair Credit Reporting Act&#34;, &#34;8&#34;); dt.Rows.Add(&#34;9. Meal Period Policy&#34;, &#34;9&#34;); dt.Rows.Add(&#34;10. Meal Break Waiver Agreement&#34;, &#34;10&#34;); dt.Rows.Add(&#34;11. Rest Period Policy&#34;, &#34;11&#34;); dt.Rows.Add(&#34;12. Employee Safety Orientation Sign-Off Sheet&#34;, &#34;12&#34;); dt.Rows.Add(&#34;13. Policy Statement Firearms, Weapons-Free Workplace&#34;, &#34;13&#34;); dt.Rows.Add(&#34;14. Assignment Abandonment&#34;, &#34;14&#34;); dt.Rows.Add(&#34;15. Remedying the Effects of Identity Theft&#34;, &#34;15&#34;); dt.Rows.Add(&#34;16. Drug and Alcohol Policy&#34;, &#34;16&#34;); dt.Rows.Add(&#34;17. General Safety Video&#34;, &#34;17&#34;); return dt; } protected void Next_Click(object sender, EventArgs e) { DataTable newDt = new DataTable(); newDt.Columns.Add(&#34;Text&#34;); newDt.Columns.Add(&#34;Value&#34;); DataTable dt = (DataTable)Session[&#34;ListItems&#34;]; ListItemCollection liCol = FormList.Items; for (int i = 0; i &lt; dt.Rows.Count; i++) { ListItem li = liCol[i]; if (li.Selected) { if (FormList.SelectedItem.Value == &#34;1&#34;) { GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;); } else if (FormList.SelectedItem.Value == &#34;2&#34;) { GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;); } else if (FormList.SelectedItem.Value == &#34;3&#34;) { GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;); } } } } private void GetNewDt(DataTable newDt, DataTable dt, int i, string url) { dt.Rows[i].Delete(); newDt.Rows.Add(FormList.SelectedItem.Text, FormList.SelectedItem.Value); Session[&#34;newdt&#34;] = newDt; Response.Redirect(url); } protected void Display(object sender, EventArgs e) { string[] filePaths = Directory.GetFiles(Server.MapPath(&#34;~/File/&#34;)); List&lt;System.Web.UI.WebControls.ListItem&gt; files = new List&lt;System.Web.UI.WebControls.ListItem&gt;(); foreach (string filePath in filePaths) { files.Add(new System.Web.UI.WebControls.ListItem(Path.GetFileName(filePath), filePath)); } GridView1.DataSource = files; GridView1.DataBind(); }</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = Session[&#34;newdt&#34;] as DataTable; StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; dt.Rows.Count; i++) { sb.Append(&#34;&lt;p&gt;&#34;); sb.Append(dt.Rows[i][&#34;Text&#34;] + &#34;&#34; + dt.Rows[i][&#34;Value&#34;]); sb.Append(&#34;&lt;/p&gt;&#34;); File.WriteAllText(Server.MapPath(&#34;~/Filehtml/&#34;) + dt.Rows[i][&#34;Value&#34;] + &#34;.htm&#34;, sb.ToString()); using (StringWriter sw = new StringWriter(sb)) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { using (StreamReader sr = new StreamReader(Server.MapPath(&#34;~/Filehtml/&#34;) + dt.Rows[i][&#34;Value&#34;] + &#34;.htm&#34;)) { using (FileStream stream = new FileStream(Server.MapPath(&#34;~/File/&#34;) + &#34;HTMLExport&#34; + dt.Rows[i][&#34;Value&#34;] + &#34;.pdf&#34;, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); stream.Close(); } } } } } } }</pre> <p><strong>VB.aspx.vb</strong></p> <pre class="brush: vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then If Session(&#34;ListItems&#34;) Is Nothing Then Dim dt As DataTable = DropDownItems() FormList.DataSource = dt FormList.DataTextField = &#34;Text&#34; FormList.DataValueField = &#34;Value&#34; FormList.DataBind() Session(&#34;ListItems&#34;) = dt Else Dim dt As DataTable = CType(Session(&#34;ListItems&#34;), DataTable) FormList.DataSource = dt FormList.DataTextField = &#34;Text&#34; FormList.DataValueField = &#34;Value&#34; FormList.DataBind() End If End If End Sub Private Function DropDownItems() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.Add(&#34;Text&#34;) dt.Columns.Add(&#34;Value&#34;) dt.Rows.Add(&#34;Select one item, click on [Load a Form] button&#34;, &#34;0&#34;) dt.Rows.Add(&#34;1. W-4 IRS Form&#34;, &#34;1&#34;) dt.Rows.Add(&#34;2. Sexual Harassment Policy&#34;, &#34;2&#34;) dt.Rows.Add(&#34;3. Arbitration Agreement&#34;, &#34;3&#34;) dt.Rows.Add(&#34;4. Disciplinary &amp; Attendance Policy&#34;, &#34;4&#34;) dt.Rows.Add(&#34;5. Paid Sick Leave&#34;, &#34;5&#34;) dt.Rows.Add(&#34;6. Authorization for Release of Information&#34;, &#34;6&#34;) dt.Rows.Add(&#34;7. General Code of Safe Practices&#34;, &#34;7&#34;) dt.Rows.Add(&#34;8. A Summary of Your Rights Under the Fair Credit Reporting Act&#34;, &#34;8&#34;) dt.Rows.Add(&#34;9. Meal Period Policy&#34;, &#34;9&#34;) dt.Rows.Add(&#34;10. Meal Break Waiver Agreement&#34;, &#34;10&#34;) dt.Rows.Add(&#34;11. Rest Period Policy&#34;, &#34;11&#34;) dt.Rows.Add(&#34;12. Employee Safety Orientation Sign-Off Sheet&#34;, &#34;12&#34;) dt.Rows.Add(&#34;13. Policy Statement Firearms, Weapons-Free Workplace&#34;, &#34;13&#34;) dt.Rows.Add(&#34;14. Assignment Abandonment&#34;, &#34;14&#34;) dt.Rows.Add(&#34;15. Remedying the Effects of Identity Theft&#34;, &#34;15&#34;) dt.Rows.Add(&#34;16. Drug and Alcohol Policy&#34;, &#34;16&#34;) dt.Rows.Add(&#34;17. General Safety Video&#34;, &#34;17&#34;) Return dt End Function Protected Sub Next_Click(ByVal sender As Object, ByVal e As EventArgs) Dim newDt As DataTable = New DataTable() newDt.Columns.Add(&#34;Text&#34;) newDt.Columns.Add(&#34;Value&#34;) Dim dt As DataTable = CType(Session(&#34;ListItems&#34;), DataTable) Dim liCol As ListItemCollection = FormList.Items For i As Integer = 0 To dt.Rows.Count - 1 Dim li As ListItem = liCol(i) If li.Selected Then If FormList.SelectedItem.Value = &#34;1&#34; Then GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;) ElseIf FormList.SelectedItem.Value = &#34;2&#34; Then GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;) ElseIf FormList.SelectedItem.Value = &#34;3&#34; Then GetNewDt(newDt, dt, i, &#34;Default.aspx&#34;) End If End If Next End Sub Private Sub GetNewDt(ByVal newDt As DataTable, ByVal dt As DataTable, ByVal i As Integer, ByVal url As String) dt.Rows(i).Delete() newDt.Rows.Add(FormList.SelectedItem.Text, FormList.SelectedItem.Value) Session(&#34;newdt&#34;) = newDt Response.Redirect(url) End Sub Protected Sub Display(ByVal sender As Object, ByVal e As EventArgs) Dim filePaths As String() = Directory.GetFiles(Server.MapPath(&#34;~/File/&#34;)) Dim files As List(Of System.Web.UI.WebControls.ListItem) = New List(Of System.Web.UI.WebControls.ListItem)() For Each filePath As String In filePaths files.Add(New System.Web.UI.WebControls.ListItem(Path.GetFileName(filePath), filePath)) Next GridView1.DataSource = files GridView1.DataBind() End Sub</pre> <p><strong>Default.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim dt As DataTable = TryCast(Session(&#34;newdt&#34;), DataTable) Dim sb As StringBuilder = New StringBuilder() For i As Integer = 0 To dt.Rows.Count - 1 sb.Append(&#34;&lt;p&gt;&#34;) sb.Append(dt.Rows(i)(&#34;Text&#34;) &amp; &#34;&#34; + dt.Rows(i)(&#34;Value&#34;)) sb.Append(&#34;&lt;/p&gt;&#34;) File.WriteAllText(Server.MapPath(&#34;~/Filehtml/&#34;) + dt.Rows(i)(&#34;Value&#34;) &amp; &#34;.htm&#34;, sb.ToString()) Using sw As StringWriter = New StringWriter(sb) Using hw As HtmlTextWriter = New HtmlTextWriter(sw) Using sr As StreamReader = New StreamReader(Server.MapPath(&#34;~/Filehtml/&#34;) + dt.Rows(i)(&#34;Value&#34;) &amp; &#34;.htm&#34;) Using stream As FileStream = New FileStream(Server.MapPath(&#34;~/File/&#34;) &amp; &#34;HTMLExport&#34; + dt.Rows(i)(&#34;Value&#34;) &amp; &#34;.pdf&#34;, FileMode.Create) Dim pdfDoc As Document = New Document(PageSize.A2, 10F, 10F, 10F, 0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) PdfWriter.GetInstance(pdfDoc, stream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() stream.Close() End Using End Using End Using End Using Next End If End Sub</pre>https://www.aspforums.net:443/Threads/105364/Convert-save-Web-Page-to-PDF-in-Folder-Directory-and-Display-in-ASPNet-GridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/105364/Convert-save-Web-Page-to-PDF-in-Folder-Directory-and-Display-in-ASPNet-GridView-using-C-and-VBNet/Mon, 19 Aug 2019 03:55:54 GMT