【问题标题】:Split large PDF file in to multiple pdfs in C#在 C# 中将大型 PDF 文件拆分为多个 pdf
【发布时间】:2021-12-16 05:58:15
【问题描述】:

我有一个大的 pdf 文件,在上传到服务器(另一个 wcf 服务)之前,我需要将其拆分为多个 pdf 或块。 我有两种方法可以将大文件(> 2 MB)发送到服务器,方法是将它们拆分为多个 pdf 或一个 pdf 成块。谁能告诉我这是如何实现的? 我找到了使用 iTextSharp 的文章,但它已被弃用。我不使用许可的。我们有什么可行的方法来实现这一点吗?

我已关注以下文章。但他们使用了已弃用的 iTextshap。 https://www.c-sharpcorner.com/article/splitting-pdf-file-in-c-sharp-using-itextsharp/

【问题讨论】:

  • iTextSharp 未被弃用。只有旧的免费啤酒版本是。当前完全支持的版本仅对非商业项目免费。您也不需要将 PDF 拆分为多个文件来分段上传。 Chunks 表示只是一堆字节。如果你想要一个 1000 字节的块,你只需从文件中读取 1000 字节。你需要什么实际上取决于服务需要什么。
  • 感谢您的回复!我在为一家公司工作,那么它可能是商业的。在 nuget 包中它显示它已被弃用。我的实际要求是我需要从服务参考调用上传方法并发送限制为 2MB 的文档。如果文件是大于 2 MB 我必须作为多个或块上传。对此如何实现有任何想法吗?
  • 谢谢PJ!!您能否建议任何解决方案来上传大型 pdf,将它们分成几部分?

标签: c# .net pdf merge split


【解决方案1】:
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;


class Program
{
    // Output Folder
    static string outputFolder = @"D:\PDFSplit\Example\outputFolder";

    static void Main(string[] args)
    {
        // Input Folder
        var inputFolder = @"D:\PDFSplit\Example\inputFolder";           

        // Input File name
        var inputPDFFileName = "sample.pdf";

        // Input file path
        string inputPDFFilePath = Path.Combine(inputFolder, inputPDFFileName);

        // Open the input file in Import Mode
        PdfDocument inputPDFFile = PdfReader.Open(inputPDFFilePath, PdfDocumentOpenMode.Import);

        //Get the total pages in the PDF
        var totalPagesInInputPDFFile = inputPDFFile.PageCount;

        while(totalPagesInInputPDFFile !=0)
        {
            //Create an instance of the PDF document in memory
            PdfDocument outputPDFDocument = new PdfDocument();
           
            // Add a specific page to the PdfDocument instance
            outputPDFDocument.AddPage(inputPDFFile.Pages[totalPagesInInputPDFFile-1]);

            //save the PDF document
            SaveOutputPDF(outputPDFDocument, totalPagesInInputPDFFile);
            
            totalPagesInInputPDFFile--;
        }           
    }

    private static void SaveOutputPDF(PdfDocument outputPDFDocument,int pageNo)
    {
        // Output file path
        string outputPDFFilePath = Path.Combine(outputFolder, pageNo.ToString() + ".pdf"); 

        //Save the document
        outputPDFDocument.Save(outputPDFFilePath);
    }
}

【讨论】:

  • 您能否确认这解决了您的问题?您能否解释一下您所做的哪些具体更改最相关?
  • 是的,上面的解决方案帮助我们将大的pdf文件拆分成多个页面。使用nuget中的pdfsharp dll
【解决方案2】:

拆分pdf的第一件事就是缩小文件大小,拆分成serval文件,然后分析。首先,您需要将需要拆分的 pdf 文档,然后您需要使用输出文件流调用拆分函数。

PdfLoadedDocument document = new PdfLoadedDocument("sample.pdf");
document.Split("Document-{0}.pdf");
document.Close(true);

还有另一种方法,首先需要使用 Document 类加载 PDF 文档,然后选择要拆分的页面到 Page[] 数组中。之后创建一个新文档并使用 Document.Pages.Add(Page[]) 方法向其中添加页面。 使用 Document.Save(String) 方法保存 PDF 文件。

【讨论】:

    【解决方案3】:

    尝试使用流,例如 StreamReader。

    查看meatvest的回答here

    还有docs

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 2022-12-19
      • 2010-10-04
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      相关资源
      最近更新 更多