代码之家  ›  专栏  ›  技术社区  ›  Namdeo Karande

使用Aspose在PDF中添加TTF字体文件。HTML

  •  0
  • Namdeo Karande  · 技术社区  · 6 年前

    我正在使用ASPOSE。Html将Html文档转换为Pdf。我需要添加一个TTF字体文件,它位于我的应用程序的resources文件夹中。 我遵循这个例子 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);
    }
    }
    

    我正在使用上述示例代码。我有Robotto。我的中的ttf文件。Net应用程序资源文件夹。因此,将html文件转换为pdf后,它采用默认字体。(罗马时代)。那么,如何将html中的字体应用于pdf文件呢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Farhan Raza    6 年前

    感谢您共享请求的数据。

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

    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);
    }
    }
    

    通过在任何浏览器中打开HTML文件,可以将生成的PDF文件与HTML文件进行比较。如果您注意到这两个文件之间有任何差异,请随时与我们联系。我们很乐意帮忙。