我如何使用与样式的HTML文件中创造一个PDF文件,iTextSharp的 [英] How I can use a html file with styles for creat a pdf file with iTextSharp

查看:95
本文介绍了我如何使用与样式的HTML文件中创造一个PDF文件,iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的 ASP.NET 应用程序创建PDF,为此我使用 iTextSharp的。我的想法是创建一个PDF出HTML文件。我想,如果我点击一个按钮我阅读背景图像和多样式的HTML文件。

Hi I want to create a pdf in my ASP.NET Application and for this I use iTextSharp. My idea is it to create a pdf out of a HTML file. I want that if I click on a Button I read a HTML file with a background-image and much styles.

我的应用程序必须做的。

My Application must do..


  1. 阅读从文件夹 App.Data HTML文件。 (使用System.IO)

  2. 创建一个PDF出这种风格此HTML文件! (使用iTextSharp.text;
    使用iTextSharp.text.pdf;
    3.send将PDF文件作为电子邮件。 (使用System.Net.Mail;采用System.Net.Mime;)

  1. Read a HTML file from the Folder App.Data. (using System.IO)
  2. Create a PDF out of this HTML file with this styles! (using iTextSharp.text; using iTextSharp.text.pdf;) 3.send the PDF file as email. (using System.Net.Mail;using System.Net.Mime;)

我尝试创建一个PDF,如果工作,但如果我使用样式在我的HTML文件中没有显示的样式:(

I try to create a PDF and if work but if I use styles in my HTML file it doesn't show the styles :(

这是我的code:

string html;

using (StreamReader reader = new StreamReader(Server.MapPath("~/App_Data/mail.html"), System.Text.Encoding.Default))
{
    html = reader.ReadToEnd(); //here i read the html file to this string
    HTMLtoPdf(html);
}

private void HTMLtoPdf(string HTML) 
{
    Document doc = new Document();
    PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\test.pdf", FileMode.Create));
    doc.Open();
    HTMLWorker hw = new HTMLWorker(doc);
    hw.Parse(new StringReader(HTML));
    doc.Close();

    ShowPdf("test.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();
}

例如我的HTML文件:

For example my HTML file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html><head>

</head><body styles="background-color:#E0E0E0; font-weight:bold; font-family:Arial; font-size:120%;">

<div>
    Test
</div>

</body></html> 

在这个例子中我使用的背景颜色,但它不工作:(我只得到HTML文本:测试无风格

In this Example I use background color but it don't work :( I get only the HTML text : test without styles.

推荐答案

你应该在这里使用的bgcolor,而不是背景颜色

you should use bgcolor here instead of background-color

这篇关于我如何使用与样式的HTML文件中创造一个PDF文件,iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆