Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a panel on page and inside panel have grid view when i export panel data into pdf every thing is fine but gridview height width and column height not work as per defined.
I try every thing from gridview property and code behind also.

I am using iTextSharp

What I have tried:

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "DeliveryChalan" + FileName + ".pdf";
            string appPath = HttpContext.Current.Request.ApplicationPath;
            string path = Server.MapPath(appPath + "\\files\\" + "DeliveryChalan" + FileName + ".pdf");
            dynamic output = new FileStream(path, FileMode.Create);
            iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
           //PdfWriter writer= iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
            //writer.PageEvent = new Footer();

            pdfDoc.Open();
            //Paragraph welcomeParagraph = new Paragraph("Hello, World!");
          
            //pdfDoc.Add(welcomeParagraph);

            for (int i = 1; i <= 4; i++)
            {
                string Id = "pnldata" + i;
                Panel pnl = (Panel)FindControl(Id);

                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                       
                        pdfDoc.SetPageSize(new Rectangle(595, 850));
                        var style = new StyleSheet();
                        style.LoadTagStyle("body", "size", "9px");
                        //style.LoadTagStyle("grdDeliverChallan2", "height", "500px");
                        htmlparser = new HTMLWorker(pdfDoc);
                        //htmlparser.SetStyleSheet(style);
                        //pdfDoc.PageSize.Height = 10;
                       
                        pdfDoc.NewPage();
                        //pdfDoc.PageSize
                        //const string V = @"vshsd";
                        //Chunk c1 = new Chunk("A chunk represents an isolated string. ");
                        //ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_RIGHT, V, 36, 820, 0);

                        pnl.RenderControl(hw);
                    
                        StringReader sr = new StringReader(sw.ToString());
                        htmlparser.Parse(sr);
                    }
                }
            }
           

            pdfDoc.Close();
Posted
Comments
ArvindTomar 12-Dec-18 6:45am    
after long R & D Finaly i Got the answer i use ironpdf.dll for control the height and with of grid and page

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
string appPath = HttpContext.Current.Request.ApplicationPath;
pdfDoc.Open();
Panel pnl;
StringWriter sw = new StringWriter();
for (int i = 1; i <= 5; i++)
{
string Id = "pnldata" + i;

pnl = (Panel)FindControl(Id);

using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
pdfDoc.SetPageSize(new Rectangle(595, 850));

htmlparser = new HTMLWorker(pdfDoc);



pdfDoc.NewPage();

pnl.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());
htmlparser.Parse(sr);

}
}
var Renderer = new HtmlToPdf();

string html1 = "" + sw.ToString() + "";
StringBuilder html = new StringBuilder(html1);

Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Portrait;
// Renderer.PrintOptions.Header = new SimpleHeaderFooter() { CenterText = "Iron PDf C# Html to PDF Example", FontSize = 10 };
// Renderer.PrintOptions.Footer = new HtmlHeaderFooter() { HtmlFragment = "page {page} of {total-pages}" };
var PDF = Renderer.RenderHtmlAsPdf(html1);
string path = Server.MapPath(appPath + "\\files\\" + "DeliveryChalan" + FileName + ".pdf");
PDF.SaveAs(path);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900