ASPOSE PDF for .NET to Generate PDF doc from XML Template

Hello,

Our organization uses ASPOSE.PDF for .NET to generate PDF documents from XML templates. This is done through an MS Dynamics CRM Plugin that uses Aspose PDF DLL’s to generate the PDF for a record within our Dynamics CRM application.

Questions:

  1. Is there a way to declare font in our plugin which would overwrite any fonts declared\not declared in XML template? We are trying to avoid adding a specific font in each node of the XML template.

  2. If we need to use specific fonts in each section of our XML template, then what are the specific variables in each XML node where a specific font needs to be declared? We are trying to avoid relying on .NET call for fonts on the host infrastructure where this plugin is executing.

  3. Does Aspose have an option to provide font library along with the Aspose PDF DLL so that we can simply reference it and use the fonts from it to generate PDF documents without having a dependency on the host infrastructure where this plugin is executing?

Below is an example of an error we’re getting:

System.ApplicationException: The Font ‘Times New Roman’ is not found. This font is not supported. —> System.ApplicationException: Font ‘Times New Roman’ not found. Please make sure the customer font file is correctly set.
at x0b7ad6e52a73357d.x5da63d1eb3319fa3.x9822cded961704e0(x87d2b31775d1d42a x137264521dd371d9, String x6f02b6a80bf6b36f, String x26398f6317998fd4, Boolean x24a4f325a5a909dd)
at x0b7ad6e52a73357d.xb806034c751f68ed.x4857db63d98e82bb(x87d2b31775d1d42a x137264521dd371d9, String x9e9070c6c983bbc0, String xff3edc9aa5f0523b, Int32 xfaf78ee5769a38d3, Boolean xb0c2ce71ea178326, Boolean xeac181883dccb27a, String x2c9aeb5371cb2290, Boolean x5be1cad1d00af914)
at x0b7ad6e52a73357d.xb806034c751f68ed.xa6fd3ca8e97dc261(x87d2b31775d1d42a x137264521dd371d9, String x9e9070c6c983bbc0, String xff3edc9aa5f0523b, Int32 xfaf78ee5769a38d3, Boolean xb0c2ce71ea178326, Boolean xeac181883dccb27a, String x2c9aeb5371cb2290, Boolean x5be1cad1d00af914)
— End of inner exception stack trace —
at x0b7ad6e52a73357d.xb806034c751f68ed.xa6fd3ca8e97dc261(x87d2b31775d1d42a x137264521dd371d9, String x9e9070c6c983bbc0, String xff3edc9aa5f0523b, Int32 xfaf78ee5769a38d3, Boolean xb0c2ce71ea178326, Boolean xeac181883dccb27a, String x2c9aeb5371cb2290, Boolean x5be1cad1d00af914)
at x0b7ad6e52a73357d.x87d2b31775d1d42a.xa6fd3ca8e97dc261(String x9e9070c6c983bbc0, String xff3edc9aa5f0523b, Int32 xfaf78ee5769a38d3, Boolean xb0c2ce71ea178326, Boolean xeac181883dccb27a, String x2c9aeb5371cb2290, Boolean x5be1cad1d00af914)
at x3f91538e8c0de749.x8df46aaf181a62d1.x9bfc414be73c292c(Text xb41faee6912a2313, Pdf x6beba47238e0ade6, xb912e9429339cb81 x180c9e166d8a6fb2)
at x3f91538e8c0de749.x8df46aaf181a62d1.xe5e58ce41d63ba98(Pdf x6beba47238e0ade6, Section x4c742376f4cbaf27, Table x1ec770899c98a268, Row xa806b754814b9ae0, Cell xe6de5e5fa2d44af5, Text xb41faee6912a2313, x33eef31791ddfc27 xb4012f98512a2f3d, xbfecfdaff4b493fe x96b3b0aca69853de, Boolean xab33bbc758d17f6b)
at x3f91538e8c0de749.x44928f367a7563fb.xd22cb714335f8d2c(Pdf x6beba47238e0ade6, Section x4c742376f4cbaf27, HeaderFooter x105be60320c854bc, Table x1ec770899c98a268, Row xa806b754814b9ae0, Cell xe6de5e5fa2d44af5, Text xb41faee6912a2313, x33eef31791ddfc27 xb4012f98512a2f3d, xbfecfdaff4b493fe x96b3b0aca69853de, Boolean x70e701cee22724e3)
at x3f91538e8c0de749.x7576dadd7c449efa.x8810991188d773db(Pdf x6beba47238e0ade6, Section xb32f8dd719a105db, x33eef31791ddfc27 xb4012f98512a2f3d)
at x3f91538e8c0de749.x6a7cd842e95518d7.xc5e548c04b18e6e2(Pdf x6beba47238e0ade6)
at x3f91538e8c0de749.xaa36f84dba9d652f.x5ec1518f85fc1bbf(xb912e9429339cb81 x80010f902a1f736e, Pdf x6beba47238e0ade6)
at Aspose.Pdf.Pdf.Save(Stream stream)
at MY.CODE.Libraries.PDF.PDFViewBase.Render(IOrganizationService crmService)

@Thorn

Cause

The error you’re encountering indicates that the specified font, “Times New Roman,” is not found in the environment where your plugin is executing. This can happen if the font is not installed on the server or if the Aspose.PDF library cannot access it.

Solution

  1. Overriding Fonts in the Plugin: You can set a default font for your PDF generation by using the FontRepository class in Aspose.PDF. This allows you to specify a font that will be used throughout your document, overriding any font settings in the XML template.

    // Set a default font
    FontRepository.Sources.Add(new FontSource("path/to/your/font.ttf"));
    
  2. Specifying Fonts in XML Template: If you need to specify fonts in your XML template, you can use the Font attribute within each text node. The specific variable to declare the font in each XML node would typically look like this:

    <Text Font="Times New Roman">Your text here</Text>
    

    Ensure that the font name matches exactly with the font file name.

  3. Bundling Fonts with Aspose PDF DLL: Aspose does not provide a built-in font library that you can reference directly. However, you can include the required font files with your application and load them at runtime using the FontRepository as shown above. This way, you avoid dependencies on the host infrastructure.

Code Example

Here’s a minimal example of how to set a default font and load a specific font file:

using Aspose.Pdf;
using Aspose.Pdf.Text;

// Load the PDF document
Document pdfDocument = new Document("input.pdf");

// Add a font source
FontRepository.Sources.Add(new FontSource("path/to/your/font.ttf"));

// Create a TextFragment and set the font
TextFragment textFragment = new TextFragment("Hello World");
textFragment.TextState.Font = FontRepository.FindFont("YourFontName");

// Add the text fragment to a page
pdfDocument.Pages[1].Paragraphs.Add(textFragment);

// Save the document
pdfDocument.Save("output.pdf");

Next Steps

  1. Ensure that the font files you need are included in your project and accessible at runtime.
  2. Modify your XML template to include the font declarations as needed.
  3. Test the PDF generation again to confirm that the font issue is resolved.

If you have any further questions or need additional assistance, please let me know!