使用Aspose.HTML以PDF格式添加TTF字体文件

时间:2018-04-18 05:45:06

标签: c# .net fonts aspose

我正在使用ASPOSE.Html将Html文档转换为Pdf。我需要添加一个TTF字体文件,该文件位于我的应用程序的资源文件夹中。 我正在关注此示例Html to Pdf。 我浏览了文档,我找不到在html文档中添加字体的示例。 这是代码 -

string dataDir = RunExamples.GetDataDir_Data();

String SimpleStyledFilePath = dataDir + "FirstFile.html";
using (FileStream fs = File.Create(SimpleStyledFilePath))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
    @"<style>
    body {
        font-family: 'roboto';
     }
    .st
    {
    color: green;
    }
    </style>
    <div id=id1>Aspose.Html rendering Text in Black Color</div>
    <div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
    <div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
    <div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>
    ");
   } 

string pdf_output;
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer pdf_renderer = new 
Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML 
file
using (Aspose.Html.HTMLDocument html_document = new 
Aspose.Html.HTMLDocument(SimpleStyledFilePath))
{
 // Set the page size less than document min-width. The content in the 
 resulting file will be cropped becuase of element with width: 200px
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new 
Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
    PageSetup =
    {
        AnyPage = new Aspose.Html.Drawing.Page(new 
Aspose.Html.Drawing.Size(100, 100)),
        AdjustToWidestPage = false
    },
};
pdf_output = dataDir + "not-adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
    // Render the output
    pdf_renderer.Render(device, html_document);
}

// Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
    PageSetup =
    {
        AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
        AdjustToWidestPage = true
    },
};
pdf_output = dataDir +  "adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
    // Render the output
    pdf_renderer.Render(device, html_document);
}
}

我正在使用上面的示例代码。我的.Net应用程序资源文件夹中有Robotto.ttf文件。因此,在将html文件转换为pdf之后,它采用默认字体。(Times Roman)。那么我将如何应用字体html到pdf文件。

1 个答案:

答案 0 :(得分:1)

感谢您分享所请求的数据。

我们从Google Fonts下载Roboto字体后修改了HTML文件。在浏览器中打开时,请确保HTML文件显示具有相应字体的内容。您可以根据您的要求修改HTML。下面的代码示例将HTML转换为PDF文件,同时通过在浏览器中打开HTML文件来完全显示内容。您可以从this Google Drive link下载源字体文件和生成的PDF文档。

dataDir = dataDir + "Roboto\\";
String SimpleStyledFilePath = dataDir + "FirstFile_Aspose.HTML.html";
using (FileStream fs = File.Create(SimpleStyledFilePath))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<style>
@font-face { font-family: Roboto; src: url('Roboto-Bold.ttf'); }
body {
    font-family: 'Roboto';
 }
.st
{
color: green;
}
</style>
<div id=id1>Aspose.Html rendering Text in Black Color</div>
<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
<div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
<div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>
");
} 

string pdf_output;
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer pdf_renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
{
 // Set the page size less than document min-width. The content in the  resulting file will be cropped becuase of element with width: 200px
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
    PageSetup =
    {
        AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),        AdjustToWidestPage = false
    },
};
pdf_output = dataDir + "not-adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
    // Render the output
    pdf_renderer.Render(device, html_document);
}

// Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
    PageSetup =
    {
        AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),        AdjustToWidestPage = true
    },
};
pdf_output = dataDir +  "adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
    // Render the output
    pdf_renderer.Render(device, html_document);
}
}

您可以在任何浏览器中打开后者,将生成的PDF文件与HTML文件进行比较。如果您发现这两个文件有任何差异,请随时与我们联系。我们很乐意提供帮助。