Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Mar 19, 2020 8:18 am

Is it possibile to split a single page of a pdf?
I mean i've got a pdf, and i want to split into desidered pieces and put every single piece in another pdf file

hakzi9898
 
Posts: 7
Joined: Wed Mar 18, 2020 7:59 pm

Thu Mar 19, 2020 10:11 am

Hello,

Thanks for your inquiry.
The following code demonstrates how to split a single PDF page into several equally sized pages in the horizontal and vertical directions.
Code: Select all
    static void Main(string[] args)
    {
        PdfDocument doc = new PdfDocument();
        doc.LoadFromFile("test.pdf");

        //Number of pieces in the horizontal direction
        int horizontalNumber = 3;
        //Number of pieces in the vertical direction
        int verticalNumber = 2;
        //!!!Note, horizontalNumber and verticalNumber must be > 0

        //Split the page horizontally into equal-sized pages
        PdfDocument temPdf = Split(doc, horizontalNumber, true);
        //Split the page vertically into equal-sized pages
        PdfDocument newPdf = Split(temPdf, verticalNumber, false);

        //Save the Pdf document
        string output = "output.pdf";
        newPdf.SaveToFile(output);
    }

    public static PdfDocument Split(PdfDocument pdf, int number, bool isHorizontal)
    {
        //Create a new Pdf
        PdfDocument newPdf = new PdfDocument();
        //Remove all the margins
        newPdf.PageSettings.Margins.All = 0;

        //Spilt pages
        foreach (PdfPageBase page in pdf.Pages)
        {
            //Set the page size of new Pdf
            if (isHorizontal)
            {
                newPdf.PageSettings.Width = page.Size.Width;
                newPdf.PageSettings.Height = page.Size.Height / number;
            }
            else
            {
                newPdf.PageSettings.Width = page.Size.Width / number;
                newPdf.PageSettings.Height = page.Size.Height;
            }
            //Add a new page
            PdfPageBase newPage = newPdf.Pages.Add();
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;
            //Draw the page in the new page
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
        }       
        return newPdf;
    }

If this does not meet your needs well, please provide your input file along with your desired output file, then we will do further investigation. You could upload here or send them to us(support@e-iceblue.com) via email.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Mar 27, 2020 10:06 am

Hello,

Greetings from E-iceblue!
Have you tested my code? Could you please give some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Apr 03, 2020 7:02 pm

Hi,
thank you for the answer.
The code is working, but what if i want to cut not in same size but for exaple the first block in a certain width and the second block in another? Is that possible?

hakzi9898
 
Posts: 7
Joined: Wed Mar 18, 2020 7:59 pm

Mon Apr 06, 2020 2:50 am

Hi,

Thanks for your response.
Please refer to the following code to split a page into two blocks with the different sizes.
Code: Select all
 PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("input.pdf");
            PdfPageBase page = doc.Pages[0];
            float width = page.Size.Width;
            float height1 = 300;
            float height2 = page.Size.Height - height1;
            PdfDocument newDoc = new PdfDocument();
            PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height1), new Spire.Pdf.Graphics.PdfMargins(0));
            page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, 0));
            newPage = newDoc.Pages.Add(new SizeF(width, height2), new Spire.Pdf.Graphics.PdfMargins(0));
            page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, -height1));

            string result = "result.pdf";
            newDoc.SaveToFile(result);


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2767
Joined: Wed Jun 27, 2012 8:50 am

Thu Apr 09, 2020 7:27 am

Hi,

Hope you are doing well.
Does my code help for you?
Looking forward to your feedback.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2767
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF