使用iTextSharp时,拆分的PDF大小更大

使用iTextSharp时,拆分的PDF大小更大,pdf,split,itextsharp,using,Pdf,Split,Itextsharp,Using,亲爱的团队:, 在我的应用程序中,我想使用itextsharp拆分pdf。如果我上传的PDF包含10页,文件大小为10MB,用于拆分,拆分后每个PDF的合并文件大小将导致文件大小超过20mb。如果可能,请减小文件大小(每个pdf) 请帮我解决这个问题 提前感谢您是否已尝试设置写入程序的压缩 Document doc = new Document(); using (MemoryStream ms = new MemoryStream()) { PdfWrite

亲爱的团队:, 在我的应用程序中,我想使用itextsharp拆分pdf。如果我上传的PDF包含10页,文件大小为10MB,用于拆分,拆分后每个PDF的合并文件大小将导致文件大小超过20mb。如果可能,请减小文件大小(每个pdf)

请帮我解决这个问题


提前感谢

您是否已尝试设置写入程序的压缩

Document doc = new Document();
    using (MemoryStream ms = new MemoryStream())  
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        writer.SetFullCompression();
    }

你有没有试过在书写器上设置压缩

Document doc = new Document();
    using (MemoryStream ms = new MemoryStream())  
    {
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        writer.SetFullCompression();
    }

这可能与文件中的资源有关。例如,如果原始文档在每个文档上使用嵌入字体,则原始文件中只有一个字体实例。当您拆分它时,每个文件都需要具有该字体。总开销为n页×大小(每种字体)。导致这种膨胀的元素包括字体、图像、颜色配置文件、文档模板(又名表单)、XMP等

虽然它不能帮助您解决眼前的问题,但如果您使用dotImage中的PDF工具,您的任务将变成一行:

PdfDocument.Separate(userpassword, ownerpassword, origPath, destFolder, "Separated Page{0}.pdf", true);
它将获取原始文件中的PDF,并在dest文件夹中创建新页面,每个页面都以模式命名。最后的目标是覆盖现有文件


免责声明:我为Atalasoft工作并编写了PDF库(也用于Adobe的Acrobat版本1、2、3和4)。

这可能与文件中的资源有关。例如,如果原始文档在每个文档上使用嵌入字体,则原始文件中只有一个字体实例。当您拆分它时,每个文件都需要具有该字体。总开销为n页×大小(每种字体)。导致这种膨胀的元素包括字体、图像、颜色配置文件、文档模板(又名表单)、XMP等

虽然它不能帮助您解决眼前的问题,但如果您使用dotImage中的PDF工具,您的任务将变成一行:

PdfDocument.Separate(userpassword, ownerpassword, origPath, destFolder, "Separated Page{0}.pdf", true);
它将获取原始文件中的PDF,并在dest文件夹中创建新页面,每个页面都以模式命名。最后的目标是覆盖现有文件


免责声明:我为Atalasoft工作并编写了PDF库(也曾在Adobe的Acrobat 1、2、3和4版上使用)。

嗨,伙计们,我修改了上述代码,将一个PDF文件拆分为多个PDF文件

        iTextSharp.text.pdf.PdfReader reader = null;
        int currentPage = 1;
        int pageCount = 0;
        //string filepath_New = filepath + "\\PDFDestination\\";

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //byte[] arrayofPassword = encoding.GetBytes(ExistingFilePassword);
        reader = new iTextSharp.text.pdf.PdfReader(filepath);
        reader.RemoveUnusedObjects();
        pageCount = reader.NumberOfPages;
        string ext = System.IO.Path.GetExtension(filepath);
        for (int i = 1; i <= pageCount; i++)
        {
            iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
            string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
            reader1.RemoveUnusedObjects();
            iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
            iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
            doc.Open();
            for (int j = 1; j <= 1; j++)
            {
                iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
                pdfCpy.SetFullCompression();
                pdfCpy.AddPage(page);
                currentPage += 1;
            }
            doc.Close();
            pdfCpy.Close();
            reader1.Close();
            reader.Close();

        }
iTextSharp.text.pdf.PdfReader reader=null;
int currentPage=1;
int pageCount=0;
//字符串filepath_New=filepath+“\\pdfdestation\\”;
System.Text.UTF8Encoding encoding=新的System.Text.UTF8Encoding();
//byte[]arrayofPassword=encoding.GetBytes(ExistingFilePassword);
reader=new iTextSharp.text.pdf.PdfReader(文件路径);
reader.RemoveUnusedObjects();
pageCount=reader.NumberOfPages;
字符串ext=System.IO.Path.GetExtension(filepath);

对于(inti=1;i大家好,我修改了上面的代码,将一个PDF文件拆分为多个PDF文件

        iTextSharp.text.pdf.PdfReader reader = null;
        int currentPage = 1;
        int pageCount = 0;
        //string filepath_New = filepath + "\\PDFDestination\\";

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //byte[] arrayofPassword = encoding.GetBytes(ExistingFilePassword);
        reader = new iTextSharp.text.pdf.PdfReader(filepath);
        reader.RemoveUnusedObjects();
        pageCount = reader.NumberOfPages;
        string ext = System.IO.Path.GetExtension(filepath);
        for (int i = 1; i <= pageCount; i++)
        {
            iTextSharp.text.pdf.PdfReader reader1 = new iTextSharp.text.pdf.PdfReader(filepath);
            string outfile = filepath.Replace((System.IO.Path.GetFileName(filepath)), (System.IO.Path.GetFileName(filepath).Replace(".pdf", "") + "_" + i.ToString()) + ext);
            reader1.RemoveUnusedObjects();
            iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage));
            iTextSharp.text.pdf.PdfCopy pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create));
            doc.Open();
            for (int j = 1; j <= 1; j++)
            {
                iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader1, currentPage);
                pdfCpy.SetFullCompression();
                pdfCpy.AddPage(page);
                currentPage += 1;
            }
            doc.Close();
            pdfCpy.Close();
            reader1.Close();
            reader.Close();

        }
iTextSharp.text.pdf.PdfReader reader=null;
int currentPage=1;
int pageCount=0;
//字符串filepath_New=filepath+“\\pdfdestation\\”;
System.Text.UTF8Encoding encoding=新的System.Text.UTF8Encoding();
//byte[]arrayofPassword=encoding.GetBytes(ExistingFilePassword);
reader=new iTextSharp.text.pdf.PdfReader(文件路径);
reader.RemoveUnusedObjects();
pageCount=reader.NumberOfPages;
字符串ext=System.IO.Path.GetExtension(filepath);

对于(int i=1;i)我们可以看到输入和输出PDF吗?我们可以看到输入和输出PDF吗?