使用iTextSharp将HTML转换为PDF

时间:2019-08-22 13:55:06

标签: c# html pdf fonts itext

我已经使用iTextSharp将html文件转换为pdf。在html文件中,我正在使用字体家族的自定义字体文件ttf

以下是html代码。

<html>
<head>
    <style type="text/css">
        @font-face {
            font-family: "Univers Regular";
            font-style: normal;
            font-weight: 400;
            src: url("/Test/Fonts/UniversNext/w02/UniversNextforHSBCW02-Rg.woff");
        }

        .regular {
            font-family: 'Univers Regular';
        }
    </style>
</head>
<body style="font-family:'Univers Regular'">
    <table style="border-spacing:0px;width:695px;margin-bottom:40px;" cellpadding="0" cellspacing="0">
        <tr>
            <td style="width:11px;"></td>
            <td style="text-align:left;">
                Test HTML to PDF
            </td>
        </tr>
</body>
</html>

以及下面的c#代码将html文件转换为pdf文档

string pdfPath = @"C:\pdfFile.pdf";

using (FileStream stream = new FileStream(pdfPath, FileMode.Create))
{
    Document pdfDoc = new Document(PageSize.A4);
    pdfDoc.SetMargins(40, 40, 40, 80);


    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
    pdfDoc.Open();


    //var xmlWorkerFontProvider = new XMLWorkerFontProvider();
    //xmlWorkerFontProvider.Register("/HSBC/Fonts/UniversNext/w02/UniversNextforHSBCW02-Rg.ttf");

    StringReader sr = new StringReader(fileContent.ToString());
    XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);

    pdfDoc.Close();
    stream.Close();
}

一切正常,我唯一的问题是,字体系列未得到应用。它始终显示iTextSharp随附的默认字体。

请帮助!

0 个答案:

没有答案
相关问题