在旋转PdfPCell在iTextSharp的创建本地连接 [英] Create local link in rotated PdfPCell in iTextSharp

查看:357
本文介绍了在旋转PdfPCell在iTextSharp的创建本地连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个链接到另一个网页使用iTextSharp的我的PDF。在旋转细胞
链接无法正常工作。其他细胞如预期:

I'm trying to put a link to another page in my pdf using iTextSharp. link in rotated cell is not working. other cells work as expected:

FileStream fs = new FileStream("TestPDF.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();

PdfPTable linkTable = new PdfPTable(2);
PdfPCell linkCell = new PdfPCell();

linkCell.HorizontalAlignment = Element.ALIGN_CENTER;
linkCell.Rotation = 90;
linkCell.FixedHeight = 70;

Anchor linkAnchor = new Anchor("Click here");
linkAnchor.Reference = "#target";
Paragraph linkPara = new Paragraph();
linkPara.Add(linkAnchor);
linkCell.AddElement(linkPara);
linkTable.AddCell(linkCell);

PdfPCell linkCell2 = new PdfPCell();
Anchor linkAnchor2 = new Anchor("Click here 2");
linkAnchor2.Reference = "#target";
Paragraph linkPara2 = new Paragraph();
linkPara2.Add(linkAnchor2);
linkCell2.AddElement(linkPara2);
linkTable.AddCell(linkCell2);

linkTable.AddCell(new PdfPCell(new Phrase("cell 3")));
linkTable.AddCell(new PdfPCell(new Phrase("cell 4")));
doc.Add(linkTable);

doc.NewPage();

Anchor destAnchor = new Anchor("top");
destAnchor.Name = "target";
PdfPTable destTable = new PdfPTable(1);
PdfPCell destCell = new PdfPCell();
Paragraph destPara = new Paragraph();
destPara.Add(destAnchor);
destCell.AddElement(destPara);
destTable.AddCell(destCell);
destTable.AddCell(new PdfPCell(new Phrase("cell 2")));
destTable.AddCell(new PdfPCell(new Phrase("cell 3")));
destTable.AddCell(new PdfPCell(new Phrase("cell 4")));
doc.Add(destTable);

doc.Close();



我使用'iTextSharp的5.5.8。我试着Chunk.SetAction PdfAction.GotoLocalPage和Chunk.SetLocalGoto。没有什么对我的作品

I'm using 'iTextSharp 5.5.8'. I've tried with Chunk.SetAction PdfAction.GotoLocalPage and Chunk.SetLocalGoto. Nothing works for me

感谢您。

推荐答案

其实iText的(夏普)做创建的链接标注在旋转的单元格中的锚,太多,但它的坐标是完全错误的:

Actually iText(Sharp) did create a Link annotation for the anchor in the rotated cell, too, but its coordinates are completely wrong:

/Rect[-0.003 0 53.34 12]

这些坐标,甚至是部分摘页面这或许可以解释一些PDF浏览器所特有的行为。

These coordinates even are partially off-page which might explain peculiar behavior of some PDF viewers.

(我分析了iText的Java代码它有同样的问题,因为我在家里使用Java我更该匹配iTextSharp的C#代码非常相似,虽然。)

(I analyzed the iText Java code which has the same issues, because I'm more at home with Java. The matching iTextSharp C# code is very similar, though.)

这样做的原因是 PdfDocument 代码处理 PdfChunk 假定当前的坐标系是原来的用户空间协调与初始化系统媒体框数据。因此,它采用了当前坐标没有任何转变,产生局部的链接注释:

The cause for this is that the PdfDocument code processing a PdfChunk assumes that the current coordinate system is the original user space coordinate system initialized with the MediaBox data. Thus, it uses the current coordinates without any transformation to generate local Link annotations:

float xMarker = text.getXTLM();
float baseXMarker = xMarker;
float yMarker = text.getYTLM();
...
if (chunk.isAttribute(Chunk.LOCALGOTO)) {
    float subtract = lastBaseFactor;
    if (nextChunk != null && nextChunk.isAttribute(Chunk.LOCALGOTO))
        subtract = 0;
    if (nextChunk == null)
        subtract += hangingCorrection;
    localGoto((String)chunk.getAttribute(Chunk.LOCALGOTO), xMarker, yMarker, xMarker + width - subtract, yMarker + fontSize);
}

(PdfDocument.writeLineToContent(PdfLine,PdfContentByte,PdfContentByte,对象[ ],浮点))

不幸的是,虽然,细胞旋转由用户的装置坐标系统改变来实现,例如

Unfortunately, though, cell rotation is implemented by means of a user coordinate system change, e.g. a rotation by 90°:

ct.setSimpleColumn(-0.003f, -0.001f, netWidth + 0.003f, calcHeight);
...
pivotY = cell.getTop() + yPos - currentMaxHeight + cell.getEffectivePaddingBottom();
switch (cell.getVerticalAlignment()) {
    case Element.ALIGN_BOTTOM:
        pivotX = cell.getLeft() + xPos + cell.getWidth() - cell.getEffectivePaddingRight();
        break;
    case Element.ALIGN_MIDDLE:
        pivotX = cell.getLeft() + xPos + (cell.getWidth() + cell.getEffectivePaddingLeft() - cell.getEffectivePaddingRight() + calcHeight) / 2;
        break;
    default: //top
        pivotX = cell.getLeft() + xPos + cell.getEffectivePaddingLeft() + calcHeight;
        break;
}
saveAndRotateCanvases(canvases, 0, 1, -1, 0, pivotX, pivotY);

(PdfPRow.writeCells(INT,INT,浮球,浮球,PdfContentByte [],布尔))

因此, PdfDocument 上面代码中呼吁建立一个旋转的锚块将在定位通常完全错误的位置。

Thus, the PdfDocument code above called for a rotated anchor chunk will position at a generally completely wrong position.

顺便说一句,这不仅涉及当地的链接注释,但是各种的块产生的注释。一个特别邪恶的情况是一个通用的标签:如果一个页面事件监听器反应一个 GenericTag 事件,就可以呼叫,但没有在使用一段绘制操作的坐标坐标相对于媒体框

By the way, this does not only concern local Link annotations but all kinds of annotations generated for a chunk. A particularly evil case is that of a generic tag: if a page event listener reacts to a GenericTag event, it can use the coordinates for some drawing action during the call but not as coordinates relative to the MediaBox.

一个这个最有可能需要信令任何坐标系统修复更改 PdfDocument 和更新代码中有使用坐标不受这些转换影响的施行而采取这一信息考虑在内。尤其是 GenericTag 事件应扩大到同时接收变换和原始坐标。

A fix for this most likely requires signaling any coordinate system changes to the PdfDocument and updating the code there to take this information into account when using coordinates for purposes not influenced by those transformations. In particular the GenericTag event should be extended to receive both the transformed and the original coordinates.

我建议离开此修复程序iText的发展。

I would propose leaving this fix to iText development.

这篇关于在旋转PdfPCell在iTextSharp的创建本地连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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