Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
You can convert your html to pdf with itextsharp dll. Just make sure your html tags closed well.
// Namespaces
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
// Lets consider your html looks like HTML.
string HTML = "<table style=\"width:800px;\"><tr><td style=\"width:100px;\">FOO</td><td style=\"width:10px\">:</td><td>BLABLABLA ...</td></tr></table>";
try
{
//string filename = "\\pdf\\x_pdf___" + Request.QueryString["id"] + ".pdf";
string filepath = HttpContext.Current.Server.MapPath("\\pdf\\x_pdf___" + Request.QueryString["id"] + ".pdf");
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));
document.Open();
HTMLWorker hw = new HTMLWorker(document);
StringReader sr = new StringReader(HTML);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
//hw.Parse(new StringReader(HTML));
document.Close();
ShowPdf(filename, filepath);
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
}
catch (Exception ex)
{
string error = ex.Message;
}
// function to show on page
private void ShowPdf(string filename, string filePath)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.BufferOutput = true;
string FilePath = HttpContext.Current.Server.MapPath(filename);
Response.WriteFile(FilePath);
Response.End();
}
@Architshrivastava
Copy link

Architshrivastava commented Sep 13, 2018

hello sir how to set background image in html table and print to pdf.
i am using this but its not working
string HTML = "<div style="background-image:url('C:\Users\Archit\Downloads\itbp.jpg');width:100%;height:100%;\ "><table style="width:100%;"><td style="width:100px;">FOO<td style="width:10px">:BLABLABLA ...";

        try
        {
          
            string filepath = "C:\\Users\\Archit\\Desktop\\test2.pdf";
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));
            document.Open();
            StringReader sr = new StringReader(HTML);
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
            document.Close();
            PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }

@tarunesh-saurav
Copy link

tarunesh-saurav commented Jun 15, 2019

iTextSharp.text.Image addLogo = default(iTextSharp.text.Image);
addLogo = iTextSharp.text.Image.GetInstance(Server.MapPath("Images") + "/Content/images/asia-cloud-logo.png");
addLogo.SetAbsolutePosition(30f, 800f);
addLogo.ScaleToFit(128, 37);

your other code
document.Open();
HTMLWorker hw = new HTMLWorker(document);

            StringReader sr = new StringReader(xx.ToString());
            document.Add(addLogo);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment