cart: empty [ Login ]

Export DXF to PDF with embed fonts

1 2  next > 
EPLAN Solution
10/24/2019 7:34 AM
Hello, When we try export our DXF file to PDF file, the PDF file shows question marks for Japanese text (every arbitrary text with Asian characters). But if the text is not bold and italic, it is exported well. Is our code OK? DXF file Exported PDF file
EPLAN Solution
10/24/2019 7:52 AM
DXF file: Exported PDF file: Code:
Code:
[csharp] using System; using System.Collections.Generic; using System.Linq; using System.Drawing; using System.Drawing.Printing; using System.IO; using WW.Cad.Model; using WW.Cad.IO; using WW.Cad.Drawing; using WW.Drawing; using WW.Math; namespace DXFExportToPDF { class Program { static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: DXFExport <DXF file>"); Environment.Exit(0); } string filename = args[0]; DxfModel model = DxfReader.Read(filename); string outfile = Path.GetFileNameWithoutExtension(Path.GetFullPath(filename)); GraphicsConfig gc = new GraphicsConfig(false, ArgbColors.White) { TryDrawingTextAsText = true }; Bounds3D bounds = GetBounds(model); PaperSize ps; int width = ConvertToHundredthsOfInch(bounds.Delta.X); int height = ConvertToHundredthsOfInch(bounds.Delta.Y); width += 100; // In case of a pdf, half inch margin is "created" around the graphics, so an inch must be added to width (half of inch on right side and half of inch on left side). height += 100; // The same for height. Size pdfSize = LimitPdfSize(width, height); ps = new PaperSize(PaperKind.Custom.ToString(), pdfSize.Width, pdfSize.Height); Matrix4D transform = GetTransformto2D(bounds, ps.Width, ps.Height); double dpmm = 120 / 25.4; // Dots per milimeter using (Stream s = File.Create(outfile + ".pdf")) { PdfExporter pe = new PdfExporter(s); // For some fonts and unicode characters (cyrillic) the export will not work properly, // therefore the PDF exporter also supports font embedding. pe.EmbedFonts = true; pe.DrawPage(model, gc, transform, ps); pe.EndDocument(); } } private static Bounds3D GetBounds(DxfModel model) { BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model); return boundsCalculator.Bounds; } private static int ConvertToHundredthsOfInch(double mm) { double hundredsOfInch = mm / 0.254; double floor = Math.Floor(hundredsOfInch); double ceiling = Math.Floor(hundredsOfInch); return Math.Abs(hundredsOfInch - floor) < Math.Abs(hundredsOfInch - ceiling) ? ((int)floor) : ((int)ceiling); } /// <summary>Returns the size for an PDF 1.4 document. It makes the size smaller if it exceeds the PDF 1.4 maximum size limit (which is 200 inches).</summary> /// <param name="Width">The width of the pdf in hundreds of inches (1/100 of an inch).</param> /// <param name="height">The height of the pdf in hundreds of inches (1/100 of an inch).</param> /// <returns>The size (in hundreds of an inch) for the PDF to comply with PDF 1.4 size limitation. It keeps the original size aspect ratio.</returns> private static Size LimitPdfSize(int Width, int height) { if (Width > 20000 || height > 20000) { double widthRatio = ((double)Width) / 20000; double heightRatio = ((double)height) / 20000; if (widthRatio >= heightRatio) { return new Size((int)(Width / widthRatio), (int)(height / widthRatio)); } else { return new Size((int)(Width / heightRatio), (int)(height / heightRatio)); } } else { return new Size(Width, height); } } private static Matrix4D GetTransformto2D(Bounds3D bounds, double pageWidth, double pageHeight, double fac = PdfExporter.InchToPixel) { double margin = 0.5 * fac; // half inch -> to mm pageWidth *= 0.01 * fac; pageHeight *= 0.01 * fac; Matrix4D to2DTransform = WW.Cad.Base.DxfUtil.GetScaleTransform( bounds.Corner1, bounds.Corner2, bounds.Center, new Point3D(margin, margin, 0d), new Point3D(pageWidth - margin, pageHeight - margin, 0d), new Point3D(0.5 * pageWidth, 0.5 * pageHeight, 0) ); return to2DTransform; } } } [/csharp]
Wout
11/3/2019 9:15 AM
Hi, It took me a couple of hours to dig into this problem and have concluded it's not a CadLib bug, but did find a way to export these Japanese characters. Can you please order a support ticket here? - Wout
Wout
11/6/2019 11:07 PM
Hi, The text is using the DengXian font, and the text contains japanese/chinese characters. If the font is not installed, CadLib will use the Arial font, and the Arial font does not contain any japanese/chinese characters, so they won't be rendered. If you install the Dengxian Font Pack, CadLib can find the font and will render the text correctly. https://www.microsoft.com/en-us/download/details.aspx?id=49113 Note that somehow in GDI+ the text is rendered correctly using the Arial font, but this seems to be an implementation detail where Microsoft will find the preferred chinese/japanese font installed on the system, and it will use that. I can't really find definite/detailed information on how the GDI+ font fall backs are implemented by Microsoft. - Wout
EPLAN Solution
11/11/2019 8:51 AM
This solution doesn't work. DengXian font I have properly installed. When I downloaded from your url and tried install it, I get this message: DXF file was generated through CadLib on my system too and font there is ok.
Wout
11/11/2019 9:00 AM
I tested this on Windows 7, I'll have a look at it again on Windows 10 then. - Wout
Wout
11/11/2019 1:02 PM
I've tried finding the font on Windows 10, and in theory it should be part of the Simplified Chinese language, which can be added under Windows 10 settings -> Language -> Chinese (Simplified, China) (according to this and this), but on my system that does not add the DengXian fonts (you can check that by looking in C:\Windows\Fonts). The aforementioned language pack does not install on Windows 10, but it only fails to install because of the Windows version check, not because it actually found the DengXian font. So, it's probably not very helpful for you, but technically I don't see how it's a CadLib problem. When the font is present on the system, the pdf output will be fine. - Wout
EPLAN Solution
11/12/2019 8:02 AM
And can you tell me, why exporting to DXF using CadLib is without any errors, but export this DXF to PDF cause this problem?
Wout
11/12/2019 9:01 AM
Export to DXF doesn't render anything, you can specify non-existent fonts and the DXF will still be valid.
EPLAN Solution
11/12/2019 10:22 AM
Thank you for your help. Problem was solved. :D In our company computers are not installed whole font set. We have installed only DengXian Regular. After installing missing fonts DengXian Bold and DengXian Light is everything allright.
1 2  next >