使用 Aspose.HTML 在 PDF 中添加 TTF 字体文件
Add a TTF font file in PDF using Aspose.HTML
我正在使用 ASPOSE.Html 将 Html 文档转换为 Pdf.I 需要添加 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);
}
}
我正在使用上面的示例 code.I 在我的 .Net 应用程序资源 folder.So 中有 Robotto.ttf 文件,在将 html 文件转换为 pdf 后它采用默认字体。( Times Roman)。那么我如何将 html 中的字体应用到 pdf 文件。
感谢您分享请求的数据。
从 Google Fonts. Please make sure that the HTML file displays the content with respective font when opened in a browser. You may modify the HTML as per your requirements. Below code sample converts the HTML to a PDF file while rendering the content exactly as it is displayed by opening the HTML file in browser. You may download the source font files and generated PDF documents from this Google Drive link 下载 Roboto 字体后,我们修改了 HTML 文件。
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 文件进行比较,方法是在任何浏览器中打开后者。如果您发现这两个文件之间存在任何差异,请随时与我们联系。我们很乐意提供帮助。
我正在使用 ASPOSE.Html 将 Html 文档转换为 Pdf.I 需要添加 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);
}
}
我正在使用上面的示例 code.I 在我的 .Net 应用程序资源 folder.So 中有 Robotto.ttf 文件,在将 html 文件转换为 pdf 后它采用默认字体。( Times Roman)。那么我如何将 html 中的字体应用到 pdf 文件。
感谢您分享请求的数据。
从 Google Fonts. Please make sure that the HTML file displays the content with respective font when opened in a browser. You may modify the HTML as per your requirements. Below code sample converts the HTML to a PDF file while rendering the content exactly as it is displayed by opening the HTML file in browser. You may download the source font files and generated PDF documents from this Google Drive link 下载 Roboto 字体后,我们修改了 HTML 文件。
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 文件进行比较,方法是在任何浏览器中打开后者。如果您发现这两个文件之间存在任何差异,请随时与我们联系。我们很乐意提供帮助。