Answered by:
On Button Click I Want To See Same Design In 1 Page PDF

Question
-
User-807418713 posted
Hello
This is my sql table with data
/****** Object: Table [dbo].[PO_reg] Script Date: 07/31/2019 10:36:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PO_reg]( [Inc] [int] IDENTITY(1,1) NOT NULL, [POID] [varchar](50) NULL, [AccountID] [varchar](50) NULL, [PODate] [datetime] NULL, [SupplierID] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO SET IDENTITY_INSERT [dbo].[PO_reg] ON INSERT [dbo].[PO_reg] ([Inc], [POID], [AccountID], [PODate], [SupplierID]) VALUES (0, N'P1', N'AABBCC', CAST(0x0000AB2100000000 AS DateTime), N'AABBCC') SET IDENTITY_INSERT [dbo].[PO_reg] OFF /****** Object: Table [dbo].[PO_Description_New] Script Date: 07/31/2019 10:36:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PO_Description_New]( [POFUDescriptionID] [int] IDENTITY(1,1) NOT NULL, [ItemDescription] [varchar](50) NULL, [Packing_Details] [varchar](500) NULL, [Rate] [float] NULL, [HSN_Code] [varchar](50) NULL, [Tax_Perc] [varchar](50) NULL, [Quantity] [float] NULL, [Unit] [varchar](50) NULL, [ScheduleTo] [datetime] NULL, [POID] [varchar](50) NULL, [InvoiceID] [varchar](50) NULL, [InvoiceDescID] [varchar](50) NULL, [GodownDespatchNoteNo] [varbinary](50) NULL, [RefNo] [varchar](50) NULL, [Remarks] [varchar](250) NULL, CONSTRAINT [PK_PO_Description_New] PRIMARY KEY CLUSTERED ( [POFUDescriptionID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO SET IDENTITY_INSERT [dbo].[PO_Description_New] ON INSERT [dbo].[PO_Description_New] ([POFUDescriptionID], [ItemDescription], [Packing_Details], [Rate], [HSN_Code], [Tax_Perc], [Quantity], [Unit], [ScheduleTo], [POID], [InvoiceID], [InvoiceDescID], [GodownDespatchNoteNo], [RefNo], [Remarks]) VALUES (1, N'Item1', NULL, 15, NULL, NULL, 150, N'KG', NULL, N'P1', NULL, NULL, NULL, NULL, NULL) INSERT [dbo].[PO_Description_New] ([POFUDescriptionID], [ItemDescription], [Packing_Details], [Rate], [HSN_Code], [Tax_Perc], [Quantity], [Unit], [ScheduleTo], [POID], [InvoiceID], [InvoiceDescID], [GodownDespatchNoteNo], [RefNo], [Remarks]) VALUES (2, N'Item2', NULL, 20, NULL, NULL, 200, N'KG', NULL, N'P1', NULL, NULL, NULL, NULL, NULL) SET IDENTITY_INSERT [dbo].[PO_Description_New] OFF
This is my store procedure
create procedure [dbo].[HeadDetail](@PONO varchar(50)) as select a.SupplierID,a.AccountID,a.PODate,a.POID from dbo.PO_reg a where a.POID=@PONO create procedure [dbo].[PO_All_Details](@PONO varchar(50)) as select a.POID,a.ItemDescription,a.Rate,a.Quantity,a.Unit,a.ScheduleTo,'ABC' as HSN_Tax, isnull((a.Rate * a.Quantity * 18 / 100),0) as GST_Amount from dbo.PO_Description_New a where POID=@PONO
This is my aspx page
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" enableEventValidation ="false" CodeFile="Chem_D.aspx.cs" Inherits="Repeater_Rpt" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Purchase Order</title> <style type="text/css"> table { border-collapse: collapse; } table, th, tr, td { border: 1px solid black; } </style> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function () { $("#Button4").click(function () { $("#hfGridHtml").val($("#grid").html()); }); }); </script> <script type="text/javascript"> function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } </script> </head> <body style="font-size: 12pt; font-family: Times New Roman"> <form id="form1" runat="server" > <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:HiddenField ID="hfGridHtml" runat="server" Value="" /> <table> <tr> <td colspan="2"> <strong><span style="font-size: 24pt; color: #008080; font-family: Palatino Linotype"> PO Report</span></strong></td> <td style="width: 100px"> <asp:Button ID="Button2" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button2_Click" Style="background-image: url(images/btn_5.png)" Text="Refresh" Width="94px" /></td> <td style="width: 96px"> <asp:Button ID="Button4" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button4_Click" Style="background-image: url(images/btn_5.png)" Text="PDF View" Width="94px" /></td> <td style="width: 100px"> <input id="Button1" accesskey="P" onclick="printDiv('printableArea')" style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" type="button" /></td> </tr> </table> <%--Code Starts--%> <div id="printableArea"> <div id="grid" runat="server"> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound" > <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td colspan="3" style="text-align: center;"> <strong><span style="color: #008b8b; font-family: Palatino Linotype; font-size: 16pt;">Company Name<br /> </span></strong><span style="color: #008b8b; font-family: Palatino Linotype"><strong> <span style="font-size: 16pt"> Register No<br /> </span> <span style="color: #800000">N0 12345 Road <br /> (India)<br /> Phone : 11 22 33 44 55 66 77 88 99 </span></strong></span> </td> <td colspan="3" > <span style="color: maroon; font-family: Palatino Linotype"><strong> To: <asp:Label ID="Label6" runat="server" Text='<%# Eval("SupplierID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Supplier GST No:<asp:Label ID="Label13" runat="server" Text='KLAAZZ' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></br> City : <asp:Label ID="Label7" runat="server" Text='DDEEFF' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> State :<asp:Label ID="Label8" runat="server" Text='MMNNPP' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Country :<asp:Label ID="Label1" runat="server" Text='INDIA' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> </td> </tr> <tr> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO Date : <asp:Label ID="Label10" runat="server" Text='<%# Eval("PODate","{0:dd-MMM-yyyy}") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></strong></span></td> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO No : <asp:Label ID="Label9" runat="server" Text='<%# Eval("POID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> </strong></span></td> </tr> <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound"> <HeaderTemplate> <tr> <td style="color: #800000;; color: teal; font-family: Palatino Linotype; font-size: 14pt"> Item Name</td> <td style="color: #800000;; color: teal; font-family: Palatino Linotype; font-size: 14pt"> HSN & Tax%</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt"> Rate</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt"> Qty</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;"> Amount</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;"> Tax Amount</td> </tr> </HeaderTemplate> <ItemTemplate> <tr > <td> <%# Eval("ItemDescription")%> </td> <td> <%# Eval("HSN_Tax")%> </td> <td > <%# Eval("Rate") %> </td> <td > <asp:Label ID="LA" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LB" runat="server" Text='<%# ((Eval("Quantity")!= DBNull.Value ? Convert.ToDouble(Eval("Quantity")) : 0) * (Eval("Rate")!= DBNull.Value ? Convert.ToDouble(Eval("Rate")) : 0)).ToString()%>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LC" runat="server" Text=' <%# Eval("GST_Amount") %>'></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> <tr visible="false" id="hiddenrow" runat="server"> <td colspan="6"> </td> </tr> <tr> <td colspan="4" > <span style="color: maroon;font-family: Palatino Linotype"><strong> Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="FLB" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="FLC" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="5" > <span style="color: maroon;font-family: Palatino Linotype"><strong> Grand Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="GTotal" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="6" > <span style="font-size: 7pt"><span style="font-size: 11pt"><span style="text-decoration: underline"> <span style="color: red; font-family: Palatino Linotype"><strong>Terms & Conditions:</strong></span> <br /> </span></span>1. This purchase order shall be deemed as accepted if no replay is received within 7 days.<br /> 2. Purchase order is automatically cancelled if the supply is not made within the delivery time specified.<br /> </span></td> </tr> <tr> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Incharge</strong></span></td> <td colspan="3" > <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Authorized Signatory</strong></span></td> </tr> </FooterTemplate> </asp:Repeater> </table> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:Repeater> </div> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CHEMIMSConnectionString %>" SelectCommand="HeadDetail" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="GetLabelData" Name="PONO" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:Label style="VERTICAL-ALIGN: middle; TEXT-ALIGN: center; display:none" id="GetLabelData" runat="server" Width="109px" ForeColor="#660000" Font-Size="12pt" Font-Names="Palatino Linotype" Font-Bold="True"></asp:Label> </form> </body> </html>
This is my aspx cs page
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Specialized; using System.Text; using System.Drawing; using System.IO; using System.Net; using System.Net.Mail; using System.Net.Configuration; public partial class Repeater_Rpt : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetLabelData.Text = "P1"; ; } } decimal pscTotal = 0; decimal amountTotal = 0; int emptyrow; protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { pscTotal = 0; amountTotal = 0; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string connection = ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString; SqlConnection sqlCon = new SqlConnection(connection); string sqlStr = "DECLARE @return_value int EXEC @return_value = [dbo].[PO_All_Details] @PONO='" + GetLabelData.Text + "'"; SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlCon); DataSet ds = new DataSet(); DataTable dt1 = new DataTable(); da.Fill(ds, "Grouped"); DataTable DT = ds.Tables["Grouped"]; for (int i = DT.Rows.Count; i < 7; i++) { emptyrow++; } Repeater rep = e.Item.FindControl("Repeater2") as Repeater; rep.DataSource = ds; rep.DataBind(); } } protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label l2 = e.Item.FindControl("LB") as Label; Label l3 = e.Item.FindControl("LC") as Label; pscTotal += Convert.ToDecimal(l2.Text); amountTotal += Convert.ToDecimal(l3.Text); } decimal aa, bb, cc; if (e.Item.ItemType == ListItemType.Footer) { if (emptyrow > 0) { HtmlTableRow a = (HtmlTableRow)e.Item.FindControl("hiddenrow"); a.Visible = true; a.Height = 28 * emptyrow + "px"; } emptyrow = 0; // Label l4 = e.Item.FindControl("FLB") as Label; Label l5 = e.Item.FindControl("FLC") as Label; l4.Text = pscTotal.ToString("#"); l5.Text = amountTotal.ToString("#"); // Label LGTotal = e.Item.FindControl("GTotal") as Label; aa = Convert.ToDecimal(l4.Text); bb = Convert.ToDecimal(l5.Text); cc = aa + bb; LGTotal.Text = cc.ToString("#"); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { } protected void refresh_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void TextBox3_TextChanged(object sender, EventArgs e) { } protected void Button4_Click(object sender, EventArgs e) { } }
It showing result perfect as below
The same above design with font & color as its i want to see in pdf format on button click
How to do so
Please give me complete code
Thank You
Wednesday, July 31, 2019 5:15 AM
Answers
-
User-719153870 posted
Hi Gopi.MCA,
I'm sorry that i misunderstood your original intention.
I believe you want to download your form as PDF directly by click the "print" button without doing the print event for the real intention.
I suggest you can use the iTextSharp to do this job. Please refer to :https://www.nuget.org/packages/iTextSharp/
You can refer to below code in my demo:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PDFDemo.aspx.cs" Inherits="WebFormDemo01_No_Authentication_.PDFDemo" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Purchase Order</title> <style type="text/css"> table { border-collapse: collapse; } table, th, tr, td { border: 1px solid black; } </style> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function () { $("#Button4").click(function () { $("#hfGridHtml").val($("#grid").html()); }); }); </script> <script type="text/javascript"> function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } </script> </head> <body style="font-size: 12pt; font-family: Times New Roman"> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:HiddenField ID="hfGridHtml" runat="server" Value="" /> <table> <tbody> <tr> <td colspan="2"> <strong><span style="font-size: 24pt; color: #008080; font-family: Palatino Linotype">PO Report</span></strong></td> <td style="width: 100px"> <asp:Button ID="Button2" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button2_Click" Style="background-image: url(images/btn_5.png)" Text="Refresh" Width="94px" /></td> <td style="width: 96px"> <asp:Button ID="Button4" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button4_Click" Style="background-image: url(images/btn_5.png)" Text="PDF View" Width="94px" /></td> <td style="width: 100px"> <%--<input id="Button1" accesskey="P" onclick="printDiv('printableArea')" style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" type="button" />--%> <asp:Button ID="Button3" runat="server" AccessKey="P" Style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" Text="" OnClick="Button3_Click" /></td> </tr> </tbody> </table> <%--Code Starts--%> <div id="printableArea" style="font-size: 12pt; font-family: Times New Roman;width:464px" runat="server"> <div id="grid" runat="server" style="font-size: 12pt; font-family: Times New Roman;width:464px"> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <table><tbody> </HeaderTemplate> <ItemTemplate> <tr> <td colspan="3" style="text-align: center;"> <strong><span style="color: #008b8b; font-family: Palatino Linotype; font-size: 16pt;">Company Name<br /> </span></strong><span style="color: #008b8b; font-family: Palatino Linotype"><strong> <span style="font-size: 16pt">Register No<br /> </span> <span style="color: #800000">N0 12345 Road <br /> (India)<br /> Phone : 11 22 33 44 55 66 77 88 99 </span></strong></span> </td> <td colspan="3"> <span style="color: maroon; font-family: Palatino Linotype"><strong>To: <asp:Label ID="Label6" runat="server" Text='<%# Eval("SupplierID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Supplier GST No:<asp:Label ID="Label13" runat="server" Text='KLAAZZ' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label><br /> City : <asp:Label ID="Label7" runat="server" Text='DDEEFF' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> State :<asp:Label ID="Label8" runat="server" Text='MMNNPP' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Country :<asp:Label ID="Label1" runat="server" Text='INDIA' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /></strong></span></td> </tr> <tr> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO Date : <asp:Label ID="Label10" runat="server" Text='<%# Eval("PODate","{0:dd-MMM-yyyy}") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></strong></span></td> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO No : <asp:Label ID="Label9" runat="server" Text='<%# Eval("POID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> </strong></span></td> </tr> <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound"> <HeaderTemplate> <tr> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Item Name</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">HSN & Tax%</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Rate</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Qty</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Amount</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Tax Amount</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("ItemDescription")%> </td> <td> <%# Eval("HSN_Tax")%> </td> <td> <%# Eval("Rate") %> </td> <td> <asp:Label ID="LA" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LB" runat="server" Text='<%# ((Eval("Quantity")!= DBNull.Value ? Convert.ToDouble(Eval("Quantity")) : 0) * (Eval("Rate")!= DBNull.Value ? Convert.ToDouble(Eval("Rate")) : 0)).ToString()%>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LC" runat="server" Text=' <%# Eval("GST_Amount") %>'></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> <tr visible="false" id="hiddenrow" runat="server"> <td colspan="6"></td> </tr> <tr> <td colspan="4"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="FLB" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="FLC" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="5"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Grand Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="GTotal" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="6"> <span style="font-size: 7pt"><span style="font-size: 11pt"><span style="text-decoration: underline"> <span style="color: red; font-family: Palatino Linotype"><strong>Terms & Conditions:</strong></span> <br /> </span></span>1. This purchase order shall be deemed as accepted if no replay is received within 7 days.<br /> 2. Purchase order is automatically cancelled if the supply is not made within the delivery time specified.<br /> </span></td> </tr> <tr> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Incharge</strong></span></td> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Authorized Signatory</strong></span></td> </tr> </FooterTemplate> </asp:Repeater> </ItemTemplate> <FooterTemplate></tbody></table> </FooterTemplate> </asp:Repeater> </div> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DConString %>" SelectCommand="HeadDetail" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="GetLabelData" Name="PONO" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:Label Style="vertical-align: middle; text-align: center; display: none" ID="GetLabelData" runat="server" Width="109px" ForeColor="#660000" Font-Size="12pt" Font-Names="Palatino Linotype" Font-Bold="True"></asp:Label> </form> </body> </html>
CS:
using System; using System.Data; using System.Configuration; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml; protected void Button3_Click(object sender, EventArgs e) { using (Stream fs = new FileStream("C:\\Users\\yangsh\\Desktop\\HelloWorld.pdf", FileMode.Create)) { using (Document doc = new Document(PageSize.A4)) { PdfWriter writer = PdfWriter.GetInstance(doc, fs); doc.Open(); var sb = new StringBuilder(); sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\"> "); sb.Append("<body style=\"font - size: 12pt; font - family: Times New Roman\">"); sb.Append("<head id=\"Head1\" runat=\"server\">"); sb.Append("<title>Purchase Order</title>"); sb.Append("<style type=\"text / css\">"); sb.Append("table {"); sb.Append("border - collapse: collapse;width:500px;border-spacing:0"); sb.Append("}"); sb.Append("table, th, tr, td {"); sb.Append("border: 1px solid black;"); sb.Append("}"); sb.Append("</ style > "); sb.Append("</head>"); sb.Append("<form method=\"post\" action=\"./ PDFDemo\" id=\"form1\">"); printableArea.RenderControl(new HtmlTextWriter(new StringWriter(sb))); sb.Append("</form>"); sb.Append("</body>"); sb.Append("</html>"); string html = sb.ToString(); using (StringReader sr = new StringReader(html)) { XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr); } doc.Close(); } } }
Here's the result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2019 10:19 AM
All replies
-
User-719153870 posted
Hi Gopi.MCA,
I built a demo based on all your provided code and print out a PDF with all same font & color as shown in the image you put.
But a little difference is that the first row of this table("PO Report") is missing, is this what you really want?
Here's the screenshot of the PDF i printed with your code:
Best Regard,
Yang Shen
Wednesday, July 31, 2019 6:45 AM -
User-807418713 posted
Hello
Thanks For Your Reply
Can i get Code
Thank You
Wednesday, July 31, 2019 7:44 AM -
User-719153870 posted
Hi Gopi.MCA,
I built this demo with all your code and didn't change any letter of it.
In case, i will provide mine:
In SQL, create table and procedure:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PO_reg]( [Inc] [int] IDENTITY(1,1) NOT NULL, [POID] [varchar](50) NULL, [AccountID] [varchar](50) NULL, [PODate] [datetime] NULL, [SupplierID] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO SET IDENTITY_INSERT [dbo].[PO_reg] ON INSERT [dbo].[PO_reg] ([Inc], [POID], [AccountID], [PODate], [SupplierID]) VALUES (0, N'P1', N'AABBCC', CAST(0x0000AB2100000000 AS DateTime), N'AABBCC') SET IDENTITY_INSERT [dbo].[PO_reg] OFF /****** Object: Table [dbo].[PO_Description_New] Script Date: 07/31/2019 10:36:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PO_Description_New]( [POFUDescriptionID] [int] IDENTITY(1,1) NOT NULL, [ItemDescription] [varchar](50) NULL, [Packing_Details] [varchar](500) NULL, [Rate] [float] NULL, [HSN_Code] [varchar](50) NULL, [Tax_Perc] [varchar](50) NULL, [Quantity] [float] NULL, [Unit] [varchar](50) NULL, [ScheduleTo] [datetime] NULL, [POID] [varchar](50) NULL, [InvoiceID] [varchar](50) NULL, [InvoiceDescID] [varchar](50) NULL, [GodownDespatchNoteNo] [varbinary](50) NULL, [RefNo] [varchar](50) NULL, [Remarks] [varchar](250) NULL, CONSTRAINT [PK_PO_Description_New] PRIMARY KEY CLUSTERED ( [POFUDescriptionID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO SET IDENTITY_INSERT [dbo].[PO_Description_New] ON INSERT [dbo].[PO_Description_New] ([POFUDescriptionID], [ItemDescription], [Packing_Details], [Rate], [HSN_Code], [Tax_Perc], [Quantity], [Unit], [ScheduleTo], [POID], [InvoiceID], [InvoiceDescID], [GodownDespatchNoteNo], [RefNo], [Remarks]) VALUES (1, N'Item1', NULL, 15, NULL, NULL, 150, N'KG', NULL, N'P1', NULL, NULL, NULL, NULL, NULL) INSERT [dbo].[PO_Description_New] ([POFUDescriptionID], [ItemDescription], [Packing_Details], [Rate], [HSN_Code], [Tax_Perc], [Quantity], [Unit], [ScheduleTo], [POID], [InvoiceID], [InvoiceDescID], [GodownDespatchNoteNo], [RefNo], [Remarks]) VALUES (2, N'Item2', NULL, 20, NULL, NULL, 200, N'KG', NULL, N'P1', NULL, NULL, NULL, NULL, NULL) SET IDENTITY_INSERT [dbo].[PO_Description_New] OFF ---------------------------------------------------- create proc [HeadDetail] @PONO varchar(50) as select a.SupplierID,a.AccountID,a.PODate,a.POID from dbo.PO_reg a where a.POID=@PONO create procedure [dbo].[PO_All_Details](@PONO varchar(50)) as select a.POID,a.ItemDescription,a.Rate,a.Quantity,a.Unit,a.ScheduleTo,'ABC' as HSN_Tax, isnull((a.Rate * a.Quantity * 18 / 100),0) as GST_Amount from dbo.PO_Description_New a where POID=@PONO
In project, ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PDFDemo.aspx.cs" Inherits="WebFormDemo01_No_Authentication_.PDFDemo" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Purchase Order</title> <style type="text/css"> table { border-collapse: collapse; } table, th, tr, td { border: 1px solid black; } </style> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function () { $("#Button4").click(function () { $("#hfGridHtml").val($("#grid").html()); }); }); </script> <script type="text/javascript"> function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } </script> </head> <body style="font-size: 12pt; font-family: Times New Roman"> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:HiddenField ID="hfGridHtml" runat="server" Value="" /> <table> <tr> <td colspan="2"> <strong><span style="font-size: 24pt; color: #008080; font-family: Palatino Linotype">PO Report</span></strong></td> <td style="width: 100px"> <asp:Button ID="Button2" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button2_Click" Style="background-image: url(images/btn_5.png)" Text="Refresh" Width="94px" /></td> <td style="width: 96px"> <asp:Button ID="Button4" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button4_Click" Style="background-image: url(images/btn_5.png)" Text="PDF View" Width="94px" /></td> <td style="width: 100px"> <input id="Button1" accesskey="P" onclick="printDiv('printableArea')" style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" type="button" /></td> </tr> </table> <%--Code Starts--%> <div id="printableArea"> <div id="grid" runat="server"> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <table> </HeaderTemplate> <ItemTemplate> <tr> <td colspan="3" style="text-align: center;"> <strong><span style="color: #008b8b; font-family: Palatino Linotype; font-size: 16pt;">Company Name<br /> </span></strong><span style="color: #008b8b; font-family: Palatino Linotype"><strong> <span style="font-size: 16pt">Register No<br /> </span> <span style="color: #800000">N0 12345 Road <br /> (India)<br /> Phone : 11 22 33 44 55 66 77 88 99 </span></strong></span> </td> <td colspan="3"> <span style="color: maroon; font-family: Palatino Linotype"><strong>To: <asp:Label ID="Label6" runat="server" Text='<%# Eval("SupplierID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Supplier GST No:<asp:Label ID="Label13" runat="server" Text='KLAAZZ' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></br> City : <asp:Label ID="Label7" runat="server" Text='DDEEFF' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> State :<asp:Label ID="Label8" runat="server" Text='MMNNPP' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Country :<asp:Label ID="Label1" runat="server" Text='INDIA' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /></td> </tr> <tr> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO Date : <asp:Label ID="Label10" runat="server" Text='<%# Eval("PODate","{0:dd-MMM-yyyy}") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></strong></span></td> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO No : <asp:Label ID="Label9" runat="server" Text='<%# Eval("POID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> </strong></span></td> </tr> <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound"> <HeaderTemplate> <tr> <td style="color: #800000;; color: teal; font-family: Palatino Linotype; font-size: 14pt">Item Name</td> <td style="color: #800000;; color: teal; font-family: Palatino Linotype; font-size: 14pt">HSN & Tax%</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Rate</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Qty</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Amount</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Tax Amount</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("ItemDescription")%> </td> <td> <%# Eval("HSN_Tax")%> </td> <td> <%# Eval("Rate") %> </td> <td> <asp:Label ID="LA" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LB" runat="server" Text='<%# ((Eval("Quantity")!= DBNull.Value ? Convert.ToDouble(Eval("Quantity")) : 0) * (Eval("Rate")!= DBNull.Value ? Convert.ToDouble(Eval("Rate")) : 0)).ToString()%>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LC" runat="server" Text=' <%# Eval("GST_Amount") %>'></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> <tr visible="false" id="hiddenrow" runat="server"> <td colspan="6"></td> </tr> <tr> <td colspan="4"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="FLB" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="FLC" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="5"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Grand Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="GTotal" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="6"> <span style="font-size: 7pt"><span style="font-size: 11pt"><span style="text-decoration: underline"> <span style="color: red; font-family: Palatino Linotype"><strong>Terms & Conditions:</strong></span> <br /> </span></span>1. This purchase order shall be deemed as accepted if no replay is received within 7 days.<br /> 2. Purchase order is automatically cancelled if the supply is not made within the delivery time specified.<br /> </span></td> </tr> <tr> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Incharge</strong></span></td> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Authorized Signatory</strong></span></td> </tr> </FooterTemplate> </asp:Repeater> </table> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:Repeater> </div> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DConString %>" SelectCommand="HeadDetail" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="GetLabelData" Name="PONO" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:Label Style="vertical-align: middle; text-align: center; display: none" ID="GetLabelData" runat="server" Width="109px" ForeColor="#660000" Font-Size="12pt" Font-Names="Palatino Linotype" Font-Bold="True"></asp:Label> </form> </body> </html>
.CS:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections.Specialized; using System.Text; using System.Drawing; using System.IO; using System.Net; using System.Net.Mail; using System.Net.Configuration; namespace WebFormDemo01_No_Authentication_ { public partial class PDFDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetLabelData.Text = "P1"; ; } } decimal pscTotal = 0; decimal amountTotal = 0; int emptyrow; protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { pscTotal = 0; amountTotal = 0; if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string connection = ConfigurationManager.ConnectionStrings["DConString"].ConnectionString; SqlConnection sqlCon = new SqlConnection(connection); string sqlStr = "DECLARE @return_value int EXEC @return_value = [dbo].[PO_All_Details] @PONO='" + GetLabelData.Text + "'"; SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlCon); DataSet ds = new DataSet(); DataTable dt1 = new DataTable(); da.Fill(ds, "Grouped"); DataTable DT = ds.Tables["Grouped"]; for (int i = DT.Rows.Count; i < 7; i++) { emptyrow++; } Repeater rep = e.Item.FindControl("Repeater2") as Repeater; rep.DataSource = ds; rep.DataBind(); } } protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label l2 = e.Item.FindControl("LB") as Label; Label l3 = e.Item.FindControl("LC") as Label; pscTotal += Convert.ToDecimal(l2.Text); amountTotal += Convert.ToDecimal(l3.Text); } decimal aa, bb, cc; if (e.Item.ItemType == ListItemType.Footer) { if (emptyrow > 0) { HtmlTableRow a = (HtmlTableRow)e.Item.FindControl("hiddenrow"); a.Visible = true; a.Height = 28 * emptyrow + "px"; } emptyrow = 0; // Label l4 = e.Item.FindControl("FLB") as Label; Label l5 = e.Item.FindControl("FLC") as Label; l4.Text = pscTotal.ToString("#"); l5.Text = amountTotal.ToString("#"); // Label LGTotal = e.Item.FindControl("GTotal") as Label; aa = Convert.ToDecimal(l4.Text); bb = Convert.ToDecimal(l5.Text); cc = aa + bb; LGTotal.Text = cc.ToString("#"); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { } protected void refresh_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect(Request.RawUrl); } protected void TextBox3_TextChanged(object sender, EventArgs e) { } protected void Button4_Click(object sender, EventArgs e) { } } }
Best Regard,
Yang Shen
Wednesday, July 31, 2019 7:59 AM -
User-807418713 posted
Hello
I need code to view this in pdf
Please give me code to convert my page in to pdf report
Wednesday, July 31, 2019 9:06 AM -
User-719153870 posted
Hi Gopi.MCA,
I'm sorry that i misunderstood your original intention.
I believe you want to download your form as PDF directly by click the "print" button without doing the print event for the real intention.
I suggest you can use the iTextSharp to do this job. Please refer to :https://www.nuget.org/packages/iTextSharp/
You can refer to below code in my demo:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PDFDemo.aspx.cs" Inherits="WebFormDemo01_No_Authentication_.PDFDemo" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Purchase Order</title> <style type="text/css"> table { border-collapse: collapse; } table, th, tr, td { border: 1px solid black; } </style> <script type="text/javascript" src="jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function () { $("#Button4").click(function () { $("#hfGridHtml").val($("#grid").html()); }); }); </script> <script type="text/javascript"> function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; } </script> </head> <body style="font-size: 12pt; font-family: Times New Roman"> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:HiddenField ID="hfGridHtml" runat="server" Value="" /> <table> <tbody> <tr> <td colspan="2"> <strong><span style="font-size: 24pt; color: #008080; font-family: Palatino Linotype">PO Report</span></strong></td> <td style="width: 100px"> <asp:Button ID="Button2" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button2_Click" Style="background-image: url(images/btn_5.png)" Text="Refresh" Width="94px" /></td> <td style="width: 96px"> <asp:Button ID="Button4" runat="server" AccessKey="R" BackColor="Transparent" BorderColor="Transparent" BorderStyle="Dashed" BorderWidth="1px" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="10pt" ForeColor="#FFFFFF" Height="40px" OnClick="Button4_Click" Style="background-image: url(images/btn_5.png)" Text="PDF View" Width="94px" /></td> <td style="width: 100px"> <%--<input id="Button1" accesskey="P" onclick="printDiv('printableArea')" style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" type="button" />--%> <asp:Button ID="Button3" runat="server" AccessKey="P" Style="background-image: url(images/NewPrint.png); width: 110px; height: 42px; background-color: transparent" Text="" OnClick="Button3_Click" /></td> </tr> </tbody> </table> <%--Code Starts--%> <div id="printableArea" style="font-size: 12pt; font-family: Times New Roman;width:464px" runat="server"> <div id="grid" runat="server" style="font-size: 12pt; font-family: Times New Roman;width:464px"> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound"> <HeaderTemplate> <table><tbody> </HeaderTemplate> <ItemTemplate> <tr> <td colspan="3" style="text-align: center;"> <strong><span style="color: #008b8b; font-family: Palatino Linotype; font-size: 16pt;">Company Name<br /> </span></strong><span style="color: #008b8b; font-family: Palatino Linotype"><strong> <span style="font-size: 16pt">Register No<br /> </span> <span style="color: #800000">N0 12345 Road <br /> (India)<br /> Phone : 11 22 33 44 55 66 77 88 99 </span></strong></span> </td> <td colspan="3"> <span style="color: maroon; font-family: Palatino Linotype"><strong>To: <asp:Label ID="Label6" runat="server" Text='<%# Eval("SupplierID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Supplier GST No:<asp:Label ID="Label13" runat="server" Text='KLAAZZ' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label><br /> City : <asp:Label ID="Label7" runat="server" Text='DDEEFF' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> State :<asp:Label ID="Label8" runat="server" Text='MMNNPP' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /> Country :<asp:Label ID="Label1" runat="server" Text='INDIA' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> <br /></strong></span></td> </tr> <tr> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO Date : <asp:Label ID="Label10" runat="server" Text='<%# Eval("PODate","{0:dd-MMM-yyyy}") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label></strong></span></td> <td colspan="3"> <span style="color: #800000; font-family: Palatino Linotype"><strong>PO No : <asp:Label ID="Label9" runat="server" Text='<%# Eval("POID") %>' Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Teal"></asp:Label> </strong></span></td> </tr> <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound"> <HeaderTemplate> <tr> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Item Name</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">HSN & Tax%</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Rate</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt">Qty</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Amount</td> <td style="color: #800000; color: teal; font-family: Palatino Linotype; font-size: 14pt; text-align: right;">Tax Amount</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# Eval("ItemDescription")%> </td> <td> <%# Eval("HSN_Tax")%> </td> <td> <%# Eval("Rate") %> </td> <td> <asp:Label ID="LA" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LB" runat="server" Text='<%# ((Eval("Quantity")!= DBNull.Value ? Convert.ToDouble(Eval("Quantity")) : 0) * (Eval("Rate")!= DBNull.Value ? Convert.ToDouble(Eval("Rate")) : 0)).ToString()%>'></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="LC" runat="server" Text=' <%# Eval("GST_Amount") %>'></asp:Label> </td> </tr> </ItemTemplate> <FooterTemplate> <tr visible="false" id="hiddenrow" runat="server"> <td colspan="6"></td> </tr> <tr> <td colspan="4"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="FLB" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> <td style="text-align: right;"> <asp:Label ID="FLC" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="5"> <span style="color: maroon; font-family: Palatino Linotype"><strong>Grand Total: </strong></span> </td> <td style="text-align: right;"> <asp:Label ID="GTotal" runat="server" Font-Bold="True" Font-Names="Palatino Linotype" Font-Size="12pt" ForeColor="Blue"></asp:Label> </td> </tr> <tr> <td colspan="6"> <span style="font-size: 7pt"><span style="font-size: 11pt"><span style="text-decoration: underline"> <span style="color: red; font-family: Palatino Linotype"><strong>Terms & Conditions:</strong></span> <br /> </span></span>1. This purchase order shall be deemed as accepted if no replay is received within 7 days.<br /> 2. Purchase order is automatically cancelled if the supply is not made within the delivery time specified.<br /> </span></td> </tr> <tr> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Incharge</strong></span></td> <td colspan="3"> <span style="font-family: Palatino Linotype"><strong><span style="color: red">Company Name <br /> </span> <br /> Authorized Signatory</strong></span></td> </tr> </FooterTemplate> </asp:Repeater> </ItemTemplate> <FooterTemplate></tbody></table> </FooterTemplate> </asp:Repeater> </div> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DConString %>" SelectCommand="HeadDetail" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="GetLabelData" Name="PONO" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:CONTROLPARAMETER ControlID="Label4" Name="PONO" PropertyName="Text" Type="String" /> <asp:Label Style="vertical-align: middle; text-align: center; display: none" ID="GetLabelData" runat="server" Width="109px" ForeColor="#660000" Font-Size="12pt" Font-Names="Palatino Linotype" Font-Bold="True"></asp:Label> </form> </body> </html>
CS:
using System; using System.Data; using System.Configuration; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml; protected void Button3_Click(object sender, EventArgs e) { using (Stream fs = new FileStream("C:\\Users\\yangsh\\Desktop\\HelloWorld.pdf", FileMode.Create)) { using (Document doc = new Document(PageSize.A4)) { PdfWriter writer = PdfWriter.GetInstance(doc, fs); doc.Open(); var sb = new StringBuilder(); sb.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\"> "); sb.Append("<body style=\"font - size: 12pt; font - family: Times New Roman\">"); sb.Append("<head id=\"Head1\" runat=\"server\">"); sb.Append("<title>Purchase Order</title>"); sb.Append("<style type=\"text / css\">"); sb.Append("table {"); sb.Append("border - collapse: collapse;width:500px;border-spacing:0"); sb.Append("}"); sb.Append("table, th, tr, td {"); sb.Append("border: 1px solid black;"); sb.Append("}"); sb.Append("</ style > "); sb.Append("</head>"); sb.Append("<form method=\"post\" action=\"./ PDFDemo\" id=\"form1\">"); printableArea.RenderControl(new HtmlTextWriter(new StringWriter(sb))); sb.Append("</form>"); sb.Append("</body>"); sb.Append("</html>"); string html = sb.ToString(); using (StringReader sr = new StringReader(html)) { XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr); } doc.Close(); } } }
Here's the result of this demo:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2019 10:19 AM