how to apply css for html to pdf convert process? - c#

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

Related

ItextSharp arabic letters not showing up at all

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

Export to PDF using iText not displaying complete data from GridView

I am trying to export my GridView to PDF. When I click the button I can see that the document downloading with around 977KB but when it has finished download and browse to the download folder the file size then is 1KB.
This is the code I am using:
protected void ExportToPDF(object sender, EventArgs e)
{
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.AllowPaging = false;
GridView1.DataBind();
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();
}
I am not sure what is going wrong here?
Thanks

Converting a modal dialog window to pdf

I'm trying to convert a modal dialog with 3 DataTables to pdf using iTextSharp:
DataTable dtTable = (DataTable)ViewState["LoanPlan"];
DataTable dtTax = (DataTable)ViewState["PlanTaxData"];
DataTable dtPlan = (DataTable)ViewState["PlanAllData"];
I'm using this code to try and accomplish it:
protected void buttonPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
But it seems that it's actually trying to convert the whole .aspx page to .pdf format. How should I go about targeting just the modal dialogue or just the 3 DataTables?

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)

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

Categories

Resources