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/
CodePudding user response:
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);
}
}
CodePudding user response:
Try using streams, for instance StreamReader.
See meatvest's answer here
And the docs