0

I have a large pdf file which I need to split into multiple pdfs or chunks before I upload to the server(another wcf service). I have two approaches to send large files(>2 MB) to server by splitting them multiple pdfs or one pdf into chunks .Can any one tell me this how to achieve ? I found the articles using iTextSharp but it's deprecated one. I don't use licensed one. Do we have any feasible way to achieve this ?

I have followed the following article .But they have used iTextshap which is a deprecated one . https://www.c-sharpcorner.com/article/splitting-pdf-file-in-c-sharp-using-itextsharp/

mohan
  • 75
  • 1
  • 10
  • iTextSharp isn't deprecated. Only the old free-as-in-beer version is. The current, fully supported version is free only for non-commercial projects. You **don't** need to split a PDF in multiple files to upload it in parts either. `Chunks` means just a bunch of bytes. If you want a chunk of 1000 bytes, you just read 1000 bytes from a file. What you need actually depends on what the service requires though. – Panagiotis Kanavos Nov 01 '21 at 14:53
  • Thanks for the response ! I'm working for a company then it might be commercial .And in nuget package it is showing it's deprecated .My actual requirement is I need to call upload method from a service reference and send the document which restricts to 2MB.If the file is larger than 2 MB I have to upload as multiple or chunks .Any idea on this how to achieve ? – mohan Nov 01 '21 at 15:00
  • Thanks PJ!! Can you please suggest any solution to upload large pdfs by breaking them in to pieces? – mohan Nov 01 '21 at 18:00

3 Answers3

0
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);
    }
}
mohan
  • 75
  • 1
  • 10
  • 1
    Can you confirm that this solved your problem? Can you offer some explanation of what specific changes you made that were most relevant? – Jeremy Caney Nov 03 '21 at 00:10
  • Yes,the above solution helps us to split large pdf file into multiple pages.Use pdfsharp dll from nuget – mohan Nov 03 '21 at 12:56
0

The first thing to split the pdf is to reduce the size of the file and split into serval files and then analyze it. First you need to pdf document which you need to split and then you need to call the split function with the output file streams.

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

There is a another way, first you need to load the PDF document using Document class and then choose the pages to be split into a Page[] array. After that Create a new Document and add pages to it using Document.Pages.Add(Page[]) method. Save the PDF file using the Document.Save(String) method.

satheesh
  • 61
  • 2
-1

Try using streams, for instance StreamReader.

See meatvest's answer here

And the docs