浏览代码

Reports saved to PDF are now compressed

frogsoftware 1 年之前
父节点
当前提交
d41a2181f8
共有 1 个文件被更改,包括 29 次插入25 次删除
  1. 29 25
      inabox.wpf/Reports/ReportUtils.cs

+ 29 - 25
inabox.wpf/Reports/ReportUtils.cs

@@ -20,6 +20,8 @@ using InABox.Core;
 using InABox.Core.Reports;
 using InABox.Scripting;
 using InABox.Wpf.Reports.CustomObjects;
+using Syncfusion.Pdf;
+using Syncfusion.Pdf.Parsing;
 using XmlDocument = System.Xml.XmlDocument;
 
 namespace InABox.Wpf.Reports
@@ -251,41 +253,43 @@ namespace InABox.Wpf.Reports
                 
         }
 
+        public static byte[] CompressPDF(byte[] original)
+        {
+
+                //Load the existing PDF document
+                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(original);
+
+                //Create a new compression option.
+                PdfCompressionOptions options = new PdfCompressionOptions();
+                //Enable the compress image.
+                options.CompressImages = true;
+                //Set the image quality.
+                options.ImageQuality = 50;
+                
+                //Assign the compression option to the document.
+                loadedDocument.CompressionOptions = options;
+                
+                //Creating the stream object.
+                using (MemoryStream stream = new MemoryStream())
+                {
+                    //Save the document into stream.
+                    loadedDocument.Save(stream);
+                    return stream.GetBuffer();
+                }
+            
+        }
+
         public static byte[] ReportToPDF(ReportTemplate template, DataModel data, bool repopulate = true)
         {
             byte[] result = null;
             if (template.IsRDL)
                 MessageBox.Show("RDL Reports are not supported!");
-            // using (var pdf = new MemoryStream())
-            // {
-            //     using (var ms = ReportUtils.SetupReportToStream(template.RDL, data.AsDictionary))
-            //     {
-            //         using (ReportWriter reportWriter = new ReportWriter(ms))
-            //         {
-            //             reportWriter.ReportProcessingMode = Syncfusion.ReportWriter.ProcessingMode.Local;
-            //             reportWriter.Save(pdf, WriterFormat.PDF);
-            //
-            //             //String filename = Path.Combine(GetPath(), "report.pdf");
-            //             //reportWriter.Save(filename, WriterFormat.PDF);
-            //             //PdfLoadedDocument rpt = new PdfLoadedDocument(filename);
-            //
-            //             PdfLoadedDocument rpt = new PdfLoadedDocument(pdf);
-            //             PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A1B);
-            //             PdfDocument.Merge(doc, rpt);
-            //             var msFinal = new MemoryStream();
-            //             doc.Save(msFinal);
-            //             result = msFinal.GetBuffer();
-            //         }
-            //     }
-            //     //result = GhostScriptIt(pdf.GetBuffer());
-            //     //result = pdf.GetBuffer();
-            // }
             else
                 using (var report = SetupReport(template, data, repopulate))
                 {
                     report.Prepare();
                     var ms = new MemoryStream();
-                    report.Export(new PDFExport(), ms);
+                    report.Export(new PDFExport() { JpegCompression = true, JpegQuality = 65, Compressed = true }, ms);
                     result = ms.GetBuffer();
                 }