반응형

itextsharp.dll


itextsharp.pdfa.dll


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;


public partial class _Default : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 

        }
    }
 
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        String htmlText = "<font  " +
         " color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
         " color=\"black\"><br><br>Some text here<br><br><br><font   " +
         " color=\"#0000FF\"><b><i>Another title here   " +
         " </i></b></font><font   " +
         " color=\"black\"><br><br>Text1<br>Text2<br><OL><LI><DIV Style='color:green'>Pham Duy Hoa</DIV></LI><LI>how are u</LI></OL><br/>"+
         "<table border='1'><tr><td style='color:red;text-align:right;width:20%'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>ASFAFA</td></tr><tr><td style='color:red;text-align:right'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>DAFSDGAFW</td></tr></table><br/>"+
         "<div><ol><li>123456</li><li>123456</li><li>123456</li><li>123456</li></ol></div>";
 

 
        HTMLToPdf(htmlText, "PDFfile.pdf");
    }
 
    public void HTMLToPdf(string HTML, string FilePath)
    {
        Document document = new Document();
 
        PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Chap0101.pdf", FileMode.Create));
        document.Open();
        Image pdfImage = Image.GetInstance(Server.MapPath("logo.png"));
 
        pdfImage.ScaleToFit(100, 50);
 
        pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);
 
        document.Add(pdfImage);
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(HTML));
        document.Close();
        ShowPdf("Chap0101.pdf");
    }
    private void ShowPdf(string s)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + s);
        Response.ContentType = "application/pdf";
        Response.WriteFile(s);
        Response.Flush();
        Response.Clear();
    }
}

출처 : http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp

html 내용을 pdf로 변환해야하는 상황이 생겼다.

iTextSharp가 탁월한 해결법이 되었다.

첨부된 두 dll파일을 참조해서 예제를 실행하면 된다.

참고로 이소스가 필요했던 이유는 그래프와 html table셑을 하나의 결과물로 받아보기를 원하는 고객의 요구에 부응하기 

위해서 였다. mschart를 써서 그래프를 그린후 이미지파일로 저장하고 이를 다시 PDF문서에 그래프 + <table></table>구조로

파싱하는 구조다. 이거 알아내는데 하루가 꼬박 걸렸다.

+ Recent posts