【问题标题】:How can I split a PDF file by file size using C#?如何使用 C# 按文件大小拆分 PDF 文件?
【发布时间】:2012-07-27 17:48:03
【问题描述】:

我找到了一个 How to break a PDF into parts 教程,该教程演示了如何使用 Adob​​e Acrobat 按页面或最大文件大小将 PDF 文件拆分为单独的 PDF 文件:

我在 StackOverflow 上有 found many examples 关于如何使用 C# 按页拆分 PDF。但是我该怎么做后者呢?如何使用 C# 将一个 PDF 文件按最大文件大小拆分为多个 PDF 文件?

例如,假设我有一个 70 页和 40 MB 的 PDF 文件。我如何使用 C# 将文件拆分为大约 5 个不超过 10 MB 的 PDF 文件,而不是拆分为 7 个每个 10 页的 PDF 文件?

到目前为止,我见过的最好的方法是在Using itextsharp to split a pdf into smaller pdf's based on sizeCyfer13 使用iTextSharp 按页面拆分文件,然后按大小对这些页面文件进行分组。但是否有更直接的方式来完成此操作而无需先按页面拆分?

【问题讨论】:

    标签: c# pdf acrobat


    【解决方案1】:

    PDFsharp Sample: Split Document开始,我写了如下SplitBySize方法:

    public static void SplitBySize(string filename, long limit)
    {
        PdfDocument input = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
        PdfDocument output = CreateDocument(input);
    
        string name = Path.GetFileNameWithoutExtension(filename);
        string temp = string.Format("{0} - {1}.pdf", name, 0);
        int j = 1;
        for (int i = 0; i < input.PageCount; i++)
        {
            PdfPage page = input.Pages[i];
            output.AddPage(page);
            output.Save(temp);
            FileInfo info = new FileInfo(temp);
            if (info.Length <= limit)
            {
                string path = string.Format("{0} - {1}.pdf", name, j);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                File.Move(temp, path);
            }
            else
            {
                if (output.PageCount > 1)
                {
                    output = CreateDocument(input);
                    ++j;
                    --i;
                }
                else
                {
                    throw new Exception(
                        string.Format("Page #{0} is greater than the document size limit of {1} MB (size = {2})",
                        i + 1,
                        limit / 1E6,
                        info.Length));
                }
            }
        }
    }
    

    我将继续测试,但到目前为止它正在工作。

    【讨论】:

    • CreateDocument(input) 语句呢?你在那个函数里面有什么?
    • 你能把完整的代码放上去吗……挺有意思的……想试试看代码。
    【解决方案2】:

    这是一个未经测试的示例代码,假设您准备在纯二进制级别拆分,即 PDF Reader 不会读取这些部分,您必须重新加入这些部分以使其可读:

    下面的代码首先在 byte[] 数组中获取 pdf 文件。然后根据任意分区大小(本例中为 5),获取每个部分二进制文件的文件大小。然后,它将创建一个临时内存流并循环创建每个分区并写入一个新的 .part 文件。 (您可能需要进行一些更改才能使其可行)。

            byte[] pdfBytes = File.ReadAllBytes("c:\foo.pdf");
            int fileSize = pdfBytes.Length / 5; //assuming foo is 40MB filesize will be abt 8MB
            MemoryStream m = new MemoryStream(pdfBytes);
            for (int i = 0; i < 4; i++)
            {
                byte[] tbytes = new byte[fileSize];
                m.Read(tbytes,i*fileSize,fileSize);
                File.WriteAllBytes("C:\foo" + i + ".part",tbytes);
            }
    

    【讨论】:

    • 感谢您的回答,但我确实需要生成的文件为 PDF 格式。我已将我的问题更新为更具体。
    • 在这种情况下,也许您可​​以查看 PDFSharp,这是一个开源库,可让您通过 .NET 处理 PDF 文件:pdfsharp.codeplex.com
    猜你喜欢
    • 2014-02-17
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多