Hey,
I have a 50MB file of 2500 pages which I want to split into multiple files. Size of each part must not be greater than 8MB. How can I achieve this?
Aspose.PDF does not provide any direct functionality to check the file size. However, you can achieve it using C# classes as a workaround. Please check below code snippet and we hope it will help:
Document destinationPdfDocument = new Document();
Document sourcePdfDocument = new Document();
var filesFromDirectory = Directory.GetFiles(dataDir, "*.pdf");
for (int i = 0; i < filesFromDirectory.Count(); i++)
{
if (i == 0)
{
destinationPdfDocument = new Document(filesFromDirectory[i]);
}
else
{
// Open second document
sourcePdfDocument = new Document(filesFromDirectory[i]);
// Add pages of second document to the first
destinationPdfDocument.Pages.Add(sourcePdfDocument.Pages);
//** I need to check size of destinationPdfDocument over here to limit the size of resultant PDF**
MemoryStream ms = new MemoryStream();
destinationPdfDocument.Save(ms);
long filesize = ms.Length;
ms.Flush();
// Compare the filesize with MBs
if (i == filesFromDirectory.Count() - 1)
{
destinationPdfDocument.Save(dataDir + "PDFOutput_" + i + ".pdf");
}
else if ((filesize / (1024 * 1024)) < 1) // Check if PDF is less than 1MB
continue;
else
{
destinationPdfDocument.Save(dataDir + "PDFOutput_" + i.ToString() + ".pdf");
destinationPdfDocument = new Document();
}
}
}
// Encrypt PDF
destinationPdfDocument.Encrypt("userP", "ownerP", 0, CryptoAlgorithm.AESx128);
// Save concatenated output file
destinationPdfDocument.Save(dataDir + "CombinedPDF.pdf");
@asad.ali
I was working with Aspose Java and did try to use the same logic in Java but it deosn’t seem to work for me.