Export div content to pdf format - c#

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();

Related

how to save pdf on server map path using iTextsharp

i am using following code for generate pdf and it is work perfect:
string strQuery = "select * from userdata";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
it works good. but i am able to save this pdf to on server map path.
i have written below after pdfDoc.Close();
String path = Server.MapPath("~/mypdf.pdf");
But it is not saving pdf to server map path.
how can i do this?
You are currently writing the document to the following output stream: Response.OutputStream
Once you do pdfDoc.Close();, the PDF bytes are gone.
If you want to save the PDF to the server, then you need to replace the following line:
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
With this line:
PdfWriter.GetInstance(pdfDoc, new FileStream(context.Server.MapPath("~") + "mypdf.pdf");
Now your bytes won't be sent to the browser, but the PDF will be created on your server.

Error while converting gridview into pdf format using iTextsharp

I am having gridview in which in few records it has image and in few records it didn't have image which gives error while converting it to pdf format.This code works fine if each row have images but not in case if any record didn't have image. Can anyone suggest me what to do with code so that my error can be resolve
here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
grdview();
string file = "testc" + ".pdf";
string attachment = string.Format("attachment;filename={0}", file);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfdoc = new iTextSharp.text.Document();
pdfdoc.SetMargins(0f, 0f, 0f, 0f);
pdfdoc.SetPageSize(iTextSharp.text.PageSize.A2);
HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
pdfdoc.Open();
htmlparser.Parse(sr);
pdfdoc.Close();
Response.Write(pdfdoc);
Response.End();
}
Image what error appears while conversion

Export to pdf | How to show option to open and save

I have this working code for exporting to pdf. When I export it , it asks me location to save file. Instead , I want to have option of OPEN or SAVE . Currently what I am doing is :
protected void btnPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView2.AllowPaging = false;
GridView2.DataBind();
GridView2.RenderControl(hw);
GridView2.HeaderRow.Style.Add("width", "15%");
GridView2.HeaderRow.Style.Add("font-size", "10px");
GridView2.Style.Add("text-decoration", "none");
GridView2.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView2.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
If you have the file on the server
Response.TransmitFile(//FILE PATH);
If you don't
Response.BinaryWrite(//Pass Byte Array)

'The document has no page' error thrown when closing a document

I'm generating, saving and writing PDF on output stream.
I'm getting a The document has no page error while writing on output stream.
What is the problem?
string contents;
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size","11px");
abs.RenderControl(hw);
string path = Server.MapPath("../Images/a.png");
contents = sw.ToString();
contents = contents.Replace("../Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/pdf/") + fileName, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30,5,35,5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);
cpdfDoc.Close();
Response.Write(cpdfDoc);
Response.Flush();
Response.End();
No problem in saving. I'm getting error on this line cpdfDoc.Close();.
use this code and set your pdf location. and image path.
its working.
string contents = "hi";
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size", "11px");
string path = Server.MapPath("~/Images/a.png");
contents = contents.Replace("~/Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/") + DateTime.Now.Ticks, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);
don't use this line
contents = sw.ToString();

how to apply css for html to pdf convert process?

I used iTextSharp to convert pdf from HTML with inline Css, but the css is not applying.
divPDF.InnerHtml = "<ul style="float:none;width:100%;display:block;list-style:none;margin:0 0 20px 0;padding:0;clear:both"><li style="text-indent:0;"><label style="float:left;width:50%;">Additional Medicare tax (1.45% + .9%)</label>P1_F1</li><li style="text-indent:0;"><label style="float:left;width:50%;">Additional Obamacare Medicare tax (3.8%)</label>P1_F2</li></ul>";
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);
divPDF.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Categories

Resources