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();
Related
Im using the Itextsharp library to create a pdf document that contains both arabic and english letters. However, the arabic letters dont show at all!
tried other librarys like pdfsharp but to no avail. Some have suggested changing the font but im not sure exactly how to do that
Code is below
protected void BtnExportPDFReport_Click(object sender, EventArgs e)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
grdShowReport.AllowPaging = false;
grdShowReport.DataBind();
grdShowReport.RenderControl(hw);
StringReader srw = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(srw);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=DatabaseReports.pdf");
Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><style> td { mso-number-format:\\#; } </style> ");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
I need to export data to pdf file ,
but I have `An exception of type
System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code
this is my Code
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView ExportGrid = new GridView();
ExportGrid.AllowPaging = false;
List<Tayf.Models.Product> DataLst = new List<Tayf.Models.Product>();
DataLst = HttpContext.Current.Cache["productcache"] as List<Tayf.Models.Product>;
ExportGrid.DataSource = DataLst;//DataSet1 has all values without paging
ExportGrid.AllowPaging = false;
ExportGrid.DataBind();
ExportGrid.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
my data is Very huge over than 1 million row and I need to export all data in one file
As your data is such big, maybe you should separate your data into many parts and write them into the pdf separately.
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
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();
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();