private void SetCurrentPaper(PageMediaSize pageMediaSize) { var widthInInch = Math.Round(pageMediaSize.Width.Value / 96 * 100); var heightInInch = Math.Round(pageMediaSize.Height.Value / 96 * 100); var paperSize = PaperSizes.FirstOrDefault(p => p.Width == widthInInch && p.Height == heightInInch); if (paperSize != null) CurrentPaper = PaperSizes[PaperSizes.IndexOf(paperSize)]; }
public DimensionBuilder SetPaperSize(PaperSizes sizes) { var selected = sizes.ToSelectedSize(); this.Request.Dimensions.PaperWidth = selected.Width; this.Request.Dimensions.PaperHeight = selected.Height; return(this); }
private void InitPaperSize() { var ps = Model.PrinterSettings; // TODO: improve conversion from PaperFormat to PaperSize var paperSizes = PaperSizes.GetPaperSizes(ps); var paperSize = paperSizes.FirstOrDefault(p => p.PaperName == PaperFormat.A4.ToString()); ps.DefaultPageSettings.PaperSize = paperSize; }
public static (double Width, double Height) ToSelectedSize(this PaperSizes selectedSize) { if (!Enum.IsDefined(typeof(PaperSizes), selectedSize)) { throw new InvalidEnumArgumentException(nameof(selectedSize), (int)selectedSize, typeof(PaperSizes)); } if (selectedSize == PaperSizes.None) { throw new InvalidOperationException(nameof(selectedSize)); } return(PaperSizer.First(s => s.Size == selectedSize).Value); }
/// <summary> /// Updates the instance of printer settings according to properties of the XmlPaper. /// </summary> public void UpdatePageSettings(PrinterSettings settings) { var size = PaperSizes.PaperSizeByFormatName(PaperName, settings); if (size == null) { // let's create a custom paper size size = new PaperSize { Width = Width, Height = Height, PaperName = PaperName }; } var page = settings.DefaultPageSettings; page.Landscape = Landscape; page.PaperSize = size; page.Margins = Margins; }
private SizeF GetUsablePaperSize() { if (string.IsNullOrWhiteSpace(View.PaperFormat)) { return(default(SizeF)); } var paperSize = PaperSizes.PaperSizeByFormatName(View.PaperFormat, Model.PrinterSettings); if (paperSize != null) { var margins = AppConfig.Instance.PrintingMargins; float width = paperSize.Width - margins.Left - margins.Right; float height = paperSize.Height - margins.Top - margins.Bottom; bool swap = View.Orientation != Orientation.Vertical; return(new SizeF(swap ? height : width, swap ? width : height)); } Logger.Current.Warn("Failed to find specified paper format: " + View.PaperFormat); return(default(SizeF)); }
public void getDXFFormat(DxfModel model, string filename, string outfile) { try { string extension = System.IO.Path.GetExtension(filename); if (string.Compare(extension, ".dwg", true) == 0) { model = DwgReader.Read(filename); } else { model = DxfReader.Read(filename); } } catch (Exception e) { //Console.Error.WriteLine("Error occurred: " + e.Message); //Environment.Exit(1); } GDIGraphics3D graphics = new GDIGraphics3D(GraphicsConfig.BlackBackgroundCorrectForBackColor); Size maxSize = new Size(500, 500); Bitmap bitmap = ImageExporter.CreateAutoSizedBitmap( model, graphics, Matrix4D.Identity, System.Drawing.Color.Black, maxSize ); //string outfile = Path.GetFileNameWithoutExtension(Path.GetFullPath(filename)); Stream stream = null; //string outfile = AppDomain.CurrentDomain.BaseDirectory + "Drill\\rockHistogram\\"+filename; BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model); Bounds3D bounds = boundsCalculator.Bounds; PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4);//设置为A4纸的大小 // Lengths in inches. float pageWidth = (float)paperSize.Width / 100f; float pageHeight = (float)paperSize.Height / 100f; float margin = 0.2f; //值越小 model相对于pdf图幅越大 // Scale and transform such that its fits max width/height // and the top left middle of the cad drawing will match the // top middle of the pdf page. // The transform transforms to pdf pixels. Matrix4D 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(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel) ); using (stream = File.Create(outfile + ".pdf")) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.DrawPage( model, GraphicsConfig.WhiteBackgroundCorrectForBackColor, to2DTransform, paperSize ); pdfGraphics.EndDocument(); } /* * 可选的图片格式 * stream = File.Create(outfile + ".png"); * ImageExporter.EncodeImageToPng(bitmap, stream); * * stream = File.Create(outfile + ".tiff"); * ImageExporter.EncodeImageToTiff(bitmap, stream); * * stream = File.Create(outfile + ".jpg"); * ImageExporter.EncodeImageToJpeg(bitmap, stream); * * stream = File.Create(outfile + ".gif"); * ImageExporter.EncodeImageToGif(bitmap, stream); * * stream = File.Create(outfile + ".bmp"); * ImageExporter.EncodeImageToBmp(bitmap, stream); */ }
public PsExporter(Stream stream, PaperKind paperKind) : this(stream, PaperSizes.GetPaperSize(paperKind)) { }
public static void Main(string[] args) { if (args.Length != 2) { args = new string[2]; //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test1.dwg"; //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test2.dwg"; //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test3.dwg"; //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test4.dwg"; args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test5Insert.dwg"; //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test6.dwg"; } string format = args[0]; string filename = args[1]; DxfModel model = null; try { string extension = Path.GetExtension(filename); if (string.Compare(extension, ".dwg", true) == 0) { model = DwgReader.Read(filename); } else { model = DxfReader.Read(filename); } } catch (Exception e) { Console.Error.WriteLine("Error occurred: " + e.Message); Environment.Exit(1); } //foreach (var entityGroups in model.Entities.GroupBy(a => a.GetType())) //{ // Console.WriteLine(entityGroups.GetType()); // //if (typeof(DxfLine) == entityGroups.Key) // //{ // // foreach (var item in entityGroups) // // { // // Console.WriteLine(item.Color); // // } // //} //} //FileStream fs = new FileStream("D:\\C项目\\CadLCmd\\CadLCmd\\txt\\test1.txt", FileMode.Create); FindEntities(model.Entities); string json = JsonConvert.SerializeObject(cadEntities); File.WriteAllText("D:\\C项目\\CadLCmd\\CadLCmd\\txt\\model.json", json); File.WriteAllText("D:\\Project\\cadtest\\node_modules\\@cadTestUbim\\res\\data\\dxfdata.json", json); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); KTCodes = ktls.ToArray(); KTest = ktest.ToArray(); DxfType = dxfType.ToArray(); KInsert = kins.ToArray(); KEC = kco.ToArray(); KCIRCLE = kcircle.ToArray(); //Console.ReadKey(); string outfile = Path.GetDirectoryName(Path.GetFullPath(filename)) + "\\12"; Stream stream; if (format == "pdf") { BoundsCalculator boundsCalculator = new BoundsCalculator(); boundsCalculator.GetBounds(model); Bounds3D bounds = boundsCalculator.Bounds; PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.Letter); // Lengths in inches. float pageWidth = (float)paperSize.Width / 100f; float pageHeight = (float)paperSize.Height / 100f; float margin = 0.5f; // Scale and transform such that its fits max width/height // and the top left middle of the cad drawing will match the // top middle of the pdf page. // The transform transforms to pdf pixels. Matrix4D 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(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel), new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel) ); using (stream = File.Create(outfile + ".pdf")) { PdfExporter pdfGraphics = new PdfExporter(stream); pdfGraphics.DrawPage( model, GraphicsConfig.WhiteBackgroundCorrectForBackColor, to2DTransform, paperSize ); pdfGraphics.EndDocument(); } } else { GDIGraphics3D graphics = new GDIGraphics3D(GraphicsConfig.BlackBackgroundCorrectForBackColor); Size maxSize = new Size(500, 500); Bitmap bitmap = ImageExporter.CreateAutoSizedBitmap( model, graphics, Matrix4D.Identity, System.Drawing.Color.Black, maxSize ); switch (format) { case "bmp": using (stream = File.Create(outfile + ".bmp")) { ImageExporter.EncodeImageToBmp(bitmap, stream); } break; case "gif": using (stream = File.Create(outfile + ".gif")) { ImageExporter.EncodeImageToGif(bitmap, stream); } break; case "tiff": using (stream = File.Create(outfile + ".tiff")) { ImageExporter.EncodeImageToTiff(bitmap, stream); } break; case "png": using (stream = File.Create(outfile + ".png")) { ImageExporter.EncodeImageToPng(bitmap, stream); } break; case "jpg": using (stream = File.Create(outfile + ".jpg")) { ImageExporter.EncodeImageToJpeg(bitmap, stream); } break; default: Console.WriteLine("Unknown format " + format + "."); break; } } }
// For each layout, add a page to the PDF file. // Optionally specify a modelView (for model space only). // Optionally specify a layout. 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); } } }