/// <summary> /// 打印Excel /// </summary> /// <param name="bs"></param> /// <param name="printName"></param> /// <param name="orientation">打印方向,默认纵向</param> public static void PrintExcel(byte[] bs, string printName, PageOrientationType orientation) { string printArea = "";//默认 MemoryStream ms = null; try { ms = new MemoryStream(bs); Workbook workbook = new Workbook(ms); Worksheet worksheet = workbook.Worksheets[0]; //打印设置 PageSetup pageSetup = worksheet.PageSetup; pageSetup.CenterHorizontally = true; //水平居中 pageSetup.CenterVertically = false; //不需要垂直居中 if (printArea != "") { //pageSetup.PrintArea = "A1:I22"; } pageSetup.Orientation = orientation; // pageSetup.Orientation = PageOrientationType.Landscape;//横向 // pageSetup.Orientation = PageOrientationType.Portrait;//纵向 ImageOrPrintOptions options = new ImageOrPrintOptions(); System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings(); defaultPrint = fPrintDocument.PrinterSettings.PrinterName; //defaultPrint = @"\\prtsvr\MP 3054 PCL 6 jszx"; //new Log4netHelper().AddLog("默认打印机名称是:"+defaultPrint); SheetRender sr = new SheetRender(worksheet, options); if (printName != "") { sr.ToPrinter(printName); } else { sr.ToPrinter(defaultPrint); } } catch { throw; } finally { GC.Collect(); //垃圾回收机制 if (ms != null) { ms.Close(); } } }
/// <summary> /// Method to Export the given plot window to PDF /// </summary> /// <param name="pw"></param> private void ExportToPDF(IGTPlotWindow pw) { IGTExportService svcExport = null; IGTPDFPrinterProperties printProps = null; try { // Construct printer properties PageOrientationType orientation = (pw.NamedPlot.PaperWidth > pw.NamedPlot.PaperHeight) ? PageOrientationType.Portrait : PageOrientationType.Landscape; printProps = GTClassFactory.Create <IGTPDFPrinterProperties>(); printProps.PageWidth = pw.NamedPlot.PaperWidth; printProps.PageHeight = pw.NamedPlot.PaperHeight; printProps.Orientation = orientation; printProps.PageSize = PageSizeValue.Auto; printProps.Resolution = ResolutionValue.DPI600; // Perform export svcExport = GTClassFactory.Create <IGTExportService>(); svcExport.PDFLayersEnabled = false; svcExport.SaveAsPDF(m_strDoumentsPath, printProps, pw, true); m_oGTTransactionManager.Begin("Attach Street Light Supplemental Agreement Plot"); IGTKeyObject gTTempKeyObject = m_gTDataContext.OpenFeature(m_gTDesignAreaKeyObject.FNO, m_gTDesignAreaKeyObject.FID); Recordset rs = gTTempKeyObject.Components.GetComponent(8130).Recordset; rs.AddNew("G3E_FID", gTTempKeyObject.FID); rs.Fields["HYPERLINK_T"].Value = m_strDoumentsPath; rs.Fields["DESCRIPTION_T"].Value = "Street Supplemental Plot"; rs.Fields["TYPE_C"].Value = "SUPPLEPLOT"; rs.Fields["G3E_FNO"].Value = 8100; rs.Update(); if (m_oGTTransactionManager.TransactionInProgress) { m_oGTTransactionManager.Commit(); } } catch { throw; } finally { svcExport = null; printProps = null; pw = null; } }
/// <summary> /// 打印操作 /// </summary> /// <param name="workbook"></param> /// <param name="printInfo"></param> /// <returns></returns> public void Print(string json_cellList, PrintInfoModel printInfo, PageOrientationType orientation) { IDictionary <string, object> logDict = new Dictionary <string, object>(); logDict.Add("json_cellList", json_cellList); logDict.Add("printInfo", printInfo); logDict.Add("orientation", orientation); ExceptionInfoEntity exception = WipLogHelper.GetExceptionInfo <IDictionary <string, object> >(namespaceName, "Print", logDict , "", printInfo == null ? "" : printInfo.processCardNumber, ExceptionSource.WIPPost, ExceptionLevel.BusinessError, wipSource); try { //创建文件 List <ExcelSheetModel> sheetModelList = new List <ExcelSheetModel>(); List <ExcelCellModel> cellModelList = JsonConvert.DeserializeObject <List <ExcelCellModel> >(json_cellList); ExcelSheetModel sheetModel = new ExcelSheetModel() { sheetName = this.workbook.GetSheetAt(0).SheetName, dataList = cellModelList, sheetType = "", }; sheetModelList.Add(sheetModel); byte[] bytes = ExcelUtil.writeExcelToFile(this.workbook, sheetModelList, printInfo.tempName); //修改为“打印中” var result = UpdatePrintStatusToPrinting(printInfo.id); if (!result) { throw new Exception("更新状态为打印中失败,printInfo.id:" + printInfo.id.ToString()); } PrintHelper.PrintExcel(bytes, printInfo.printerName, orientation); //打印成功 this.DoByPrintResultSuccess(printInfo); } catch (Exception ex) { WipLogHelper.GetExceptionInfoForError(ex, ref exception); WipLogHelper.WriteExceptionInfo(exception); //打印失败 this.DoByPrintResultFailure(printInfo, ex); } }
public PageOrientationType orientation = PageOrientationType.Portrait;//打印方向,默认纵向 public PrintMgr GetPrintMgr(PrintType printType) { PrintMgr printMgr = null; switch (printType) { case PrintType.ProcessCardForPostHeatStorageOut: case PrintType.ProcessCardForBuffer: case PrintType.ProcessCardForWCSByWhGate1: case PrintType.ProcessCardForWCSByWhGate3: printMgr = new ProcessCardPrint(printType); break; case PrintType.InspectOrder: //人工抽检位送检单 case PrintType.InspectOrderForReject: //不良品打印送检单 printMgr = new InspectOrderPrint(printType); orientation = PageOrientationType.Landscape; //横向打印 break; } return(printMgr); }
/// <summary> /// Method to Export the given plot window to PDF /// </summary> /// <param name="p_destDir">Directory path of the target PDF filename.</param> /// <param name="p_destName">PDF filename</param> /// <param name="p_append">Append the results to destFileName, if destFileName already exists; otherwise create/overwrite FileName</param> /// <param name="pw">Plot window which needs to be exported to PDF</param> private void ExportToPDF(string p_destDir, string p_destName, bool p_append, IGTPlotWindow pw) { IGTExportService svcExport = null; IGTPDFPrinterProperties printProps = null; try { // Construct full path string destPathName = Path.Combine(p_destDir, p_destName); // Construct printer properties PageOrientationType orientation = (pw.NamedPlot.PaperWidth > pw.NamedPlot.PaperHeight) ? PageOrientationType.Portrait : PageOrientationType.Landscape; printProps = GTClassFactory.Create <IGTPDFPrinterProperties>(); printProps.PageWidth = pw.NamedPlot.PaperWidth; printProps.PageHeight = pw.NamedPlot.PaperHeight; printProps.Orientation = orientation; printProps.PageSize = PageSizeValue.Auto; printProps.Resolution = ResolutionValue.DPI600; // Perform export svcExport = GTClassFactory.Create <IGTExportService>(); svcExport.PDFLayersEnabled = false; svcExport.SaveAsPDF(destPathName, printProps, pw, p_append); } catch { throw; } finally { svcExport = null; printProps = null; pw = null; } }
/// <summary> /// 輸入 DataTable 轉成 有浮水印 的 PDF /// </summary> /// <param name="dt"></param> /// <param name="columnNameMappings"></param> /// <param name="folderName"></param> /// <param name="watermark"></param> /// <param name="pot"></param> /// <returns>產生的 pdf 檔名 (fullpath) </returns> private static string GenPDF(DataTable dt, Dictionary <string, string> columnNameMappings , string folderName, string watermark, PageOrientationType pot) { ChangeColumnDisplayName(dt, columnNameMappings); //output file name var fileNameWithoutExt = $"{Guid.NewGuid().ToString("N")}"; var outputExcel = Path.Combine(folderName, $"{fileNameWithoutExt}_tmp.xlsx"); var tempPdf = Path.Combine(folderName, $"{fileNameWithoutExt}_tmp.pdf"); var outputPdf = Path.Combine(folderName, $"{fileNameWithoutExt}.pdf"); //proc excel // Instantiating a Workbook object var workbook = new Workbook(); var worksheet = workbook.Worksheets[0]; worksheet.Cells.ImportDataTable(dt, true, "A1"); worksheet.AutoFitColumns(); worksheet.AutoFitRows(); var range = worksheet.Cells.MaxDisplayRange; var pageSetup = workbook.Worksheets[0].PageSetup; //var titleEndColumnName = CellsHelper.ColumnIndexToName(range.ColumnCount-1); //pageSetup.PrintTitleColumns = $"$A:${titleEndColumnName}"; pageSetup.PrintTitleRows = "$1:$1"; pageSetup.IsPercentScale = true; pageSetup.Orientation = pot; //border //Setting border for each cell in the range var style = workbook.CreateStyle(); var colorBlack = System.Drawing.Color.Black; style.SetBorder(BorderType.BottomBorder, CellBorderType.Medium, colorBlack); style.SetBorder(BorderType.LeftBorder, CellBorderType.Medium, colorBlack); style.SetBorder(BorderType.RightBorder, CellBorderType.Medium, colorBlack); style.SetBorder(BorderType.TopBorder, CellBorderType.Medium, colorBlack); range.SetStyle(style); // Saving the Excel file //workbook.Save(outputExcel); //workbook.Save(tempPdf); //save to stream var pdfStream = new MemoryStream(); workbook.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf); var pdfDocument = new Aspose.Pdf.Document(pdfStream); if (!string.IsNullOrWhiteSpace(watermark)) { //針對 PDF 加入 Watermark Aspose.Pdf.Facades.Stamp aStamp = new Aspose.Pdf.Facades.Stamp(); aStamp.Rotation = 45; var textStamp = new TextStamp(watermark); //set properties of the stamp // textStamp.Background = true; textStamp.Opacity = 0.2; textStamp.TextState.FontSize = 60.0F; textStamp.HorizontalAlignment = HorizontalAlignment.Center; textStamp.VerticalAlignment = VerticalAlignment.Center; textStamp.RotateAngle = aStamp.Rotation; textStamp.TextState.Font = FontRepository.FindFont("Arial"); textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Gray; foreach (var page in pdfDocument.Pages) { page.AddStamp(textStamp); } } pdfDocument.Save(outputPdf); return(outputPdf); }
/// <summary> /// Convert Excel PageOrientaton to Word PageOrientaton /// </summary> /// <param name="excelPageOrientation">Excel PageOrientation (Portrait or Landscape)</param> /// <returns>Portrait or Landscape, by default returns Portrait</returns> private Orientation ConvertPageOrientation(PageOrientationType excelPageOrientation) { Orientation pageOrientation = Orientation.Portrait; switch (excelPageOrientation) { case PageOrientationType.Portrait: { pageOrientation = Orientation.Portrait; break; } case PageOrientationType.Landscape: { pageOrientation = Orientation.Landscape; break; } default: { pageOrientation = Orientation.Portrait; break; } } return pageOrientation; }
public static PageSetup SetOrientation(this PageSetup pageSetup, PageOrientationType value) { pageSetup.Orientation = value; return(pageSetup); }