.net - 如何在绝对位置插入 iTextSharp.text.Rectangle 矩形?

标签 .net itext

使用下面的代码,插入的矩形位于页面的左下角。怎么可以iTextSharp.text.Rectangle矩形插入到绝对位置?谢谢。

iTextSharp.text.Rectangle rectangle = new Rectangle(10, 10, 100, 15, 0);  
// (lower-left-x, lower-left-y, upper-right-x (llx + width), upper-right-y (lly + height), rotation angle 
rectangle.BorderColor = BaseColor.WHITE;
rectangle.BackgroundColor = BaseColor.YELLOW;
overContent.Rectangle(rectangle);
//stamper.Close();
PdfAnnotation annotation = PdfAnnotation.CreateLink(
                        stamper.Writer, rectangle, PdfAnnotation.HIGHLIGHT_INVERT,
                        new PdfAction("http://itextpdf.com/"));
stamper.AddAnnotation(annotation, 1);

最佳答案

OP 在评论中说

the Hyperlink rectangle is ALWAYS at the bottom-left of the generated PDF no matter how I change the x & y values in the Rectangle arguments (int x, int y, int width, int height).



这里错误变得明显,OP假定Rectangle的第三个和第四个参数构造函数为宽度和高度。情况并非如此,参见。 Rectangle 的文档化源代码构造函数:
/// <summary>
/// Constructs a Rectangle-object.
/// </summary>
/// <param name="llx">lower left x</param>
/// <param name="lly">lower left y</param>
/// <param name="urx">upper right x</param>
/// <param name="ury">upper right y</param>
public Rectangle(float llx, float lly, float urx, float ury)

...

/**
 * Constructs a <CODE>Rectangle</CODE>-object.
 *
 * @param llx   lower left x
 * @param lly   lower left y
 * @param urx   upper right x
 * @param ury   upper right y
 * @param rotation the rotation (0, 90, 180, or 270)
 * @since iText 5.0.6
 */
public Rectangle(float llx, float lly, float urx, float ury, int rotation)

和参见还有 OP 自己代码中的注释:
// (lower-left-x, lower-left-y, upper-right-x (llx + width), upper-right-y (lly + height), rotation angle 

第三个和第四个参数是第二个点的坐标。矩形位置和大小由 (llx, lly) 中的对角线决定至 (urx, ury) .

此外,尽管有变量名称和描述,iTextSharp 并不强制第一个点位于左下角,第二个点位于右上角;如果第二个在右下角或左下角,那就这样吧......

因此,由于 OP 仅更改了前两个参数,因此第二个点保持在 (100, 15)它通常非常靠近页面底部并且非常靠近页面左边框。

他应该尝试类似的事情:
iTextSharp.text.Rectangle rectangle = new Rectangle(510, 510, 600, 515, 0);  

关于.net - 如何在绝对位置插入 iTextSharp.text.Rectangle 矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38568690/

相关文章:

java - 如何将多个段落包装在一个元素中?

java - iText 7、htmlPDF 2 - DefaultFontProvider 的并行使用

java - 如何在 Java 中使用 JDBC 从 MySQL 检索数学方程式?

c# - System.NullReferenceException 未在第一个 Try/Catch block 中捕获

c# - 微软 Surface vs Windows Touch?

.net - .Net 中的语言互操作如何工作?

c# - 使用 iTextSharp 将 PDF 文件中使用的字体保存到文件

rotation - 在itext中使用中心旋转文本

c# - PCL 中的 TPL 和监视器

.net - 如何在 NUnit 2.5.6 中使用 : PartCover . NET 4