private static void ReadandWriter(string path, string printName, Dictionary <string, string> list) { try { StreamReader sr = new StreamReader(path); string content = "", line = ""; while (sr.Peek() != -1) { line = sr.ReadLine(); foreach (string key in list.Keys) { if (line.StartsWith(key)) { line = key + list[key]; } } content += line + "\r\n"; } sr.Close(); sr.Dispose(); StreamWriter sw = new StreamWriter(path, false); sw.Write(content); sw.Close(); sw.Dispose(); } catch (Exception exp) { PrinterLOG.WriterLog(exp.Message, "_PrintSetting_" + printName, false); } }
/// <summary> /// 获取所有相关文件 /// </summary> /// <param name="url"></param> /// <returns></returns> public string[] GetFilelist(string url) { try { url = HttpContext.Current.Server.MapPath(url); string path = url.Substring(0, url.LastIndexOf("\\")); string name = url.Substring(url.LastIndexOf("\\") + 1); string[] filelist = Directory.GetFiles(path, name.Insert(name.LastIndexOf("."), "*")); return(filelist); } catch (Exception exp) { PrinterLOG.WriterLog(exp.Message, "GetFilelist", true); return(new string[] { "" }); } }
/// <summary> /// 动态设置Iframe路径 /// </summary> /// <param name="url">url</param> public void Setframe(string url) { string parm = url.Substring(0, url.LastIndexOf("/") + 1); string PdfPath = ""; bool exist = true; int count = 0; while (exist) { if (File.Exists(HttpContext.Current.Server.MapPath(url))) { bool b = true; int i = 0; while (string.IsNullOrEmpty(PdfPath) || b) { MoveFile mf = new MoveFile("/Preview"); mf.DeleteFile(HttpContext.Current.Server.MapPath(url)); string path = mf.GetFilePath(HttpContext.Current.Server.MapPath(url)).Replace("\\", "/"); PdfPath = path; FileInfo info = new FileInfo(HttpContext.Current.Server.MapPath(PdfPath)); if (info.Exists && info.Length > 1000) { b = false; exist = false; } ++i; //Thread.Sleep(i * 100); } } else { if (count >= 100) { PrinterLOG.WriterLog(@"Cannot find the file:" + url, "Setframe", true); break; } ++count; exist = true; Thread.Sleep(500); } } HttpContext.Current.Response.Write(PdfPath + "?datetime=" + parm); }
/// <summary> /// 创建文件夹 /// </summary> /// <param name="filePath">文件路径</param> public void CreateDirectory(string filePath) { string _strpath = null; string[] st = filePath.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries); try { for (int l = 0; l < st.Length - 1; ++l) { _strpath += st[l] + "\\"; if (!Directory.Exists(_strpath)) { Directory.CreateDirectory(_strpath); } } } catch (Exception exp) { PrinterLOG.WriterLog(exp.Message, "CreateDirectory", true); } }
public static ReportDocument SetReportDoc(double top, double bottom, double left, double right, int printCount, params string[] nums) { ReportDocument doc = new ReportDocument(); try { string ReportPath = nums[0].ToString(); doc.Load(ReportPath); for (int i = 3; i < nums.Length; i++) { string paramValue = nums[i].ToString(); ParameterDiscreteValue pdv = new ParameterDiscreteValue(); pdv.Value = paramValue; ParameterValues pv = new ParameterValues(); pv.Add(pdv); doc.DataDefinition.ParameterFields[i - 3].ApplyCurrentValues(pv); } if (doc.Database.Tables.Count > 0) { //for (int i = 0; i < doc.Database.Tables.Count; i++) //{ // doc.Database.Tables[i].ApplyLogOnInfo(LoginInfo()); //} doc.Database.Tables[0].ApplyLogOnInfo(LoginInfo()); } string msgStr = "【" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "】 加载报表 \r\n"; PrinterLOG.WriterLog(msgStr, nums[2], false); //Sections sections = doc.ReportDefinition.Sections; //foreach (Section section in sections) //{ // ReportObjects reportObjects = section.ReportObjects; // foreach (ReportObject reportObject in reportObjects) // { // if (reportObject.Kind == ReportObjectKind.SubreportObject) // { // SubreportObject subreportObject = (SubreportObject)reportObject; // ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName); // if (subReportDocument.Database.Tables.Count > 0) // { // for (int i = 0; i < subReportDocument.Database.Tables.Count; i++) // { // subReportDocument.Database.Tables[i].ApplyLogOnInfo(LoginInfo()); // } // //subReportDocument.Database.Tables[0].ApplyLogOnInfo(LoginInfo()); // } // } // } //} //打印机名字 doc.PrintOptions.PrinterName = nums[2].ToString(); //设置打印边距 PageMargins margins; margins = doc.PrintOptions.PageMargins; margins.topMargin = Convert.ToInt32(top * 567); //567 margins.leftMargin = Convert.ToInt32(left * 567); margins.rightMargin = Convert.ToInt32(right * 567); margins.bottomMargin = Convert.ToInt32(bottom * 567); doc.PrintOptions.ApplyPageMargins(margins); if (nums[1].ToString().StartsWith("Y")) { //if (nums[1].ToString() == "Y1") //{ // doc.PrintOptions.PaperSize = PaperSize.PaperA4; //} //else //{ // doc.PrintOptions.PaperOrientation = PaperOrientation.DefaultPaperOrientation; // doc.PrintOptions.PaperSize = PaperSize.DefaultPaperSize; //} //打印份数,是否逐份打印,打印第一页,最后一页 if (printCount > 20) { printCount = 1; } doc.PrintToPrinter(printCount, false, 0, 0); } } catch (Exception exp) { PrinterLOG.WriterLog(exp.Message, nums[2].ToString(), true); doc = null; } return(doc); }
/// <summary> /// 虚拟打印 /// </summary> /// <param name="GetNums">报表相关参数</param> /// <param name="GetMargin">打印机边距数组</param> /// <param name="printCount">打印份数</param> /// <param name="url">ref 虚拟打印生成的文件路径</param> /// <param name="printName">ref 打印机名称</param> /// <param name="fielParam">生成文件名的参数数组</param> /// <returns>实现打印并返回ReportDocument</returns> public ReportDocument VirtualPrint(string[] GetNums, double[] GetMargin, int printCount, ref string url, string folder, ref string printName, string[] fileParam) { string msgStr = "【" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "】 准备参数 \r\n"; ReportDocument doc = new ReportDocument(); try { //string folder = Server.MapPath("PDF"); string PaperSize = ""; string fileName = GetfileName(ref PaperSize, fileParam); // 获取 纸张类型 ,文件名称 if (fileParam[0].ToLower() == "blsample" && Convert.ToInt32(GetNums[4]) > -1) { fileName = fileName.Insert(fileName.LastIndexOf("_"), "_" + GetNums[4]); } if (string.IsNullOrEmpty(fileName)) { return(null); } // 设置文件类型 string ff = HttpContext.Current.Request["format"]; int format = string.IsNullOrEmpty(ff) ? 4 : Convert.ToInt32(ff); GetPrintName(ref printName, PaperSize, PrintImgSetting.PIS.FileFormat(format)); // 获取可用的打印机名称 UpdatePrinterStatus(printName, 1, fileParam); // 更新打印机状态 //PrintImgSetting pis = new PrintImgSetting(); // 配置虚拟打印机 相关属性 //文件打印像素 int size = 240; // 设置文件是否连续显示 string cm = HttpContext.Current.Request["CreateMode"]; int mode = string.IsNullOrEmpty(cm) ? 0 : Convert.ToInt32(cm); string randomFolder = "/PreviewTemp/" + (string.IsNullOrEmpty(folder) ? DateTime.Now.ToString("hhmmssffff") : folder); MoveFile mf = new MoveFile(); mf.CreateDirectory(HttpContext.Current.Server.MapPath(randomFolder)); //设置文件目录 //PrintImgSetting.PIS.SetFileFolder(printName, HttpContext.Current.Server.MapPath(randomFolder)); //Thread.Sleep(300); //PrintImgSetting.PIS.SetFileName(printName, fileName); //设置文件名称 //Thread.Sleep(300); //PrintImgSetting.PIS.SetFileFormat(printName, format); //设置文件路径 //Thread.Sleep(300); //PrintImgSetting.PIS.SetCreateMode(printName, mode); //设置文件连续显示 //Thread.Sleep(300); PrintZanSetting.PrintSetting(printName, HttpContext.Current.Server.MapPath(randomFolder), fileName, null); msgStr += "【" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "】 开始打印,打印机:" + printName + " ; 用户:" + FSecurityHelper.CurrentUserDataGET()[0] + "," + FSecurityHelper.CurrentUserDataGET()[12] + " ; 报表类型:" + GetNums[0].Substring(GetNums[0].LastIndexOf("\\") + 1) + " ; SeedID :" + GetNums[3] + "\r\n"; doc = ToPrintPaper(GetNums, GetMargin, printCount, printName); //实现打印功能 msgStr += "【" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + "】 打印完成,开始执行预览 \r\n------------------------------------------------------\r\n"; //打印机生成的文件路径 url = randomFolder + "/" + fileName + "." + PrintImgSetting.PIS.FileFormat(format); //Thread.Sleep(1000); // 等待打印文件读写 UpdatePrinterStatus(printName, 0, fileParam); // 更新打印机状态 PrinterLOG.WriterLog(msgStr, printName, false); return(doc); //Setframe(url); } catch (Exception exp) { PrinterLOG.WriterLog(exp.Message, printName, true); UpdatePrinterStatus(printName, 0, fileParam); // 更新打印机状态 return(null); //HttpContext.Current.Response.Write(exp.Message); //HttpContext.Current.Response.Flush(); //HttpContext.Current.Response.Close(); } }