HTML to PDF for .NET set default FONT-FAMILY

How do I set the default FONT-FAMILY when converting from HTML to PDF using ASPOSE.TOTAL for .NET?

@jdacz

Could you please share some more detail about your requirement? HTML document may contains multiple fonts style. Do you want to change the font of whole document while converting it to PDF?

Please share your input HTML and expected output PDF. We will then provide you more information on it.

Hi @tahir.manzoor,

Yes, we want the whole document to use a font-family:serif.

I currenlty have this test html,

<html>
<head>
<Title>This is SERIF</Title>
<style>
html * {
font-family: serif;
}
</style>
</head>

<body>
<h1>A Lorem Ipsum H1</h1>
<h2>B Lorem Ipsum H2</h2>
<h3>C Lorem Ipsum H3</h3>
<p>D Lorem Ipsum p</p>
</body>
</html>

Please see image for the converted PDF.forAspose.zip (65.4 KB)

Currently, the resulting pdf document looks like San-serif (which is not required). We would like a font a little more “serif” like Times New Roman, etc.

Please provide code for setting a generic font-family: serif or for a more specific font like Times New Roman so we can try.

FYI, we are using Aspose.PDF Version=19.3.0

@jdacz

Please use the following code example to achieve your requirement. Hope this helps you.

string newName = "Arial";
using (Document pdfDocument = new Document(MyDir + "input.html", new HtmlLoadOptions()))
{
    TextFragmentAbsorber tfa = new TextFragmentAbsorber(@"");
    tfa.TextSearchOptions.IsRegularExpressionUsed = true;
    pdfDocument.Pages.Accept(tfa);
    TextFragmentCollection tfc = tfa.TextFragments;
    foreach (TextFragment tf in tfc)
    {
        tf.TextState.Font = FontRepository.FindFont(newName);
    }

    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    pdfDocument.Save(dataDir + "output_out.pdf", pdfSaveOptions);
}
1 Like