Issue with SHX fonts

I am using aspose.cad .net for converting a DWG file to a PDF file. I can’t seem to get the SHX fonts into the PDF.

There are 3 .shx fonts used in my DWG drawing put when i export my file as PDF the SHX font is not used. I see the default linestyle in stead of the shx.
I tried several ways to “export” the SHX files, using different locations, nothing seems to work. Am i missing something?

using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(dwgFile, loadOptions))
{
string[] layouts = new[] { cadImage.ActivePage.LayoutName }; //current layout

 Console.WriteLine($"Selected layouts: {string.Join(", ", layouts)}");
 foreach (CadLayerTable layer in cadImage.Layers)
 {
     Console.WriteLine($"layer: {layer.Name}, LineWeight: {layer.LineWeight}, LineTypeName: {layer.LineTypeName}");
  //   layer.PlotFlag = true; // Ensure the layer is set to plot
 }

 // Set PDF export options
 PdfOptions pdfOptions = new PdfOptions();
 pdfOptions.PdfDocumentInfo = new Aspose.CAD.FileFormats.Pdf.PdfDocumentInfo
 {
     Title = "Converted DWG to PDF",
     Author = "myCADConverter",
     Subject = "DWG to PDF Conversion",
     Keywords = "DWG, PDF, Conversion"
 };

 GetPaperSizeInPoints(pageSize, out int pageWidth, out int pageHeight);
 string[] files = System.IO.Directory.GetFiles(@"C:\mypath\Fonts\", "*.shx", SearchOption.AllDirectories);
 // Get all SHX font names used in the drawing
 var usedShxFonts = GetUsedShxFontNames(cadImage);

 // Only include SHX files that are actually used in the drawing
 var shxFilesToUse = files
     .Where(f => usedShxFonts.Contains(Path.GetFileName(f), StringComparer.OrdinalIgnoreCase))
     .ToArray();

 var rasterOptions = new CadRasterizationOptions
 {
     //ShxFonts = shxFilesToUse,//shxfilesloc

     PageWidth = pageWidth,
     PageHeight = pageHeight,
     BackgroundColor = Color.White, // Use 'Color' from System.Drawing
     Layouts = layouts,
     DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseObjectColor,
     
     // AutomaticLayoutsScaling = true,
     //NoScaling = false,
     //ExportAllLayoutContent = true

 };

 //SHX font handling
 rasterOptions.ShxFonts = new string[]   //SHX files in same dir a drawing file
 {
     "ISO.shx",
     "isocp2.shx", // this font file is expected to be near drawing file
      "NLCS.SHX"
 };

     // Convert DWG to PDF
 pdfOptions.Layers = cadImage.Layers.GetLayersNames().ToArray();
 pdfOptions.VectorRasterizationOptions = rasterOptions;
 cadImage.Save(pdfFile, pdfOptions);

}

@RaimondL

It sounds like you’re experiencing issues with SHX fonts not rendering correctly in your PDF output from a DWG file using Aspose.CAD. Here’s a structured approach to troubleshoot and resolve this issue:

Cause: The problem may stem from inaccessible SHX font files, incorrect font names, layers not set to plot, or misconfigured rasterization options.

Solution: Follow these steps to ensure proper rendering of SHX fonts:

  1. Ensure SHX Fonts are Accessible: Confirm that the SHX font files are located in a directory that your application can access. Specify the path to the SHX files directly in your code if necessary.

  2. Check Font Names: Verify that the names of the SHX font files in your rasterOptions.ShxFonts array match exactly with those used in your DWG file, as the names are case-sensitive.

  3. Set Plot Flag: Ensure that the layers containing the SHX fonts are set to plot by setting the PlotFlag property of the layers to true.

  4. Use Correct Rasterization Options: Ensure that your CadRasterizationOptions are correctly configured. Enabling ExportAllLayoutContent can help ensure that all content, including fonts, is exported.

  5. Debugging Output: Add debugging output to check which SHX fonts are being used in the drawing. This will help confirm that the correct fonts are being loaded.

Here’s a minimal code snippet to illustrate how to set the PlotFlag and ensure the SHX fonts are included:

foreach (CadLayerTable layer in cadImage.Layers)
{
    layer.PlotFlag = true; // Ensure the layer is set to plot
}

// Set SHX fonts
rasterOptions.ShxFonts = new string[]
{
    "ISO.shx",
    "isocp2.shx",
    "NLCS.SHX"
};

Next Steps: After implementing these adjustments, attempt to convert the DWG to PDF again. If the issue persists, consult the Aspose documentation for any additional settings related to SHX fonts.

Please let me know if this resolves your issue or if you need further assistance!

@RaimondL,
Hi,
could you please attach the initial file and the PDF result you obtain on your side, so we can directly reproduce the issue?

I added a simpel file that’s shows the problem in the attached zip. Including the SHX file(s) that are used.
Issue_SHX font.zip (828.6 KB)

@RaimondL,
thank you for the details, we can confirm the issue and created CADNET-10226 to apply the fix for it.

1 Like

@RaimondL,
Hi,
could you please check if the export is better now with the latest Aspose.CAD for .NET 25.9.

Thanks for the fix. Issue solved

1 Like

@RaimondL,
we are happy to help :slight_smile: