【问题标题】:iText - How to rotate pdf content without rotating a pageiText - 如何在不旋转页面的情况下旋转 pdf 内容
【发布时间】:2021-02-10 18:53:14
【问题描述】:

我有 pdf 文件,其中有很多具有纵向方向的帖子标签页面。我需要缩小每个页面的现有内容,然后在每个页面中添加额外的信息。出于这个原因,我使用 PDFStamper 类,它在下面的代码中成功地缩小了内容。那么接下来的要求就是在不旋转页面的情况下将页面内容旋转 90 度或 270 度。页面必须保持纵向,但缩小的内容必须旋转。我尝试使用 PdfDictionary 类并测试了很多代码变体,但内容没有按要求旋转。我需要建议在代码中注释后必须包含哪些代码块 - //代码仅将 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旋转到纵向然后将结果保存到文件再次。

标签: c# itext


【解决方案1】:

下面的代码是我的解决方案。在此解决方案中使用了临时文件,其中保存了缩小并旋转为横向 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

            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多