使用ITextSharp无法在PDF中实现样式

时间:2018-09-11 06:31:01

标签: c# itext pdf-generation xmlworker

我正在尝试使用ITextSharp从HTML字符串生成PDF。但是外观样式没有得到应用。我在stackoverflow上查看了与之相关的帖子,但没有得到任何帮助。 请帮我如何将pdf中的HTML转换为HTML UI中显示的实际样式。样式标记中包含CSS。 这是我的C#代码

public static string GeneratePDF(string html, string cssText="")
        {
            try
            {
               // var cssText = "";
                Guid random_guid;
                random_guid = Guid.NewGuid();
                string fileName = random_guid.ToString() + ".pdf";
                string filename_with_folder = @"Temp\" + fileName;
                string fullFilePath = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, filename_with_folder);
                using (var memoryStream = new MemoryStream())
                {
                    var doc = new Document();
                    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(fullFilePath, FileMode.Create));
                    doc.Open();
                    using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
                    {
                        using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
                        {
                            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, htmlMemoryStream, cssMemoryStream);
                        }
                    }
                    doc.Close();
                    fileName = Utility.UploadFileToAzure(fullFilePath, fileName, "application/pdf ");
                    return fileName;
                }
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogError(ex);
            }
            return string.Empty;
        }

这里是带有HTMl字符串和CSS的代码

   public void test()
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder css = new StringBuilder();
            css.Append(@"<style type='text/css'>

        body{font-family: Segoe, 'Segoe UI', 'DejaVu Sans', 'Trebuchet MS', Verdana, 'sans-serif'; background: #f2f2f2; margin: 0; padding: 0; font-size: 20px; color: #565656;}
        .outerpdfboxCont{width: 100%; background: #fff; margin: 0 auto; padding: 15px 30px; max-width: 80%; display: table;box-shadow: 1px 1px 2px #ddd;}   

        .blueBorderLine{background: #0A82B4; height: 10px; width: 100%; margin: 10px auto;}

        .workingWrapperboxPdf{margin: 10px 0; padding: 10px 0px; display: table;width: 100%; }
        .workingWrapperboxPdf h2{font-size: 36px; font-weight: 500; margin: 40px 0 0; line-height: 40px; color: #000;}
        .workingWrapperboxPdf h3{font-size: 18px; font-weight: 300;line-height: 20px;}

        .darkB{color: #000!important; font-weight: 500!important;}
        .DataboxH{margin: 20px 0; display: table;width: 100%;}
        .border-heading{border-bottom: 1px solid #ddd; font-size: 30px!important; line-height: 35px!important; color: #000!important;width: 100%; padding-bottom: 10px;}

    </style>");
            sb.Append(@"<body>
<div class='outerpdfboxCont'>   
    <div class='blueBorderLine'></div>  

<div class='workingWrapperboxPdf'>  

    <h3>Dalvir Singh</h3>   
    <h3 class='darkB'>India</h3>

    <div class='DataboxH'>
        <h2>Summery Of the PDF</h2>
        <h3 class='darkB'>Summery of pdf will go here</h3>
        <h3>PDF created on 2018-6-12</h3>
    </div>
</div>  
</div>
</body>");
GeneratePDF(sb.ToString(),css.ToString());
        }

1 个答案:

答案 0 :(得分:1)

这是旧版本的iText for .NET的已知问题。

  

两年前,我们放弃了 iTextSharp 这个名称,取而代之的是 .NET的iText

我已经使用了您的HTML:

<html>
<head>
<style type='text/css'>

        body{font-family: Segoe, 'Segoe UI', 'DejaVu Sans', 'Trebuchet MS', Verdana, 'sans-serif'; background: #f2f2f2; margin: 0; padding: 0; font-size: 20px; color: #565656;}
        .outerpdfboxCont{width: 100%; background: #fff; margin: 0 auto; padding: 15px 30px; max-width: 80%; display: table;box-shadow: 1px 1px 2px #ddd;}   

        .blueBorderLine{background: #0A82B4; height: 10px; width: 100%; margin: 10px auto;}

        .workingWrapperboxPdf{margin: 10px 0; padding: 10px 0px; display: table;width: 100%; }
        .workingWrapperboxPdf h2{font-size: 36px; font-weight: 500; margin: 40px 0 0; line-height: 40px; color: #000;}
        .workingWrapperboxPdf h3{font-size: 18px; font-weight: 300;line-height: 20px;}

        .darkB{color: #000!important; font-weight: 500!important;}
        .DataboxH{margin: 20px 0; display: table;width: 100%;}
        .border-heading{border-bottom: 1px solid #ddd; font-size: 30px!important; line-height: 35px!important; color: #000!important;width: 100%; padding-bottom: 10px;}

    </style>
</head>
<body>
<div class='outerpdfboxCont'>   
    <div class='blueBorderLine'></div>  

<div class='workingWrapperboxPdf'>  

    <h3>Dalvir Singh</h3>   
    <h3 class='darkB'>India</h3>

    <div class='DataboxH'>
        <h2>Summery Of the PDF</h2>
        <h3 class='darkB'>Summery of pdf will go here</h3>
        <h3>PDF created on 2018-6-12</h3>
    </div>
</div>  
</div>
</body>
</html>

我已经在浏览器中将其打开:

enter image description here

然后我服用了iText 7pdfHTML add-on

我执行了以下代码行

HtmlConverter.ConvertToPdf(src, dest);

在这一行中,src表示HTML文件,dest表示将基于HTML创建的PDF。

这就是生成的PDF的样子:

enter image description here

如您所见,样式受到尊重。关于字体系列:您可以通过创建ConverterProperties对象(使转换器访问字体库)来获得优先级更高的字体之一。有关该功能的更多信息,请阅读chapter 6 of the PDF to HTML tutorial

总结:升级到iText 7 + pdfHTML附加组件后,您的问题将消失。如the introduction of the HTML to PDF tutorial中所述,您正在使用的旧XMLWorkerHelper已不再受支持。