0

こんにちは、 ASP.NETアプリケーションでpdf を作成したいと考えていますiTextSharp。私の考えは、HTML ファイルから PDF を作成することです。をクリックするButtonと、背景画像と多くのスタイルを含む HTML ファイルが読み込まれます。

私のアプリケーションは..

  1. Folder から HTML ファイルを読み取りますApp.Data。(System.IO を使用)
  2. このスタイルを使用して、この HTML ファイルから PDF を作成してください。( using iTextSharp.text; using iTextSharp.text.pdf;) 3. PDF ファイルをメールで送信します。(System.Net.Mail を使用; System.Net.Mime を使用;)

PDF を作成しようとしましたが、動作する場合でも、HTML ファイルでスタイルを使用すると、スタイルが表示されません:(

ここに私のコードがあります:

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ファイル:

<!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テキストのみを取得します:スタイルなしのテスト.

4

2 に答える 2

0

ここでは background-color の代わりに bgcolor を使用する必要があります

于 2013-02-05T09:09:13.230 に答える
0

「スタイル」のスペルが間違っています。

<body styles="background-color:#E0E0E0;

そのはず

<body style="background-color:#E0E0E0;
于 2013-02-05T08:44:54.453 に答える