iText - 如何在不旋转页面的情况下旋转 pdf 内容

iText - How to rotate pdf content without rotating a page

我有 pdf 文件,其中有很多 post 标签页面具有纵向方向。我需要缩小每个页面的现有内容,然后将其他信息添加到每个页面中。出于这个原因,我使用了 PDFStamper class,它在下面的代码中成功地缩小了内容。那么接下来的需求就是在不旋转页面的情况下,将页面内容旋转90度或者270度。页面必须以纵向离开,但缩小的内容必须旋转。我尝试使用 PdfDictionary class 并测试了很多代码变体,但内容没有按要求旋转。我需要建议什么代码块必须在代码评论后 - //代码只旋转 pdf 页面的内容 90 或 270 度离开页面纵向方向??? 我的代码如下:

using iTextSharp.text.pdf;
using Microsoft.Win32;
using System;
using System.IO;

namespace RotatePDFContent
{
    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            string directory = AppDomain.CurrentDomain.BaseDirectory;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = directory;
            ofd.DefaultExt = ".pdf";
            ofd.Filter = "PDF Files(.*PDF)|*.PDF|All Files(*.*)|*.*";
            string inputPdfFilePath;
            if (ofd.ShowDialog() == true)
            {
                inputPdfFilePath = ofd.FileName.ToString();
                using (Stream inputPdfStream = new FileStream(inputPdfFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {

                    PdfReader pdfReader = new PdfReader(inputPdfStream);
                    int pagesAmount = pdfReader.NumberOfPages;

                    string pdfInputsDirectoryPath = System.IO.Path.GetDirectoryName(inputPdfFilePath);

                    string resultFileName = "Output_" + Path.GetFileNameWithoutExtension(inputPdfFilePath) +
                        Path.GetExtension(inputPdfFilePath);
                    string resultFilePath = System.IO.Path.Combine(pdfInputsDirectoryPath, resultFileName);
                    using (Stream outputPdfStream =
                        new FileStream(resultFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        PdfStamper stamper = new PdfStamper(pdfReader, outputPdfStream);

                        for (int pageNumber = 1; pageNumber <= pagesAmount; pageNumber++)
                        {
                            //Shrink original page contents for the purpose to fit into required area
                            stamper.GetUnderContent(pageNumber).SetLiteral("\nq 0.53 0 0 0.53 10 25 cm\nq\n");

                            //Code to rotate only content of pdf page 90 or 270 degrees leaving page 
                           //in Portrait orientation???
                          //????????????  
                        }
                        stamper.Close();
                    }
                }
            }
        }
    }
}

我查看了问题iText - Rotate page content while creating PDF,但直到现在才从这个问题中找到我的代码的解决方案。 附件中的图像是初始结果和所需结果。

下面的代码是我的解决方案。在此解决方案中使用临时文件,其中保存缩小并旋转为横向 pdf 页面内容。第一步代码打开临时文件后,将 pdf 内容旋转到纵向(这是所需的内容位置)并将结果保存到文件。

using iTextSharp.text.pdf;
using Microsoft.Win32;
using System;
using System.IO;

namespace RotatePDFContent
{
    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            string directory = AppDomain.CurrentDomain.BaseDirectory;
            
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = directory;
            ofd.DefaultExt = ".pdf";
            ofd.Filter = "PDF Files(.*PDF)|*.PDF|All Files(*.*)|*.*";
            string inputPdfFilePath;
            if (ofd.ShowDialog() == true)
            {
                inputPdfFilePath = ofd.FileName.ToString();
                string temporaryFilePath = string.Empty;
                string pdfInputsDirectoryPath = System.IO.Path.GetDirectoryName(inputPdfFilePath);

                using (Stream inputPdfStream = new FileStream(inputPdfFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    PdfReader pdfReader = new PdfReader(inputPdfStream);


                    #region Create temporary file to rotate page without content rotation and shrink content
                    //this part of code is created because without saving rotated to Landscape orientatio pdf file and reading it again
                    //into pdf stream didn't find other solution which set content in Portrait orienation page into required horizontal
                    //position

                    string temporaryFileName = "Temporary_" + Path.GetFileNameWithoutExtension(inputPdfFilePath) +
                                Path.GetExtension(inputPdfFilePath);
                    temporaryFilePath = System.IO.Path.Combine(pdfInputsDirectoryPath, temporaryFileName);
                    using (Stream outputPdfStream =
                        new FileStream(temporaryFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        PdfStamper stamper = new PdfStamper(pdfReader, outputPdfStream);

                        int pagesAmount = pdfReader.NumberOfPages;

                        for (int pageNumber = 1; pageNumber <= pagesAmount; pageNumber++)
                        {
                            //This code rotates all page 
                            PdfDictionary page = pdfReader.GetPageN(pageNumber);
                            PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);

                            int rotation = rotate == null ? 270 : (rotate.IntValue + 270) % 360;
                            stamper.RotateContents = true;//this line leave content of page not rotated
                            page.Put(PdfName.ROTATE, new PdfNumber(rotation));
                            stamper.GetUnderContent(pageNumber).SetLiteral("\nq 0.53 0 0 0.53 10 25 cm\nq\n");
                            
                        }
                        stamper.Close();
                    }

                    #endregion Create temporary file to rotate page without content rotation and shrink content
                }

                #region Create new  pdfReader and load into it data from temporary file and then rotate pages to required position

                using (Stream inputPdfStream = new FileStream(temporaryFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    PdfReader pdfReader = new PdfReader(inputPdfStream);
                    File.Delete(temporaryFilePath);
                    string resultFileName = "Output_" + Path.GetFileNameWithoutExtension(inputPdfFilePath) +
                        Path.GetExtension(inputPdfFilePath);
                    string resultFilePath = System.IO.Path.Combine(pdfInputsDirectoryPath, resultFileName);
                    using (Stream outputPdfStream =
                        new FileStream(resultFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        PdfStamper stamper = new PdfStamper(pdfReader, outputPdfStream);
                        int pagesAmount = pdfReader.NumberOfPages;

                        for (int pageNumber = 1; pageNumber <= pagesAmount; pageNumber++)
                        {
                            //This code rotates all page 
                            PdfDictionary page = pdfReader.GetPageN(pageNumber);
                            PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);
                            stamper.RotateContents = true;//this line leave content of page not rotated

                            int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;
                            page.Put(PdfName.ROTATE, new PdfNumber(rotation));
                            //after the above code line execution the initial content of pdf document is in the correct position 

                        }
                        stamper.Close();
                    }
                }

                #endregion Create new  pdfReader and load into it data from temporary file and then rotate pages to required position

            }
        }
    }
}