2

ここに私の問題があります。PDFにエクスポートするRPTファイルがあります。データセットからテーブル全体を提供し、rpt を PDF にエクスポートすると、作成されるファイルは約 25 MB になります。すべてのステートメントが結合されます。データセットからrptファイルに個々のレコードを与え、ステートメントごとに新しいファイルを作成すると、それぞれ約80 kbから250 kbになり、合計で約650 MBになります。2 番目のオプションでファイルのサイズを小さくして、すべてのレコードを 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 に答える