cart: empty [ Login ]

Unable to export PDF from DWG file

1 
Sachin Tripathi
10/31/2019 7:34 AM
Hi, I'm creating the DWG file using WW.CadLib and then using the sample code to export PDF from DWG file but generated PDF does not open. I am sharing that DWG file and sample code. Please suggest.
C# Code:
public static void ExportToPdf(DxfModel model) { string filename = Path.GetFileName(model.Filename); string dir = Path.GetDirectoryName(model.Filename); string filenameNoExt = Path.GetFileNameWithoutExtension(filename); // as PDF using (FileStream stream = File.Create(Path.Combine(dir, filenameNoExt + ".pdf"))) { PdfExporter pdfExporter = new PdfExporter(stream); pdfExporter.EmbedFonts = true; GraphicsConfig config = (GraphicsConfig)GraphicsConfig.WhiteBackgroundCorrectForBackColor.Clone(); config.DisplayLineTypeElementShapes = true; config.TryDrawingTextAsText = true; foreach (DxfLayout layout in model.OrderedLayouts) { if (layout.Name != "Model") { AddLayoutToPdfExporter(pdfExporter, config, model, null, layout); } } pdfExporter.EndDocument(); } } public static void AddLayoutToPdfExporter(PdfExporter pdfExporter, GraphicsConfig config, DxfModel model, DxfView modelView, DxfLayout layout) { Bounds3D bounds; const float defaultMargin = 0.5f; float margin = 0f; PaperSize paperSize = null; bool useModelView = false; bool emptyLayout = false; if (layout == null || !layout.PaperSpace) { // Model space. BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model); bounds = boundsCalculator.Bounds; if (bounds.Initialized) { if (bounds.Delta.X > bounds.Delta.Y) { paperSize = PaperSizes.GetPaperSize(PaperKind.A4Rotated); } else { paperSize = PaperSizes.GetPaperSize(PaperKind.A4); } } else { emptyLayout = true; } margin = defaultMargin; useModelView = modelView != null; } else { // Paper space layout. Bounds2D plotAreaBounds = layout.GetPlotAreaBounds(); bounds = new Bounds3D(); emptyLayout = !plotAreaBounds.Initialized; if (plotAreaBounds.Initialized) { bounds.Update((Point3D)plotAreaBounds.Min); bounds.Update((Point3D)plotAreaBounds.Max); if (layout.PlotArea == PlotArea.LayoutInformation) { switch (layout.PlotPaperUnits) { case PlotPaperUnits.Millimeters: paperSize = new PaperSize(Guid.NewGuid().ToString(), (int)(plotAreaBounds.Delta.X * 100d / 25.4d), (int)(plotAreaBounds.Delta.Y * 100d / 25.4d)); break; case PlotPaperUnits.Inches: paperSize = new PaperSize(Guid.NewGuid().ToString(), (int)(plotAreaBounds.Delta.X * 100d), (int)(plotAreaBounds.Delta.Y * 100d)); break; case PlotPaperUnits.Pixels: // No physical paper units. Fall back to fitting layout into a known paper size. break; } } if (paperSize == null) { if (bounds.Delta.X > bounds.Delta.Y) { paperSize = PaperSizes.GetPaperSize(PaperKind.A4Rotated); } else { paperSize = PaperSizes.GetPaperSize(PaperKind.A4); } margin = defaultMargin; } } } if (!emptyLayout) { // Lengths in inches. float pageWidthInInches = paperSize.Width / 100f; float pageHeightInInches = paperSize.Height / 100f; double scaleFactor; Matrix4D to2DTransform; if (useModelView) { to2DTransform = modelView.GetMappingTransform( new Rectangle2D( margin * PdfExporter.InchToPixel, margin * PdfExporter.InchToPixel, (pageWidthInInches - margin) * PdfExporter.InchToPixel, (pageHeightInInches - margin) * PdfExporter.InchToPixel), false); scaleFactor = double.NaN; // Not needed for model space. } else { to2DTransform = DxfUtil.GetScaleTransform( bounds.Corner1, bounds.Corner2, new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d), new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidthInInches - margin, pageHeightInInches - margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidthInInches / 2d, pageHeightInInches - margin, 0d) * PdfExporter.InchToPixel), out scaleFactor ); } if (layout == null || !layout.PaperSpace) { pdfExporter.DrawPage(model, config, to2DTransform, paperSize); } else { pdfExporter.DrawPage(model, config, to2DTransform, scaleFactor, layout, null, paperSize); } } }
Sachin Tripathi
11/1/2019 5:26 AM
Hi Wout, Any update on this. Regards, Sachin
Wout
11/3/2019 9:39 AM
Hi Sachin, The property DxfLayout.PlotArea wasn't setup properly for plotting. When creating your DWG file, please set this property:
C# Code:
layout.PlotArea = PlotArea.LayoutInformation;
By default it's set to PlotArea.DrawingExtents, which means it tries to get the extents from DxfLayout.Extents, which was empty, and then you get a divisions by zero and end up with NaN coordinates in the pdf. I'll update the example code as this was missing from the example code. - Wout
Wout
11/3/2019 9:50 AM
Actually upon further investigation the default value for this property should be PlotArea.LayoutInformation. I will change this in a future release, but for now you can change the property in your program yourself like in my previous reply. - Wout
Sachin Tripathi
11/3/2019 12:20 PM
Thanks, Wout! It is working absolutely fine now.
1