我对纸张尺寸的页面方向有疑问.
我有一个包含纵向和横向页面的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);
Run Code Online (Sandbox Code Playgroud)
因为我使用绝对值来设置图像位置.
img.SetAbsolutePosition(250,300);
Run Code Online (Sandbox Code Playgroud)
如果页面是横向或纵向,T如何设置图像位置?
注意:一个包含横向和纵向页面方向的pdf.
我有可能使用if语句吗?
if (//paper is landscape)
{
//code here
}
else
{
//code here
}
Run Code Online (Sandbox Code Playgroud)
}
你想实现什么目标?
通常,iText 会考虑页面旋转的值。这意味着当页面旋转时,坐标也会旋转。
如果你想推翻这一点,你可以添加这一行:
stamper.RotateContents = false;
Run Code Online (Sandbox Code Playgroud)
我的书的第 6 章对此进行了解释。你可以尝试这个例子来看看区别:
当然,这假设为页面定义了旋转。有时,通过定义不同的页面大小而不是定义旋转来模仿横向。
在这种情况下,您还应该阅读第 6 章,因为它解释了如何获取文档的 MediaBox。请参阅示例PageInformationGetPageSize() ,其中介绍了、GetRotation()和 等方法GetPageSizeWithRotation()。
这些都已记录在案,但如果它不能回答您的问题,请澄清。如示例所示,添加新内容时默认会考虑旋转,所以也许我误解了这个问题。