5

我对纸张尺寸的页面方向有疑问。
我有一个包含纵向横向页面的 pdf 文件。

此代码完美运行。

string FileLocation = "c:\\Temp\\SomeFile.pdf";
string WatermarkLocation = "c:\\Temp\\watermark.gif";
Document document = new Document();
PdfReader pdfReader = new PdfReader(FileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create));

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(250,300); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)

PdfContentByte waterMark;
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
    waterMark = stamp.GetUnderContent(page);
    waterMark.AddImage(img);
}
stamp.FormFlattening = true;
stamp.Close();

// now delete the original file and rename the temp file to the original file
File.Delete(FileLocation);
File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);

因为我使用绝对值来设置图像位置。

img.SetAbsolutePosition(250,300);

如果页面是横向或纵向,如何设置图像位置?
注意:一个具有横向和纵向页面方向的 pdf。

有没有机会我可以使用 if 语句?

if (//paper is landscape)
{
    //code here
}
else 
{
    //code here
}
4

1 回答 1

0

你想达到什么目标?

通常,iText 会考虑页面旋转的值。这意味着当页面旋转时,坐标也会旋转。

如果你想推翻这一点,你可以添加这一行:

stamper.RotateContents = false;

这在我的书的第 6 章中进行了解释。你可以试试这个例子看看有什么不同:

  1. 无旋转,文字添加正常:hello1.pdf
  2. 旋转,正常添加的文本(=旋转):hello2.pdf
  3. 旋转,忽略旋转添加的文本:hello3.pdf

当然,这假定为页面定义了旋转。有时,通过定义不同的页面大小而不是定义旋转来模仿横向。

在这种情况下,您还应该阅读第 6 章,因为它解释了如何获取文档的 MediaBox。请参阅示例PageInformation,其中介绍了GetPageSize()GetRotation()和等方法GetPageSizeWithRotation()

这一切都记录在案,但如果它不能回答您的问题,请澄清。如示例中所示,添加新内容时默认考虑轮换,所以我可能误解了这个问题。

于 2013-04-30T09:45:22.573 回答