none
C# How to write page number when split large pdf file into small multiple pdf file RRS feed

  • Question

  • I have many large pdf files which has many pages. i have to split those large pdf into small multiple pdf files.

    i have a code which i can use to split large pdf into small multiple pdf files. below code i am using for splitting.
    private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)  
            {            
                using (PdfReader reader = new PdfReader(pdfFilePath))  
                {  
                    Document document = new Document();  
                    PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));  
                    document.Open();  
    
                    for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)  
                    {  
                        if (reader.NumberOfPages >= pagenumber)  
                        {  
                            copy.AddPage(copy.GetImportedPage(reader, pagenumber));  
                        }  
                        else  
                        {  
                            break;  
                        }  
    
                    }  
    
                    document.Close();  
                }  
            }  


    code taken from https://www.c-sharpcorner.com/article/splitting-pdf-file-in-c-sharp-using-itextsharp/

    i got a routine which add page number. routine attached
    void AddPageNumber(string fileIn, string fileOut)
    {
        byte[] bytes = File.ReadAllBytes(fileIn);
        Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
        using (MemoryStream stream = new MemoryStream())
        {
            PdfReader reader = new PdfReader(bytes);
            using (PdfStamper stamper = new PdfStamper(reader, stream))
            {
                int pages = reader.NumberOfPages;
                for (int i = 1; i <= pages; i++)
                {
                    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                }
            }
            bytes = stream.ToArray();
        }
        File.WriteAllBytes(fileOut, bytes);
    }

    Now my question is how to incorporate or call AddPageNumber() routine from SplitAndSaveInterval() routine to finally add page number on each page at top right portion of newly generated pdf file.

    looking for help and suggestion. thanks


    Tuesday, August 14, 2018 5:49 AM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    If you want to set the page number on each page at top right portion of the newly generated PDF file, you need to change the parameter of your AddPageNumber method.

    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_MIDDLE, new Phrase(i.ToString(), blackFont), 550, 752, 0);                

    According to your another thread about split PDF to multiple files, I make a simple example for your reference.

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class Add_PDF_pages_to_split_files
        {
            static void Main(string[] args)
            {
                int interval = 5;
                Split(interval);
            }
    
            public static void Split(int interval)
            {
                string pdfFilePath = @"Split PDF file2.pdf";
                string outputPath = @"C:\Users\wendyz.WICRESOFT.000\Desktop\PDF";
    
                int pageNameSuffix = 0;
    
                // Intialize a new PdfReader instance with the contents of the source Pdf file:  
                PdfReader reader = new PdfReader(pdfFilePath);
    
                FileInfo file = new FileInfo(pdfFilePath);
                string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";
    
                List<int> list = new List<int>();
                int value1 = reader.NumberOfPages / interval;// 9/2   4
                int value2 = reader.NumberOfPages % interval;// 9%2   1
    
                if (interval != 1)
                {
                    for (int i = 0; i < interval - 1; i++)
                    {
                        list.Add(value1);
                    }
                    list.Add(value1 + value2);
                }
                else
                {
                    for (int i = 0; i < reader.NumberOfPages; i++)
                    {
                        list.Add(interval);
                    }
                }
    
                int startPage = 1;
                foreach (var page in list)//4,5
                {
                    pageNameSuffix++;
                    string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
                    SplitAndSaveInterval(pdfFilePath, outputPath, startPage, page, newPdfFileName);
                    AddPageNumber(outputPath+"\\"+newPdfFileName+ ".pdf", outputPath + "\\" + newPdfFileName + ".pdf");
                    startPage = startPage + page;
                }
            }
    
            public static void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int page, string pdfFileName)
            {
                using (PdfReader reader = new PdfReader(pdfFilePath))
                {
                    Document document = new Document();
                    PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
                    document.Open();
    
                    for (int pagenumber = startPage; pagenumber < (startPage + page); pagenumber++)
                    {
                        if (reader.NumberOfPages >= pagenumber)
                        {
                            copy.AddPage(copy.GetImportedPage(reader, pagenumber));
                        }
                        else
                        {
                            break;
                        }
                    }
                    document.Close();
                }
            }
            static void AddPageNumber(string fileIn, string fileOut)
            {
                byte[] bytes = File.ReadAllBytes(fileIn);
                Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
                using (MemoryStream stream = new MemoryStream())
                {
                    PdfReader reader = new PdfReader(bytes);
                    using (PdfStamper stamper = new PdfStamper(reader, stream))
                    {
                        int pages = reader.NumberOfPages;
                        for (int i = 1; i <= pages; i++)
                        {
                            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_MIDDLE, new Phrase(i.ToString(), blackFont), 550, 752, 0);
                        }
                    }
                    bytes = stream.ToArray();
                }
                File.WriteAllBytes(fileOut, bytes);
            }
        }
    }
    

    I PDF file "Split PDF Files2-5.pdf" is with one number in each page, like 5,6,7,8,9. I add page number 1,2,3,4,5 on top right.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, August 15, 2018 10:11 AM
    Wednesday, August 15, 2018 4:49 AM
    Moderator

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    If you want to set the page number on each page at top right portion of the newly generated PDF file, you need to change the parameter of your AddPageNumber method.

    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_MIDDLE, new Phrase(i.ToString(), blackFont), 550, 752, 0);                

    According to your another thread about split PDF to multiple files, I make a simple example for your reference.

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
        class Add_PDF_pages_to_split_files
        {
            static void Main(string[] args)
            {
                int interval = 5;
                Split(interval);
            }
    
            public static void Split(int interval)
            {
                string pdfFilePath = @"Split PDF file2.pdf";
                string outputPath = @"C:\Users\wendyz.WICRESOFT.000\Desktop\PDF";
    
                int pageNameSuffix = 0;
    
                // Intialize a new PdfReader instance with the contents of the source Pdf file:  
                PdfReader reader = new PdfReader(pdfFilePath);
    
                FileInfo file = new FileInfo(pdfFilePath);
                string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";
    
                List<int> list = new List<int>();
                int value1 = reader.NumberOfPages / interval;// 9/2   4
                int value2 = reader.NumberOfPages % interval;// 9%2   1
    
                if (interval != 1)
                {
                    for (int i = 0; i < interval - 1; i++)
                    {
                        list.Add(value1);
                    }
                    list.Add(value1 + value2);
                }
                else
                {
                    for (int i = 0; i < reader.NumberOfPages; i++)
                    {
                        list.Add(interval);
                    }
                }
    
                int startPage = 1;
                foreach (var page in list)//4,5
                {
                    pageNameSuffix++;
                    string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
                    SplitAndSaveInterval(pdfFilePath, outputPath, startPage, page, newPdfFileName);
                    AddPageNumber(outputPath+"\\"+newPdfFileName+ ".pdf", outputPath + "\\" + newPdfFileName + ".pdf");
                    startPage = startPage + page;
                }
            }
    
            public static void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int page, string pdfFileName)
            {
                using (PdfReader reader = new PdfReader(pdfFilePath))
                {
                    Document document = new Document();
                    PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
                    document.Open();
    
                    for (int pagenumber = startPage; pagenumber < (startPage + page); pagenumber++)
                    {
                        if (reader.NumberOfPages >= pagenumber)
                        {
                            copy.AddPage(copy.GetImportedPage(reader, pagenumber));
                        }
                        else
                        {
                            break;
                        }
                    }
                    document.Close();
                }
            }
            static void AddPageNumber(string fileIn, string fileOut)
            {
                byte[] bytes = File.ReadAllBytes(fileIn);
                Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
                using (MemoryStream stream = new MemoryStream())
                {
                    PdfReader reader = new PdfReader(bytes);
                    using (PdfStamper stamper = new PdfStamper(reader, stream))
                    {
                        int pages = reader.NumberOfPages;
                        for (int i = 1; i <= pages; i++)
                        {
                            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_MIDDLE, new Phrase(i.ToString(), blackFont), 550, 752, 0);
                        }
                    }
                    bytes = stream.ToArray();
                }
                File.WriteAllBytes(fileOut, bytes);
            }
        }
    }
    

    I PDF file "Split PDF Files2-5.pdf" is with one number in each page, like 5,6,7,8,9. I add page number 1,2,3,4,5 on top right.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, August 15, 2018 10:11 AM
    Wednesday, August 15, 2018 4:49 AM
    Moderator
  • thanks for answer sir. if we call these routine one after one SplitAndSaveInterval() and AddPageNumber() then we read files twice rather can't we add page no when copy page from one pdf to another?

    here i am pasting one ur http://what-when-how.com/itext-5/copying-pages-with-pdfcopy-itext-5/.

    see these code and tell me can i follow below code to add page no when copying page from one pdf to another ?

    Wednesday, August 15, 2018 10:17 AM