简体   繁体   中英

Export div content to pdf format

I used the following code to export div content to pdf format using itextsharp

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
Document pdfDoc = new Document(new Rectangle(1000f, 1000f));
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

it shows the following error

Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Images\logo.png'.`

if i hide the logo in desin then it exported to pdf but the alignment are missing. how to correct it.The page i need to export is follows在此处输入图片说明

In order to export the content to any pdf file, you need to follow the basic conventions of pdf file, those are given in the appendix G of the following pdf reference,

http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf

 Firstly install class in Nuget console: Install-Package itextsharp.xmlworker
Then add namespace: using iTextSharp.tool.xml;  





 Response.ContentType = "application/pdf";
            //string pdf;
            // pdf = Convert.ToInt32(hidTablEmpCode.ToString()).ToString() + ".Pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
            //Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            // var example_html = @"<p>This is <span class="" headline="" style="">some</span> sample  text<span style="">!!!</span></p>";
            StringReader sr = new StringReader(lblMessageDetail.Text);
            Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 50f, 50f);
            var writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            for (int i = 0; i < 5; i++)
            {
                string imageFilePath2 = Server.MapPath(".") + "/images/GraphicNew.jpg";
                iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(imageFilePath2);
                jpg3.Alignment = iTextSharp.text.Image.UNDERLYING;
                jpg3.ScaleToFit(3530, 835);
                jpg3.SetAbsolutePosition(0, 0);
                pdfDoc.Add(jpg3);
            }
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            string imageFilePath = Server.MapPath(".") + "/Bimages/ns5.png";
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
    
            GetEmp();
            string imageURLS = Server.MapPath("~/Emp/Document/") + hidTablEmpCode.Value + "/" + hidSign.Value.ToString();
            iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(imageURLS);
            jpg2.ScaleToFit(100, 50);
            jpg2.SetAbsolutePosition(370, 530);
            jpg2.Alignment = Element.ALIGN_LEFT;
    
            pdfDoc.Add(jpg2);
            jpg.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
            jpg.ScaleToFit(100, 50);
            jpg.SetAbsolutePosition(50, 735);
            pdfDoc.Add(jpg);
    
           
    
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM