使用iTextSharp设置AcroField位置 [英] Setting AcroField Position using iTextSharp
问题描述
iTextSharp AcroFields
类有一个 GetFieldPosition
方法。
我试图以编程方式修改字段的位置。我想知道为什么没有相应的 SetFieldPosition
,还是有另一种方法来改变字段的大小或位置?
I am attempting to modify a field's position programmatically. I'm wondering why there is no corresponding SetFieldPosition
, or is there another way of changing a field's size or position?
推荐答案
查看此,部分原因。实际上没有单一的 GetFieldPosition
,而是复数 GetFieldPositions
因为技术上可以有多个字段一样的名字。所以理论上 SetFieldPositions
需要考虑到这一点,否则它可能会删除一些字段。
See this for part of the reason. There isn't actually a singular GetFieldPosition
but instead a plural GetFieldPositions
since you can technically have multiple fields with the same name. So a theoretical SetFieldPositions
would need to take this into account or it might erase some fields.
另一个原因是一个领域的定义领域并不总是那么简单。拿这个非常简单的代码:
The other reason is that a field's defining areas aren't always that simple. Take this very simple bit of code:
var tf = new TextField(writer, new iTextSharp.text.Rectangle(100, 400, 500, 500), "name");
writer.AddAnnotation(tf.GetTextField());
它会创建一个带有角落的 400x100
文本字段在 100,400
和 500,500
。它还创建一个外观条目 / AP
,边界框为 0,0
, 400,100
在点击字段之前给出您看到的正常外观区域。如果你真的只是通过相应地移动两个角来改变位置你就可以了但是因为这个位置实际上只是一个矩形
所以没有什么可以阻止你让它更宽或更高。在这种情况下,这个 BBox
条目也需要更新。这是一个简单的案例,iText可能会通过它。但是,复选框通常至少有两种需要更改的外观状态。还有其他情况可能没有意义传播这些变化。
It creates a 400x100
text field with corners at 100,400
and 500,500
. It also creates an appearance entry /AP
with a bounding box of 0,0
and 400,100
giving the normal appearance area that you see before clicking into the field. If you literally just changed the "position" by moving the two corners accordingly you'd be okay but since the position is really just a Rectangle
there's nothing stopping you from making it wider or taller. In that case this BBox
entry would need to get updated, too. This is a simple case and iText could probably work through it. Check boxes, however, usually have at least two appearance states that need to be altered. There could also be other cases that it might not make sense to propagate these changes through.
但是,如果你想重新定位一个字段,它仍然可以完成,但是你的负担是明智的。想象一下只有这个单一文本字段的简单PDF:
If you want to reposition a field, however, it can still be done, but the burden is on you to do it sanely. Imagine a simple PDF with just this single text field:
var tf = new TextField(writer, new iTextSharp.text.Rectangle(100, 400, 500, 500), "name");
tf.Text = "Hi";
tf.BorderColor = BaseColor.BLACK;
tf.BorderWidth = 1;
writer.AddAnnotation(tf.GetTextField());
您可以使用 PdfStamper $ c移动文本字段$ c>通过获取实际字段并调整其
RECT
数组。下面的代码。
You can "move" the text field with a PdfStamper
by getting the actual field and adjusting its RECT
array. The code below.
using (var fs = new FileStream(testFile, FileMode.Create,FileAccess.Write,FileShare.None)) {
using (var reader = new PdfReader(bytes)) {
using (var stamper = new PdfStamper(reader, fs)) {
//Get our name field
var nameField = reader.AcroFields.GetFieldItem("name");
//Grab the first widget inside of it (there could be more)
var w = nameField.GetWidget(0);
//Grab the bounding array
var r = w.GetAsArray(PdfName.RECT);
//Check both of the Y values
r[1] = new PdfNumber(r.GetAsNumber(1).IntValue - 300);
r[3] = new PdfNumber(r.GetAsNumber(3).IntValue - 300);
}
}
}
这篇关于使用iTextSharp设置AcroField位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!