使用iTextSharp获取页面的确切坐标以添加具有不同页面旋转的水印 [英] Get exact cordinates of the page to add a watermark with different page rotation using iTextSharp

查看:156
本文介绍了使用iTextSharp获取页面的确切坐标以添加具有不同页面旋转的水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用iTextSharp在PDF文件上放置文本水印,而我正在努力寻找每一页上的坐标.当pdf文件中的所有页面都具有相同的旋转度时,效果很好,但是如果旋转度不同,则坐标完全不同.

I have been trying to position a Text Water marks on PDF file using iTextSharp and i am struggling to find the coordinates on each page. it works fine when all the pages in pdf file are of same rotation but if the rotation is different then the coordinates are completely different.

PdfImportedPage page = stamper.GetImportedPage(pdfReader, i);
var rotationValue = page.Rotation;

并添加水印

cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This is WaterMark 1", 20, 20, 90f);

此代码无法在页面上找到X和Y位置. 如何获得要添加水印的确切位置?

this code is unable to find the X and Y position on the page. how can i get the exact location where i want to add the watermark?

推荐答案

此问题在以法语撰写的文章中得到了回答,该文章基于我用英语回答的几个StackOverflow问题:

This question is answered in an article written in French that was based on several StackOverflow questions I answered in English: Comment créer un filigrane transparent en PDF?

此博客文章所基于的问题是:

The questions this blog post was based on, are:

  • How to watermark PDFs using text or images? (This is an important one for you, because it deals with page rotations!)
  • How to add a watermark to a PDF file?
  • How to extend the page size of a PDF to add a watermark?

这些问题及其答案可以在 StackOverflow上最好的iText问题中找到,可以从iText网站下载的免费电子书.它还包含一些从未在StackOverflow上发布的答案.

These questions and their answers can be found in The Best iText Questions on StackOverflow, a free ebook that can be downloaded from the iText site. It also contains a couple of answers that were never published on StackOverflow.

您不应该导入页面来查找轮换.还有其他获取信息的方法.您会注意到,可以使用getPageSize()GetPageSizeWithRotation()方法,这取决于您是否希望随旋转获得页面大小(还有一个GetRotation()方法).

You shouldn't import the page to find out the rotation. There are other ways to get that information. You'll notice that you can use the getPageSize() and the GetPageSizeWithRotation() methods depending on whether or not you want to get the page size along with the rotation (there's also a GetRotation() method).

此外,您应该尝试使用RotateContents属性:

Furthermore, you should experiment with the RotateContents property:

stamper.RotateContents = false;

我不清楚您是否要水印跟随还是忽略旋转,但是GetPageSize()GetPageSizeWithRotation()方法可以避免使用硬编码值例如x = 20; y = 20(如代码段所示).如果需要页面i的中间坐标,可以使用以下代码:

It is not exactly clear to me whether or not you want the watermark to follow or ignore the rotation, but the GetPageSize() and the GetPageSizeWithRotation() method, you'll be able to avoid having to use hard-coded values such as x = 20; y = 20 (as done in your code snippet). If you want the middle coordinate of page i, you can use this code:

Rectangle pagesize = reader.GetPageSizeWithRotation(i);
x = (pagesize.Left + pagesize.Right) / 2;
y = (pagesize.Top + pagesize.Bottom) / 2;

这篇关于使用iTextSharp获取页面的确切坐标以添加具有不同页面旋转的水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆