We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF Print document with multiple page orientations to portrait orientation

We receive PDFs (mostly medical records) from multiple sources and put them through a PDF printing process in an effort to set all page sizes to letter and align pages vertically. We use the code snippet below and it works well the majority of the time, but occasionally we get PDFs where the zoom setting on some of the pages is scaled down because the original page was scanned in at a horizontal orientation. For example, using the code below, the value of scale is typically 1 for a page that was scanned in with a portrait orientation. However, the value of scale for a page that was scanned in horizontally is .77, which shrinks the page contents to the lower left corner of the page. I have examples I can send.

Is there a way to set all page orientations the same so the zoom value does not shrink the page contents?

Code:

        using (var existingPdf = new Document(fullSourcePath))
        {
            using (var pEdit = new PdfPageEditor(existingPdf))
            {
                for (int i = 1; i <= existingPdf.Pages.Count; i++)
                {
                    var page = existingPdf.Pages[i];

                    var pageWidth = (float)page.Rect.Width;
                    var pageHeight = (float)page.Rect.Height;

                    var pageLetter = Aspose.Pdf.PageSize.PageLetter;

                    if (pageWidth == pageLetter.Width && pageHeight == pageLetter.Height)
                        continue;

                    var wScale = pageLetter.Width / pageWidth;
                    var hScale = pageLetter.Height / pageHeight;

                    var scale = new[] { 1f, wScale, hScale }.Min();

                    pEdit.ProcessPages = new[] { i };
                    pEdit.PageSize = Aspose.Pdf.PageSize.PageLetter;
                    pEdit.VerticalAlignmentType = VerticalAlignment.Top;
                    pEdit.HorizontalAlignment = HorizontalAlignment.Left;
                    pEdit.Zoom = scale;

                    pEdit.ApplyChanges();
                }

                pEdit.Save(fullDestinationPath);

                return true;
            }
        }

@kmalecek,

You can get or set Rotate property of the PDF page as follows:
C#

Document document = new Document(dataDir + "input.pdf");
// set orientation of the first page
document.Pages[1].Rotate = Rotation.on90;  

However, if this does not help, then kindly send us your source PDF file and highlight the problematic area with a snapshot.

I attached 2 PDFs. The Before Process PDF file is what the file looks like before it runs through the code above. The After Process PDF file is what the file looks like after it runs through the code. The result we are looking for is for the file to look the same as it does in the Before Process PDF file after it runs through the code. Even though the Before Process PDF file appears to be in portrait orientation, the page.Rect.Width is greater than the page.Rect.Height, so the zoom shrinks the contents to the bottom corner.

I attempted to rotate the page using Rotation.on90, but zoom factor remained and the contents of the page were flipped upside down. Would the correct course of action be to flip the values of the page.Rect.Height and page.Rect.Width or is there a better way?

Thanks again,

Kurt

After_Process_Pdf.pdf (580.7 KB)
Before Process PDF.pdf (580.6 KB)

@kmalecek,

Please try the following code to rotate PDF page:
C#

using (var existingPdf = new Document(dataDir + "Before Process PDF.pdf"))
{
    using (var pEdit = new PdfPageEditor(existingPdf))
    {
        for (int i = 1; i <= existingPdf.Pages.Count; i++)
        {
            var page = existingPdf.Pages[i];
            var pageWidth = (float)page.Rect.Width;
            var pageHeight = (float)page.Rect.Height;
            var pageLetter = Aspose.Pdf.PageSize.PageLetter;

            if (pageWidth == pageLetter.Width && pageHeight == pageLetter.Height)
                continue;    
            var wScale = pageLetter.Width / pageWidth;
            var hScale = pageLetter.Height / pageHeight;
            var scale = new[] { 1f, wScale, hScale }.Min();
            pEdit.ProcessPages = new[] { i };
            pEdit.PageSize = Aspose.Pdf.PageSize.PageLetter;
            pEdit.VerticalAlignmentType = VerticalAlignment.Top;
            pEdit.HorizontalAlignment = HorizontalAlignment.Left;
            pEdit.Zoom = scale;
            existingPdf.Pages[i].Rotate = Rotation.on90;
            pEdit.ApplyChanges();
        }
        MemoryStream stream = new MemoryStream();
        pEdit.Save(stream);
        Document doc = new Document(stream);
        foreach (Aspose.Pdf.Page page in doc.Pages)
        {
            Aspose.Pdf.Rectangle r = page.MediaBox;
            double newHeight = r.Width;
            double newWidth = r.Height;
            double newLLX = r.LLX;
            //  We must to move page upper in order to compensate changing page size
            // (lower edge of the page is 0,0 and information is usually placed from the
            //  Top of the page. That's why we move lover edge upper on difference between
            //  Old and new height.
            double newLLY = r.LLY + (r.Height - newHeight);
            page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
            // Sometimes we also need to set CropBox (if it was set in original file)
            page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

            // Setting Rotation angle of page
            //page.Rotate = Rotation.on90;
        }
        doc.Save(dataDir + "Output_18.3.pdf");
    }
}

This is the output PDF: Output18.3.pdf (580.7 KB). In order further enhance the output, please manually create an expected output PDF, and then share with us.