异常了:2018/6/24 4:26:21spr2490: 引发类型为“System.OutOfMemoryException”的异常。
在 spr4203.0(Stream A_0)
在 Spire.Xls.Core.Spreadsheet.XlsWorkbook.SaveToPdf(Stream stream, ConverterSetting convertSetting)
在 Spire.Xls.Core.Spreadsheet.XlsWorkbook.SaveToPdf(String fileName, ConverterSetting convertSetting)
在 Spire.Xls.Core.Spreadsheet.XlsWorksheet.SaveToPdf(String fileName)
在 WorkLogRunnerEmail.WorkLogRunnerMail.ExcelSheetsToPdf() 位置 D:\Info\MES_Web\MESAutoTask\WorkLogRunnerMail.cs:行号 121
在 WorkLogRunnerEmail.WorkLogRunnerMail.checkTimer_Elapsed(Object sender, ElapsedEventArgs e) 位置 D:\Info\MES_Web\MESAutoTask\WorkLogRunnerMail.cs:行号 77;
关键代码如下:
if (File.Exists(filepath)) { Workbook workbook = new Workbook(); workbook.LoadFromFile(filepath); using (StreamWriter sw = new StreamWriter(logPath, true)) { sw.WriteLine("文件存在:" + DateTime.Now.ToString()+","+ workbook.Worksheets.Count); } for (int j = 0; j < workbook.Worksheets.Count; j++) { Worksheet sheet = workbook.Worksheets[j]; string fileName = sheet.Name; string OnlyCode = Guid.NewGuid().ToString(); string DocumentPath = savePath + OnlyCode + ".pdf"; string officePath = savePath + OnlyCode + "_1.pdf"; sheet.SaveToPdf(DocumentPath); //sheet.Dispose(); AddImgToPdf(DocumentPath, officePath);增加图片到PDF } workbook.Dispose(); }
protected void AddImgToPdf(string path, string toPath) { string imgUrl = @"C:\MES_Web\Images\official_seal.png"; if (File.Exists(toPath)) File.Delete(toPath); using (Stream inputPdfStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Stream inputImageStream = new FileStream(imgUrl, FileMode.Open, FileAccess.Read, FileShare.Read)) using (Stream outputPdfStream = new FileStream(toPath, FileMode.Create, FileAccess.Write, FileShare.None)) { var reader = new PdfReader(inputPdfStream); var stamper = new PdfStamper(reader, outputPdfStream); var pdfContentByte = stamper.GetOverContent(1); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream); image.SetAbsolutePosition(100, 100); pdfContentByte.AddImage(image); stamper.Close(); stamper.Dispose(); } File.Replace(toPath, path, path + ".back"); }
这个问题需要你的文档,烦请将你的源文档提供一下,加spire的技术支持QQ(2181348176)或者发送邮件都可以(support@e-iceblue.com)
PdfReader
、PdfStamper
、iTextSharp.text.Image
中,只要实现了 IDispose 接口,建议都放在 using
中