cart: empty [ Login ]

Export to Pdf current view

1 
Marcellus Zebra
7/25/2013 8:10 AM
Hi, is possible to export to pdf the current view of cad file. tipically the user zoom and pan the view, so click export button and we want to obtain a pdf with the current view fragment of model. Thank's marcello.
rammi
7/25/2013 1:18 PM
At the moment I see no perfectly easy way to achieve this. The only way which comes to my mind is to create a DxfLayout/DxfViewport combination which is constructed from the current view shown in the window. Then print this layout/viewport to PDF. - Rammi
Marcellus Zebra
9/10/2013 11:23 AM
Hi Rammi, thank's very mutch. is impossible for you to create a little example of this process? I tryed to use your ideas without success... Thank's, marc.
Wout
11/26/2013 5:12 PM
Hi Marc, For unexperienced users the matrix transformations can be a little bit intimidating but you don't need to understand all the details to use it. The DWG DXF Sharp Viewer also does not contain export to PDF using a clip rectangle. In the topic that Rammi answered using a layout/viewport, that is one option. You could also use the PDF clipping, but the PDF would be bigger than necessary ofcourse, especially if you are clipping most of the drawing away. For clipping use PdfPageConfiguration.ClipRectangle in the PdfExporter.DrawPage. In general you'll need to do the following:
  • Transform the clip rectangle from the display coordinate system (DCS) to the world coordinate system (WCS). Use the inverse of the projection transform (GDIGraphics2D.To2DTransform.GetInverse()).
  • Now you know the clip rectangle in WCS. You now can calculate the matrix to fit this rectangle onto your PDF page rectangle. Use DxfUtil.GetScaleTransform like demonstrated in the PdfExporter example code. From now on the code is mostly similar to the PdfExporter example.
  • With the WCS to PDF transform, you can also calculate the clip rectangle in PDF space.
Below is an example of how to do this:
C# Code:
/// <summary> /// Exports the current view to a PDF file. /// </summary> public void ExportViewToPdf() { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "PDF file (*.pdf)|*.pdf"; if (dialog.ShowDialog() == DialogResult.OK) { Matrix4D dcsToWcsTransform = gdiGraphics3D.To2DTransform.GetInverse(); Rectangle2D clipRectangleWcs = new Rectangle2D( dcsToWcsTransform.Transform(new Point2D(ClientRectangle.Left, ClientRectangle.Bottom)), dcsToWcsTransform.Transform(new Point2D(ClientRectangle.Right, ClientRectangle.Top)) ); PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4); Size2D paperSizePdf = new Size2D(paperSize.Width / 100d * PdfExporter.InchToPixel, paperSize.Height / 100d * PdfExporter.InchToPixel); double margin = 0.5d /* inch*/ * PdfExporter.InchToPixel; Matrix4D wcsToPdfTransform = DxfUtil.GetScaleTransform( (Point3D)clipRectangleWcs.Corner1, (Point3D)clipRectangleWcs.Corner2, (Point3D)clipRectangleWcs.Center, new Point3D(margin, margin, 0), new Point3D(paperSizePdf.X - margin, paperSizePdf.Y - margin, 0), new Point3D(0.5d * paperSizePdf.X, 0.5d * paperSizePdf.Y, 0) ); // The Pdf clip rectangle specified in hundredth of inches. Matrix4D wcsToPdfInchesTransform = Transformation4D.Scaling(100d / PdfExporter.InchToPixel) * wcsToPdfTransform; PdfPageConfiguration pdfPageConfiguration = new PdfPageConfiguration(model, GraphicsConfig.AcadLikeWithWhiteBackground, wcsToPdfTransform, paperSize) { ClipRectangle = new Rectangle2D( wcsToPdfInchesTransform.Transform(clipRectangleWcs.Corner1), wcsToPdfInchesTransform.Transform(clipRectangleWcs.Corner2) ) }; //pdfPageConfiguration.ClipRectangle = null; using (Stream stream = File.Create(dialog.FileName)) { PdfExporter pdfExporter = new PdfExporter(stream); pdfExporter.DrawPage(pdfPageConfiguration); pdfExporter.EndDocument(); } } }
- Wout
guain
9/23/2014 10:57 PM
:p Thanks. It works!
guain
9/24/2014 3:14 AM
How can I make PaperSize.A4 in landscape orientation in PDF ? Thanks in advance.
Wout
9/24/2014 3:46 PM
Try using
Visual Basic Code:
PaperKind.A4Rotated
. - Wout
1