C# Equally divide pages by input value

T.Zacks 3,991 Reputation points
2022-09-17T19:12:23.973+00:00

suppose there is two textboxes on form. one textbox represent total pages in pdf file and one page where user will input value say 6 or 8

suppose total pages are 38 and user input 6 means 6 pages of pdf will go into one page of new pdf. it means new pdf file will have 7 pages where new pdf each pages will contains 6 page content of old pdf and last page 7 page will contain 2 page content.

so how to calculate these?

total page is 38 and each page will contain 6 pages. so programmatically how could i say there will be 7 pages i need to create in new pdf file?

38 and 6 is not fixed. please help me with sample code to handle this situation in such a way no error should occur.

i tried this way but no luck

        int totalrecords = 38;  
        int totalpage = totalrecords / 6;  
        int remainingpage = totalrecords % 6;  

also this does not work

        int totalrecords = 38;  
        int totalpage = totalrecords / 6;  
        int remainingpage = totalpage % 6;  

i need to find out

  1. how many pages i need to create in new pdf
  2. how many pages of old pdf should be incorporated in each page of new pdf?
  3. how many pages of old pdf need to be incorporated in the last page of new pdf

if total page is 38 of old pdf file and new pdf each page should have 6 page content of old pdf in each page. it means
so we need to create 7 pages for new pdf. each 6 pages of new pdf will have 6 pages content of old and and new pdf last page means 7th page should
have content last 2 pages content of old pdf.

i am looking for help with sample code where calculation will work accurately whatever total pages and no pages should be in new pdf file in each page.

thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,501 Reputation points Microsoft Vendor
    2022-09-21T03:17:22.673+00:00

    @ TZacks-2728, Welcome to Microsoft Q&A, you could try the following code to divide pages by input value.

     static void Main(string[] args)  
            {  
                foreach (var p in GetPage(38, 6))  
                {  
                    string abc = p.ToString();  
                    Console.WriteLine(abc);  
                }  
                Console.WriteLine("**");  
                foreach (var p in GetPage(35, 4))  
                {  
                    string abc = p.ToString();  
                    Console.WriteLine(abc);  
                }  
            }  
            public static IEnumerable<int> GetPage(int pagetotal, int userinput)  
            {  
                int size = pagetotal / userinput;  
                int rest = pagetotal % userinput;  
                int last_size =  rest;  
      
                for (int i = 0; i < size; i++)  
                {  
                    yield return userinput;  
                }  
                  
                yield return last_size;  
            }  
    

    Also, I make another test for (35,4), It outputs 8 pdf with 4 pages and one pdf with 3 pages.

    Result:

    243265-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.