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.

Tue Apr 21, 2020 3:59 pm

Hi,

I was wondering if it is possible to save (not print) a pdf document with a certain number of pages per sheet.

I checked the documentation and previous post at the forums and i only find the option to Print a pdf document with multiple pages per sheet, this is not what i'm looking. I need to save (document.Save...()) the document, not sending to print (document.Print)

Edit: typos

mark_jhonson1554
 
Posts: 2
Joined: Tue Apr 21, 2020 3:51 pm

Wed Apr 22, 2020 6:15 am

Hello,

Thanks for your inquiry.
Our Spire.PDF does not have a direct method to save multiple pdf pages per sheet. But you can refer to the following code to achieve the same effect. If there is any question, please provide your input file as well as your desired output. Then we will do further investigation.
Code: Select all
    static void Main(string[] args)
    {
        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromFile("input.pdf");
        //Save two pdf pages per sheet
        PdfDocument newPdf = MultiPageLayout(pdf, 1, 2);
        newPdf.SaveToFile("out.pdf", FileFormat.PDF);
    }

    public static PdfDocument MultiPageLayout(PdfDocument pdf, int rows, int columns)
    {
        //Page count
        int count = rows * columns;
        //Create a new Pdf
        PdfDocument newPdf = new PdfDocument();
        //Remove all the margins
        newPdf.PageSettings.Margins.All = 0;
        //Set the page size of new Pdf
        newPdf.PageSettings.Size = PdfPageSize.A4;
        List<PointF> location = new List<PointF>();
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                float x = (PdfPageSize.A4.Width / columns) * (j + 0.5f);
                float y = (PdfPageSize.A4.Height / rows) * (i + 0.5f);
                location.Add(new PointF(x, y));
            }
        }
        for (int i = 0; i < pdf.Pages.Count; i += count)
        {
            //Get all pdf pages to be drawn on a new page
            List<PdfPageBase> pages = new List<PdfPageBase>();
            for (int j = 0; j < count; j++)
            {
                if (i + j >= pdf.Pages.Count)
                {
                    break;
                }
                else
                {
                    PdfPageBase page = pdf.Pages[i + j];
                    pages.Add(page);
                }
            }
            //Add a new page
            PdfPageBase newPage = newPdf.Pages.Add();
            for (int c = 0; c < pages.Count; c++)
            {
                //Calculate the horizontal and vertical scale
                float scaleH = PdfPageSize.A4.Width / pages[c].Size.Width / columns;
                float scaleV = PdfPageSize.A4.Height / pages[c].Size.Height / rows;
                //Take the smaller of the two values
                float scale = scaleH < scaleV ? scaleH : scaleV;
                //Calculate the scaled page size
                float newWidth = pages[c].Size.Width * scale;
                float newHeight = pages[c].Size.Height * scale;
                PointF loca = new PointF(location[c].X - newWidth / 2, location[c].Y - newHeight / 2);
                //Draw the original page onto the newPage
                newPage.Canvas.DrawTemplate(pages[c].CreateTemplate(), loca, new SizeF(newWidth, newHeight));
            }
        }
        return newPdf;
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Apr 22, 2020 1:29 pm

Hi Rachel,

thanks for the response.

The code provided is not necessarily what i was expecting but it may work for my needs.

anyway, Are there any plans to implement the functionality to save multiple pages per sheet in the future?

Thanks for all

mark_jhonson1554
 
Posts: 2
Joined: Tue Apr 21, 2020 3:51 pm

Thu Apr 23, 2020 3:33 am

Hello,

Thanks for your response.
Sorry to tell that this function is not planned in the current upgrade list of our Spire.PDF.
If using the code I provided doesn't work well for your needs, please provide us with your input file and your desired output for reference. Then we'll do further investigation.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Return to Spire.PDF