0

I have found a inbuilt method to split pdf by size, however i am not sure about the parameter as it is asking for parameter to split the pdf (long size) and not sure using the parameter the split file in kb, mb. Can someone please suggest me the usage of this parameter, support i want to split the file of 95MB into 5MB files, so what value i need to pass as parameter i.e. 5000, 500, 50 or 5.

Code:

       PdfReader reader = new PdfReader(fileName);
       PdfDocument sourceDoc = new PdfDocument(reader);

       IList<PdfDocument> pdfDocs = new CustomPdfSplitter(sourceDoc, outputFileName).SplitBySize(fileSize);

Code for CustomPdfSplitter:

private class CustomPdfSplitter : PdfSplitter
    {
        private String dest;
        private int partNumber = 1;

        public CustomPdfSplitter(PdfDocument pdfDocument, String dest) : base(pdfDocument)
        {
            this.dest = dest;
        }

        protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
        {
            return new PdfWriter(dest.Replace(".pdf","_" + (partNumber++).ToString("0000")+".pdf"));
        }
    }

public string SplitPDFBySize(string fileName, string outputFileName, int fileSize)
    {
        StringBuilder outputMsg = new StringBuilder();
        PdfReader reader = new PdfReader(fileName);
        PdfDocument sourceDoc = new PdfDocument(reader);

        long fileSizeinByte = fileSize * 1000000;

        IList<PdfDocument> pdfDocs = new CustomPdfSplitter(sourceDoc, outputFileName).SplitBySize(fileSizeinByte);

        try
        {
            PBChild.Value = 0;
            PBChild.Maximum = pdfDocs.Count;


            for (int i = 0; i < pdfDocs.Count; i++)
            {
                string splittedFile = outputFileName.Replace(".pdf", "_" + (i + 1).ToString("0000") + ".pdf");
                pdfDocs[i].Close();

                outputMsg.AppendLine(fileName + " splitted with output file: " + splittedFile);
                PBChild.Value++;
            }
        }
        catch (Exception ex)
        {
            outputMsg.AppendLine("Error: " + fileName + "||" + ex.Message);
        }
        finally
        {
            sourceDoc.Close();
        }

        return outputMsg.ToString();
    }
  • what does the documentation say? – jazb Feb 15 '22 at 06:13
  • Does this answer your question? [How to split PDF document into small ones](https://stackoverflow.com/questions/49930344/how-to-split-pdf-document-into-small-ones) – Doc Brown Feb 15 '22 at 06:30
  • in documentation i am unable to found the required answer. Also i have already implemented the split pdf by page using similar to link suggested by Doc Brown. However i am unable to split pdf by size using iText7's split by size function as parameter mentioned doesn't spcified whether it is for kb, mb. – Mayur Mehta Feb 15 '22 at 06:41
  • *"I have found a inbuilt method to split pdf by size"* - You might want to explain _where_ you found that method. iText 7 includes a `PdfSplitter` which for use often needs to be customized, and most likely every other such _customized `PdfSplitter`_ class is called `CustomPdfSplitter` like in your case... – mkl Feb 15 '22 at 11:23
  • I have used customPDFsplitter which is extension to pdfsplitter (code is already available in many portals). Issue or problem i am facing is split by size is asking for a parameter of long type and i am unable to identify is it for split file in KB, MB or anything else because passing 200 as a parameter doesn't clear to it's unit i.e. KB, MB. Also the generated files are one page files for me for each of the parameters 50, 500, 5000. @mkl – Mayur Mehta Feb 16 '22 at 05:45
  • *"code is already available in many portals"* - you should provide the code (or at least a link there). As i pointed out above, there are *different* `PdfSplitter` extensions around called `CustomPdfSplitter`... – mkl Feb 16 '22 at 06:48
  • I have added custompdfsplitter code now. – Mayur Mehta Feb 16 '22 at 10:10
  • Ok, now I see what you mean. According to the JavaDocs and the code underneath, that parameter should be the length in bytes. For 5MB, therefore, the parameter should be a bit more than 5000000. – mkl Feb 16 '22 at 11:40
  • @mkl I have tried with the code but it is generating 1 page file only. I have added my code to split files by size in my questions. added code to split the file: filename--> file to split, outputfilename-->prefix for output file name and part number will be added at the end of the file name, filesize--> filesize in MB – Mayur Mehta Feb 16 '22 at 12:54

1 Answers1

0

SplitBySize take as parameter size specified in bytes. So, to split document into 5 MB documents you need to pass 5*2^20 as parameter. Although, you're right that the documentation was slightly uninformative. We've already fixed that and you can find these fixes in the SNAPSHOT version.

  • I have tried by implementing "long fileSizeinByte = (long)Math.Pow(fileSize*2, 20);", however i am still getting 1 page file. – Mayur Mehta Mar 03 '22 at 09:51