本文介绍了使用itextsharp将HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的代码:
我第一次使用itexsharp库:

I have a code bellow:
the first i using itexsharp library:

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;



第二个我有代码c#:



the second i have code c#:

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



但我不会将图像插入字符串htmltext中.
请帮帮我.
感谢



but i don''t insert image into string htmltext.
please help me.
thanks

推荐答案

 Dim jpeg As iTextSharp.text.Image
 Dim _Chunk As Chunk
jpeg = iTextSharp.text.Image.GetInstance(imagepath)
_Chunk = New Chunk(jpeg, 0, 0)
document.Add(_Chunk);



这会起作用!!!

谢谢



This will works!!!

Thanks,


这篇关于使用itextsharp将HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 01:49