0

c# でスライスされた PDF ドキュメントを圧縮する方法..??

私はpdf文書を持っています。私はその文書をスライスしています。元のpdfドキュメントのサイズが10 mbの場合、スライスサイズは15 mbに増加しています。そのため、スライスしたドキュメントを圧縮する必要があります。圧縮する方法はありますか..?? 私を助けてください..

public int ExtractPages(string sourcePdfPath, string DestinationFolder)
        {
            int p = 0, initialcount = 0;
            try
            {
                iTextSharp.text.Document document;
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdfPath), new ASCIIEncoding().GetBytes(""));

            if (!Directory.Exists(DestinationFolder))
            {
                Directory.CreateDirectory(DestinationFolder);
            }
            else
            {
                DirectoryInfo di = new DirectoryInfo(DestinationFolder);
                initialcount = di.GetFiles("*.pdf", SearchOption.AllDirectories).Length;
            }

            for (p = 1; p <= reader.NumberOfPages; p++)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    document = new iTextSharp.text.Document();
                    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream);
                    writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_2);
                    writer.CompressionLevel = iTextSharp.text.pdf.PdfStream.BEST_COMPRESSION;
                    writer.SetFullCompression();
                    document.SetPageSize(reader.GetPageSize(p));
                    document.NewPage();
                    document.Open();
                    document.AddDocListener(writer);
                    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
                    iTextSharp.text.pdf.PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
                    int rot = reader.GetPageRotation(p);
                    if (rot == 90 || rot == 270)
                    {
                        cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                    }
                    else
                    {
                        cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
                    }
                    document.Close();
                    document.Dispose();
                    File.WriteAllBytes(DestinationFolder + "/" + p + ".pdf", memoryStream.ToArray());
                }
            }
            reader.Close();
            reader.Dispose();
        }
        catch
        {
        }
        finally
        {
            GC.Collect();
        }



        if (initialcount > (p - 1))
        {
            for (int k = (p - 1) + 1; k <= initialcount; k++)
            {
                try
                {
                    File.Delete(DestinationFolder + "/" + k + ".pdf");
                }
                catch
                {
                }
            }
        }

        return p - 1;
    }
4

1 に答える 1