public ComResult Print(PrintModel model) { var xpsStream = this.MergeSvc.Merge(model); if (model.Action == PrintActionType.File || model.Action == PrintActionType.PrintAndFile) { var dirPath = $"{this.HostEnv.ContentRootPath}\\wwwroot\\download\\{DateTime.Now.ToString("yyyyMMdd")}"; if (!System.IO.Directory.Exists(dirPath)) { System.IO.Directory.CreateDirectory(dirPath); } var fileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xps"; var filePath = $"{dirPath}\\{fileName}"; using (var fileStream = new FileStream(filePath, FileMode.Create)) { xpsStream.WriteTo(fileStream); fileStream.Close(); } if (model.Action == PrintActionType.PrintAndFile) { for (int i = 0; i < model.Copies; i++) { XpsPrintHelper.Print(xpsStream, model.PrintName, Guid.NewGuid().ToString("N"), model.IsWait); } } var url = filePath.Replace($"{this.HostEnv.ContentRootPath}\\wwwroot", "").Replace("\\", "/"); return(new ComResult() { Code = "0", Msg = "操作成功", Data = url, Success = true }); } else { for (int i = 0; i < model.Copies; i++) { XpsPrintHelper.Print(xpsStream, model.PrintName, Guid.NewGuid().ToString("N"), model.IsWait); } return(new ComResult() { Code = "0", Msg = "操作成功", Data = model.ToJson(), Success = true }); } }
/* * private void Canvas_Drop(object sender, DragEventArgs e) * { * UIElement element = null; * if (e.Data.GetData(typeof(BarCode)).GetType()== typeof(BarCode)) * { * element = e.Data.GetData(typeof(BarCode)) as BarCode; * } * else if (e.Data.GetData(typeof(TextBlock)).GetType() == typeof(TextBlock)) * { * element = e.Data.GetData(typeof(TextBlock)) as TextBlock; * } * else if (e.Data.GetData(typeof(Line)).GetType() == typeof(Line)) * { * element = e.Data.GetData(typeof(Line)) as Line; * } * else if (e.Data.GetData(typeof(Rectangle)).GetType() == typeof(Rectangle)) * { * element = e.Data.GetData(typeof(Rectangle)) as Rectangle; * } * * if (element == null) return; * if (mainCanvas.Children.Count >= 0) * { * Console.WriteLine(element); * mainCanvas.Children.Add(element); * Canvas.SetTop(element, e.GetPosition(this.mainCanvas).Y); * Canvas.SetLeft(element, e.GetPosition(this.mainCanvas).X); * } * dragStartPoint = null; * selectedBrush = null; * RemoveDraggedAdorner(); * e.Handled = true; * } * * public static bool IsMovementBigEnough(Point initialMousePosition, Point currentPosition) * { * return (Math.Abs(currentPosition.X - initialMousePosition.X) >= SystemParameters.MinimumHorizontalDragDistance || * Math.Abs(currentPosition.Y - initialMousePosition.Y) >= SystemParameters.MinimumVerticalDragDistance); * } * * private void ToolBar_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) * { * dragStartPoint = new Point?(Mouse.GetPosition(this)); * } * * private int dragCount = 0;//ToolBar_PreviewMouseMove会被触发两次,剔除其中一次 * private void ToolBar_PreviewMouseMove(object sender, MouseEventArgs e) * { * if (dragStartPoint.HasValue && e.LeftButton == MouseButtonState.Pressed) * { * if (IsMovementBigEnough(dragStartPoint.Value, e.GetPosition(this))) * { * Button btn = selectedBrush as Button; * if (btn == null) return; * this.initialMouseOffset = this.dragStartPoint.Value - btn.TranslatePoint(new Point(0, 0), this); * * DataObject dataObject = new DataObject(); * if (btn.Name == "btnBarcode") * { * BarCode barCode = new BarCode("1234567"); * dataObject.SetData(barCode); * } * else if (btn.Name == "btnText") * { * TextBlock tb = new TextBlock(); * tb.AllowDrop = true; * tb.Text = "12345678"; * dataObject.SetData(tb); * } * else if (btn.Name == "btnLine") * { * Line line = new Line(); * line.Height = 1; * line.Width = 100; * line.X1 = 10; * line.X2 = 110; * line.Y1 = 20; * line.Y2 = 10; * line.Fill = Brushes.Black; * dataObject.SetData(line); * } * else if (btn.Name == "btnRect") * { * Rectangle rectangle = new Rectangle(); * rectangle.Height = 50; * rectangle.Width = 100; * rectangle.Fill = Brushes.Transparent; * dataObject.SetData(rectangle); * } * else if (btn.Name == "btnImg") * { * * } * try * { * dragCount++; * if (dragCount == 2) * { * DragDrop.DoDragDrop(this.toolBar, dataObject, DragDropEffects.Copy); * dragCount = 0; * } * } * catch { } * } * } * else * { * selectedBrush = null; * } * e.Handled = true; * } * * private void mainCanvas_PreviewDragEnter(object sender, DragEventArgs e) * { * BarCode bc = e.Data.GetData(typeof(BarCode)) as BarCode; * if (bc != null) * { * //ShowDraggedAdorner(e.GetPosition(this)); * } * e.Handled = true; * } * * private void mainCanvas_PreviewDragOver(object sender, DragEventArgs e) * { * BarCode bc = e.Data.GetData(typeof(BarCode)) as BarCode; * if (bc != null) * { * //ShowDraggedAdorner(e.GetPosition(this)); * } * e.Handled = true; * } * * * * private void mainCanvas_PreviewDragLeave(object sender, DragEventArgs e) * { * RemoveDraggedAdorner(); * e.Handled = true; * } * */ private void btnPrint_Click(object sender, RoutedEventArgs e) { this.Topmost = false; this.mainCanvas.Background = Brushes.White; // Display Printer dialog for user to // choose the printer to output to. XpsPrintHelper printHelper = new XpsPrintHelper(this.mainCanvas); PrintDialog printDialog = printHelper.GetPrintDialog(); if (printDialog == null) { return; //user selected cancel } PrintQueue printQueue = printDialog.PrintQueue; printHelper.OnAsyncPrintChange += new XpsPrintHelper.AsyncPrintChangeHandler(AsyncPrintEvent); printHelper.PrintVisualAsync(printQueue); this.Topmost = true; }
public ActionResult <TestModel> Get() { var testXps = $"{this.HostEnv.ContentRootPath}\\wwwroot\\Test.xps"; if (System.IO.File.Exists(testXps)) { var printerName = "KONICA MINOLTA bizhub C226 PCL (10.76.20.30) UPD"; XpsPrintHelper.Print(testXps, printerName, DateTime.Now.ToString(), false); return(new TestModel() { Id = 1, Name = printerName, CreateTime = DateTime.Now }); } else { return(NotFound()); } }
// ------------------------ ButtonHelperPrint ------------------------- /// <summary> /// Saves the current DocumentViewer content either /// synchronously (waiting for completion) or /// asynchronously (not waiting for completion).</summary> /// <param name="async"> /// true to save asynchronously; false to save synchronously.</param> private void ButtonHelperPrint(bool async) { // Display Printer dialog for user to // choose the printer to output to. XpsPrintHelper printHelper = new XpsPrintHelper(_contentDir); PrintDialog printDialog = printHelper.GetPrintDialog(); if (printDialog == null) { return; //user selected cancel } PrintQueue printQueue = printDialog.PrintQueue; if (async) { _printHelper = printHelper; //Make progress controls visible AsyncPrintLabel.Visibility = Visibility.Visible; AsyncPrintProgress.Visibility = Visibility.Visible; AsyncPrintStatus.Visibility = Visibility.Visible; //register for async archiving events _printHelper.OnAsyncPrintChange += new XpsPrintHelper.AsyncPrintChangeHandler(AsyncPrintEvent); AsyncPrintProgress.Value = 10; AsyncPrintStatus.Text = "Async Print Initiated"; UIEnabled(false, false, true); }// end:if (async) // Print the DocumentViewer's current content. switch (currentMode) { case eGuiMode.SingleVisual: { printHelper.PrintSingleVisual(printQueue, async); break; } case eGuiMode.MultipleVisuals: { printHelper.PrintMultipleVisuals(printQueue, async); break; } case eGuiMode.SingleFlowDocument: { printHelper.PrintSingleFlowContentDocument( printQueue, async); break; } case eGuiMode.SingleFixedDocument: { printHelper.PrintSingleFixedContentDocument( printQueue, async); break; } case eGuiMode.MultipleFixedDocuments: { printHelper.PrintMultipleFixedContentDocuments( printQueue, async); break; } } // end:switch (currentMode) } // end:ButtonHelperSave()
private void RequextAction(HttpListenerContext context) { var request = context.Request; var response = context.Response; try { this.Log.Info(request.Url); var model = this.GetPrintModel(request); var dicJson = model.ToJson(); this.Log.Info(dicJson); var printDoc = new MergeDocument(model); var xpsStream = printDoc.MergeToStream(); if (model.Action == PrintActionType.Print) { for (int i = 0; i < model.Copies; i++) { xpsStream.Seek(0, SeekOrigin.Begin); XpsPrintHelper.Print(xpsStream, model.PrintName, $"XPS_{i}_{DateTime.Now.ToString("yyMMddHHmmssfff")}", false); } var resBytes = Encoding.UTF8.GetBytes(dicJson); response.ContentType = "application/json"; response.StatusCode = 200; response.ContentLength64 = resBytes.Length; response.ContentEncoding = Encoding.UTF8; response.OutputStream.Write(resBytes, 0, resBytes.Length); } if (model.Action == PrintActionType.File) { response.ContentType = "application/vnd.ms-xpsdocument"; response.AddHeader("Content-Disposition", $"attachment; filename=XPS_{DateTime.Now.ToString("yyMMddHHmmssfff")}.xps"); response.StatusCode = 200; response.ContentLength64 = xpsStream.Length; //response.ContentEncoding = Encoding.UTF8; xpsStream.Seek(0, SeekOrigin.Begin); xpsStream.CopyTo(response.OutputStream); } if (model.Action == PrintActionType.PrintAndFile) { for (int i = 0; i < model.Copies; i++) { xpsStream.Seek(0, SeekOrigin.Begin); XpsPrintHelper.Print(xpsStream, model.PrintName, $"XPS_{i}_{DateTime.Now.ToString("yyMMddHHmmssfff")}", false); } response.ContentType = "application/vnd.ms-xpsdocument"; response.AddHeader("Content-Disposition", $"attachment; filename=XPS_{DateTime.Now.ToString("yyMMddHHmmssfff")}.xps"); response.StatusCode = 200; response.ContentLength64 = xpsStream.Length; //response.ContentEncoding = Encoding.UTF8; xpsStream.Seek(0, SeekOrigin.Begin); xpsStream.CopyTo(response.OutputStream); } xpsStream.Dispose(); printDoc.Dispose(); } catch (Exception ex) { this.Log.Error(ex.Message, ex); var json = ex.ToJson(); var bytes = Encoding.UTF8.GetBytes(json); response.ContentType = "application/json"; response.StatusCode = 500; response.ContentLength64 = bytes.Length; response.ContentEncoding = Encoding.UTF8; response.OutputStream.Write(bytes, 0, bytes.Length); } response.OutputStream.Flush(); response.OutputStream.Close(); }