I am working with some DWG files that were exported from Microsoft Visio and I need to convert them to PDF. The drawings use the Calibri font in Visio, but when exported to DWG the font is different and causes problems with text overlapping on the drawing. As part of the export process I would like to specify a different font to fix this problem.
I have been able to successfully export the DWG files to PDF, but no matter what I do I cannot get the font to change. I am using Aspose.CAD 25.3.0, and I have tried changing the font with:
foreach (CadStyleTableObject style in image.Styles)
{
// Set the font name
style.PrimaryFontName = "Calibri";
}
and
image.DefaultFont = "Calibri";
as well as some other suggestions I saw on the forums, but no luck.
I don’t know much about the DWG file format but I read a bit about SHX fonts and I am wondering if perhaps they are part of the problem. When I print the CAD image styles to the console I see that the font used by the drawing is txt.shx. Is it possible to replace SHX fonts with regular TTF fonts with Aspose.CAD?
I have attached a zip file with an example DWG file and a screenshot of the original drawing in Visio to show what I am trying to achieve by changing the fonts. For example, see how the string “ECY-RS485” is larger in the exported DWG than in the original Visio file.
Please let me know if there is a solution to this problem. Thanks! V+Master_Rev2.0.zip (389.6 KB)
To change the font when exporting a DWG file to PDF using Aspose.CAD, you can modify the font settings in the CAD drawing before saving it as a PDF. It seems you have already attempted to set the font using the PrimaryFontName property of the CadStyleTableObject, which is the correct approach. However, if the font is not changing as expected, there are a few things to consider.
Ensure the Font is Available: Make sure that the font you are trying to set (e.g., “Calibri”) is installed on the system where the conversion is taking place. If the font is not available, the system may fall back to a default font.
Iterate Over All Styles: You should iterate over all styles in the CAD image and set the font for each style. Here’s an example of how to do this:
using Aspose.CAD.FileFormats.Cad;
using Aspose.CAD.FileFormats.Cad.CadTables;
// Load the CAD drawing
using (CadImage cadImage = (CadImage)Image.Load("path_to_your_dwg_file.dwg"))
{
// Iterate over the styles and set the font
foreach (CadStyleTableObject style in cadImage.Styles)
{
style.PrimaryFontName = "Calibri"; // Set your desired font
}
// Save the CAD image as PDF
cadImage.Save("output_file.pdf");
}
Handling SHX Fonts: If the drawing uses SHX fonts (like txt.shx), these are typically used for text in CAD files and may not directly map to TTF fonts. You can replace SHX fonts with TTF fonts by ensuring that the styles are correctly set as shown above. However, if the text entities are specifically tied to SHX fonts, you may need to convert those text entities to use a different font type.
Check for Specific Styles: If you want to change the font for a specific style, you can check the style name and set the font accordingly:
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "YourSpecificStyleName")
{
style.PrimaryFontName = "Calibri"; // Change to your desired font
}
}
Exporting to PDF: After setting the fonts, ensure you save the CAD image in the desired format (PDF in your case).
By following these steps, you should be able to change the font in your DWG files when exporting to PDF. If you continue to experience issues, consider checking the Aspose.CAD documentation or community forums for additional troubleshooting tips.
If you have any further questions or need more assistance, feel free to ask!
@tomO8,
Let’s look at the string “ECY-RS485” for example. This is CadMText entity, that has the value “\C7;\fCalibri|b1|i0|;\H0.1875;ECY-RS485” (cadMText.Text) in the original DWG file. This value overrides the reference to the style (standard) set up for this MText so SHX font (shx.txt) does not relate to this problem, and the modification of font in styles had no effect. Thus, the appearance of this string is defined by the values encoded inside it directly: \f encodes the Calibri font, \H encodes its size. If you need to change the font you may change \f in this value (if you erase this part - the fonts from related style will be used), but as for my humble opinion it is more effective to change the size. And it seems that all MTEXT entities in the file follow the same text pattern.
Please try if this basic idea is useful:
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName)
{
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.PageHeight = 2000;
cadRasterizationOptions.PageWidth = 2000;
cadRasterizationOptions.Layouts = new string[] { "Model" };
foreach (CadEntityBase entity in cadImage.Entities)
{
if (entity is CadMText)
{
CadMText mtext = (CadMText)entity;
string input = mtext.Text;
string pattern = @"\\H(?<height>[0-9.]+)";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
// get initial height of the text
double height = double.Parse(match.Groups["height"].Value, System.Globalization.CultureInfo.InvariantCulture);
// set reduced height via regular InitialTextHeight property, not embedded inside a string
mtext.InitialTextHeight = height * 0.8;
// cut height value from the initial Text value (now the value from InitialTextHeight will be used)
mtext.Text = Regex.Replace(input, @"\\H[0-9.]+;", "");
}
}
}
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.VectorRasterizationOptions = cadRasterizationOptions;
cadImage.Save(pdfPath, pdfOptions);
}
Please also note that iterating over Entities collection is limited, you need at least free evaluation license to get access to all entitites in the drawing.
Amazing! Thanks Oleksii, your solution will allow me to fix my problem. All I need to do now is configure the height scaling factor, but that won’t be difficult.
If I want to adjust bold or italic properties of a font do I need to create a similar regular expression to change the |b1| and |i0| values? And does the \C7 control anything important?
(Thanks for the warning about iterating over the entities, I have a paid license through my workplace so I didn’t run into that issue)
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.