【发布时间】: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旋转到纵向然后将结果保存到文件再次。