Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用带有样式的html文件使用iTextSharp创建pdf文件_C#_Asp.net_Html_Pdf_Itextsharp - Fatal编程技术网

C# 如何使用带有样式的html文件使用iTextSharp创建pdf文件

C# 如何使用带有样式的html文件使用iTextSharp创建pdf文件,c#,asp.net,html,pdf,itextsharp,C#,Asp.net,Html,Pdf,Itextsharp,您好,我想在我的ASP.NET应用程序中创建一个pdf,为此我使用iTextSharp。我的想法是用HTML文件创建pdf。如果我点击一个按钮,我就会阅读一个带有背景图像和多种样式的HTML文件 我的申请表必须填写 从文件夹App.Data中读取HTML文件。(使用System.IO) 使用此样式从此HTML文件创建PDF!(使用iTextSharp.text; 使用iTextSharp.text.pdf;) 3.以电子邮件形式发送PDF文件。(使用System.Net.Mail;使用Syste

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

我的申请表必须填写

  • 从文件夹
    App.Data
    中读取HTML文件。(使用System.IO)
  • 使用此样式从此HTML文件创建PDF!(
    使用iTextSharp.text;
    使用iTextSharp.text.pdf;
    ) 3.以电子邮件形式发送PDF文件。(使用System.Net.Mail;使用System.Net.Mime;)
  • 我尝试创建PDF和if,但如果我在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文本:test without styles.

    您将“style”拼写错了


    你应该在这里使用bgcolor而不是背景色这将是样式而不是样式这是一个小错误但背景色不起作用使用正文中的样式标记,不随它一起使用…可能会起作用,试试吧…string html=“Test”?检查一下…我也有同样的问题,如果你得到答案,请发邮件给我。
    
    <body styles="background-color:#E0E0E0;
    
    <body style="background-color:#E0E0E0;