2

这是我的问题。我有一个要导出为 PDF 的 RPT 文件。当我从数据集中提供整个表格并将 rpt 导出为 PDF 时,创建的文件大约为 25 MB。并且所有的陈述都被加入了。现在,当我将数据集中的单个记录提供给 rpt 文件并为每个语句创建新文件时,它们大约为 80 kb 到 250 kb,每个总计大约 650 MB。有没有办法可以减少我的第二个选项的文件大小,比如所有记录都为 150 MB?

任何解决方法?如果需要,我可以发布我的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Xml.Linq;
using System.Drawing;
using BitMiracle.Docotic.Pdf;



namespace PrintSinglaStatementAtTime
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            OracleConnection connection;
            OracleDataAdapter OracleAdapter;
            DataSet ds = new DataSet();

            int one=1;
            int two = 1;
            int three = 1;
            PdfSharp.Pdf.PdfDocument document;
            PdfSharp.Pdf.PdfPage page;
            XGraphics gfx;
            XFont font = new XFont("Verdana", 8);


            connection = new OracleConnection(ConfigurationManager.AppSettings["ConnectionString"]);

            connection.Open();
            OracleAdapter = new OracleDataAdapter(ConfigurationManager.AppSettings["StatementTable"], ConfigurationManager.AppSettings["ConnectionString"]);
            OracleAdapter.Fill(ds, "domestic");
            OracleAdapter = new OracleDataAdapter(ConfigurationManager.AppSettings["MetaDataTable"], ConfigurationManager.AppSettings["ConnectionString"]);
            OracleAdapter.Fill(ds, "MetaDataDetail");
            connection.Close();

            ReportDocument reportDoc = new ReportDocument();
            reportDoc.Load(@"c:\users\desktop\statement2.rpt");
            ExportOptions CrExportOptions;
            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
            PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();

            CrExportOptions = reportDoc.ExportOptions;
            {
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;

            }

            string XmlFilename = "";
            DataTable stmt = ds.Tables["MetaDataDetail"];
            stmt.PrimaryKey = new DataColumn[] { stmt.Columns["statementnumber"] };
            stmt.Columns["custname"].ColumnName = "CustomerName";
            stmt.Columns["custid"].ColumnName = "CustomerNumber";
            stmt.Columns["soldto_addr1"].ColumnName = "CustomerStreet1";
            stmt.Columns["soldto_addr2"].ColumnName = "CustomerStreet2";
            stmt.Columns["soldto_addr3"].ColumnName = "CustomerStreet3";
            stmt.Columns["soldto_city"].ColumnName = "CustomerCity";
            stmt.Columns["soldto_state"].ColumnName = "CustomerState";
            stmt.Columns["soldto_zip"].ColumnName = "CustomerZip";
            stmt.Columns["PackagePagecount"].ColumnName = "PackagePageCount";
            stmt.Columns["sequence"].ColumnName = "SequenceNumber";


            var stmtnumber = "";
            int pgcount1oz = 1;
                 int pgcount2oz = 1;
                 int pgcount3oz = 1;
                 PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();

                 ArrayList myArrayList = new ArrayList();
            for (int i = 0; i < stmt.Rows.Count; i++)
            {
                stmtnumber = stmt.Rows[i][0].ToString();

                 var  exp = GetFilteredTable(ds.Tables["domestic"], stmtnumber);


                 reportDoc.SetDataSource(exp);
                 CrDiskFileDestinationOptions.DiskFileName = @"d:\pdf1\" + stmtnumber + ".pdf";

                 reportDoc.Export();

               // var NumberofPages = GetNoOfPagesPDF(@"d:\pdf1\" + stmtnumber + ".pdf");         
                 document = PdfReader.Open(@"d:\pdf1\" + stmtnumber + ".pdf", PdfDocumentOpenMode.Modify);

                 int counter = document.PageCount;

                 if (counter < 4)
                 {

                     for (int idx = 0; idx < counter; idx++)
                     {
                         page = document.Pages[idx];
                          gfx = XGraphics.FromPdfPage(page);


                         if (idx == 0)
                         {

                             gfx.DrawString(one.ToString("00000") + "  " + pgcount1oz.ToString("00000"), font, XBrushes.Black,

                                 new XRect(230, 130, page.Width, page.Height),
                                 XStringFormats.TopLeft);
                             pgcount1oz++;

                         }
                         else if (idx>0
                             ) {
                             gfx.DrawString(pgcount1oz.ToString("00000"), font, XBrushes.Black,

                                     new XRect(236, 130, page.Width, page.Height),
                                     XStringFormats.TopLeft);
                             pgcount1oz++;
                         }
                     }
                     string filename = @"d:\pdf1\" + stmtnumber + ".pdf";
                     document.Save(filename);



                     DataRow tbe = stmt.Rows.Find(stmtnumber);
                     tbe["packagepagecount"] = counter;
                     tbe["SequenceNumber"] = one;
                     one++;
                 }
                 else if (counter > 4 || counter < 9)
                 {
                     for (int idx = 0; idx < counter; idx++)
                     {
                  page = document.Pages[idx];
                          gfx = XGraphics.FromPdfPage(page);

                         if (idx == 0)
                         {
                             gfx.DrawString(two.ToString("00000") + "  " +  pgcount2oz.ToString("00000"), font, XBrushes.Black,

                                 new XRect(230, 130, page.Width, page.Height),
                                 XStringFormats.TopLeft);
                             pgcount2oz++;
                         }
                         else
                         {
                             gfx.DrawString(                        "  " + pgcount2oz.ToString("00000"), font, XBrushes.Black,

                                     new XRect(236, 130, page.Width, page.Height),
                                     XStringFormats.TopLeft);
                             pgcount2oz++;
                         }
                     }
                     string filename = @"d:\pdf1\" + stmtnumber + ".pdf";
                     document.Save(filename);



                     DataRow tbe = stmt.Rows.Find(stmtnumber);
                     tbe["packagepagecount"] = counter;
                     tbe["SequenceNumber"] = two;
                     two++;

                 }
                 else if ( counter > 9)
                 {
                     for (int idx = 0; idx < counter; idx++)
                     {
                          page = document.Pages[idx];
                          gfx = XGraphics.FromPdfPage(page);


                         if (idx==0)
                         { gfx.DrawString(three.ToString("00000")+"  "+pgcount3oz.ToString("00000"), font, XBrushes.Black,

                             new XRect(230, 130, page.Width, page.Height),
                             XStringFormats.TopLeft);
                         pgcount3oz++;
                     }

                     else {

                          gfx.DrawString(                          "  "+pgcount3oz.ToString("00000"), font, XBrushes.Black,

                             new XRect(236, 130, page.Width, page.Height),
                             XStringFormats.TopLeft);
                          pgcount3oz++;
                     }
                     }
                     string filename = @"d:\pdf1\" + stmtnumber + ".pdf";
                     document.Save(filename);



                     DataRow tbe = stmt.Rows.Find(stmtnumber);
                     tbe["packagepagecount"] = counter;
                     tbe["SequenceNumber"] = three;
                     three++;

                 }

                // RecompressExistingImages(@"d:\pdf1\" + stmtnumber + ".pdf", @"d:\pdf\" + stmtnumber + ".pdf");
                 #region joining pdf .. needs work 
                // byte[] bytes = File.ReadAllBytes(@"d:\pdf1\" + stmtnumber + ".pdf");
                // File.WriteAllBytes(@"d:\pdf\" + stmtnumber + ".pdf",bytes);

                 //FileStream ReadPdf = new FileStream(@"d:\pdf1\" + stmtnumber + ".pdf", FileMode.Open);
                 //long FileSize;
                 //FileSize = ReadPdf.Length;
                 //byte[] Buffer = new byte[(int)FileSize];
                 //ReadPdf.Read(Buffer, 0, (int)ReadPdf.Length);
                 //ReadPdf.Close();
                 //FileStream CreatePdf = new FileStream(@"d:\pdf\" + stmtnumber + ".pdf",
                 //FileMode.Create);
                 //CreatePdf.Write(Buffer, 0, Buffer.Length);
                 //CreatePdf.Close();


                 #endregion 

                 var filtered = stmt.AsEnumerable().Where(a => a.Field<string>("statementnumber") == stmtnumber);
                     DataView z = filtered.AsDataView();
                DataTable ccc = z.ToTable();
                 XmlFilename = @"d:\XML\" + stmtnumber + ".xml";
                System.IO.FileStream streamWrite = new System.IO.FileStream (XmlFilename, System.IO.FileMode.Create);
             ccc.WriteXml(streamWrite);
             streamWrite.Close();
             ccc.Clear();
             XElement xEle = XElement.Load(@"d:\XML\" + stmtnumber + ".xml");
             var snumber = xEle.Elements("MetaDataDetail").Elements("STATEMENTNUMBER").ToList();
             foreach (XElement PLE in snumber)
                 PLE.Remove();
             xEle.Save(@"d:\XML\" + stmtnumber + ".xml");

            }
            sw.Stop();
            Console.WriteLine("Time :{0}", sw.Elapsed);
            Console.WriteLine("one Oz :{0}   \n two oz {1} \n Three Oz {2}", pgcount1oz, pgcount2oz, pgcount3oz);


            Console.Read();
        }
        public static DataTable GetFilteredTable(DataTable dt, object statementNumber)
        {
            var detailRows = dt.Select(String.Format("statementnumber = {0}", statementNumber));
            var filteredDt = dt.Clone();
            foreach (var detailRow in detailRows)
            {
                filteredDt.Rows.Add(detailRow.ItemArray);
            }
            return filteredDt;
        }
        public static int GetNoOfPagesPDF(string FileName)
        {
            int result = 0;

            FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            StreamReader r = new StreamReader(fs);

            string pdfText = r.ReadToEnd();

            System.Text.RegularExpressions.Regex regx = new Regex(@"/Type\s*/Page[^s]");

            System.Text.RegularExpressions.MatchCollection matches = regx.Matches(pdfText);

            result = matches.Count;

            return result;
        }
        public static int sort(string stmtnumber,int PageCount )
        {

            if (PageCount <= 4)
            {
                System.IO.File.Copy(@"d:\pdf1\" + stmtnumber + ".pdf", @"d:\pdf\1oZ\" + stmtnumber + ".pdf");
            }
            else if (PageCount > 4 || PageCount <= 9)
            {
                System.IO.File.Copy(@"d:\pdf1\" + stmtnumber + ".pdf", @"d:\pdf\2oZ\" + stmtnumber + ".pdf");
            }
            else if (PageCount >= 9)
            {
                System.IO.File.Copy(@"d:\pdf1\" + stmtnumber + ".pdf", @"d:\pdf\3oZ\" + stmtnumber + ".pdf");
            }

            return 0;

        }
        static void RecompressExistingImages(string fileName, string outputName)
        {
            using (BitMiracle.Docotic.Pdf.PdfDocument doc = new BitMiracle.Docotic.Pdf.PdfDocument(fileName))
            {
                foreach (PdfImage image in doc.Images)
                    image.RecompressWithJpeg();

                doc.Save(outputName);
            }
        }
        //private static void MergeMultiplePDFIntoSinglePDF(string outputFilePath, string[] pdfFiles)
        //{
        //    Console.WriteLine("Merging started.....");
        //    PdfDocument outputPDFDocument = new PdfDocument();
        //    foreach (string pdfFile in pdfFiles)
        //    {
        //        PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
        //        outputPDFDocument.Version = inputPDFDocument.Version;
        //        foreach (PdfPage page in inputPDFDocument.Pages)
        //        {
        //            outputPDFDocument.AddPage(page);
        //        }
        //    }
        //    outputPDFDocument.Save(outputFilePath);
        //    Console.WriteLine("Merging Completed");
        //}

        }

    }
4

3 回答 3

1

有一些命令行工具可以重新压缩 PDF 文件以使它们更小:

https://askubuntu.com/questions/113544/how-to-reduce-pdf-filesize

于 2013-05-16T19:32:03.830 回答
1

如果有人来寻找同样的问题,这里就是解决方案。

为每条记录导出的 PDF 文件具有包含字体、大小等信息的标题信息。

解决方案相当简单。我创建了数据块并运行它以导出为 PDF。意思是包含所有记录的单个文档。没有其他工作可以解决。

于 2013-06-03T16:33:31.497 回答
0
   ExportOptions CrExportOptions ;
                    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                    CrDiskFileDestinationOptions.DiskFileName = "c:\\csharp.net-informations.pdf";
                    CrExportOptions = doc.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    doc.Export();

尝试使用此代码转换为 pdf

于 2013-05-17T04:49:42.443 回答