【问题标题】:iText(Sharp) - PushbuttonField inside Template inside Table CelliTextSharp) - 表格单元格内模板内的按钮字段
【发布时间】:2017-06-12 17:20:57
【问题描述】:

我正在 iTextSharp 生成的 PDF 文件中绘制一个方程。没什么复杂的,比如:

x-y
--- + 3
 z

但有两个有趣的警告:

  1. 在表格单元格中绘制了方程以及一些文本
  2. 表格是在结构化文档(章节和节)中绘制的,所以我不知道单元格将在哪里绘制。

我通过在模板中绘制方程来解决这个问题:

section.Add(new Paragraph("Here comes my equation in a table"));

PdfPTable outerTable = new PdfPTable(2);
outerTable.AddCell(new PdfPCell(new Phrase(new Chunk("Look at the next cell"))));

PdfTemplate template = pdfWriter.DirectContent.CreateTemplate(bounds.Width, bounds.Height);
draw_my_equation_into(template);
Image image = iTextSharp.text.Image.GetInstance(template);

PdfPTable innerTable = new PdfPTable(1);   // Inner table, inside cell (1, 0) in my outer table.
innerTable.AddCell(new PdfPCell(new Phrase(new Chunk("Here is my equation:"))));
innerTable.AddCell(new PdfPCell(image));

outerTable.AddCell(innerTable);

section.Add(outerTable);
chapter.Add(section);

效果很好。好东西。

但现在我意识到我想帮助我的文档的读者理解方程式,如果他们想要的话,所以我希望在他们将鼠标悬停在方程式上时出现一些文本,并让他们跳转到如果他们单击方程式,则可以进行更多解释。我知道的唯一方法是在块 as seen here 上使用 PushbuttonField 和通用标签。

但我不知道在这种情况下如何添加 PushbuttonField,我没有可以调用 SetGenericTag() 的 Chunk,而且我不知道页面上等式的位置。

创建 PdfTemplate 是不是走错路了?我是否需要使用其他方法创建我的方程,以便我可以获取一个块并调用 SetGenericTag()?或者有什么方法可以找出方程式的位置,以便我可以创建我的 PushbuttonField?

===== 稍后添加

我刚刚意识到我可以稍后移动模板的绘图,我可以在 OnCloseDocument() 处理程序期间进行绘图,我假设此时它已经完成了所有布局,因此它确切知道模板的绘制位置。但是不知道怎么问模板是在哪里画的……

【问题讨论】:

    标签: pdf itext


    【解决方案1】:

    我最终得到了一个相当复杂但实用的解决方案,基于将 CellEvent 添加到包含方程的单元格。它假定包含方程式的单元格包含方程式。这不是一个糟糕的假设,尽管我想要一个更通用的解决方案。

        class EquationDrawer : IPdfPCellEvent
        {
            IEquation m_equate;
            PdfTemplate m_template;
    
            public EquationDrawer(IEquation equate, PdfTemplate template)
            {
                m_equate = equate;
                m_template = template;
            }
            void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
            {
                // Draw the equation into the template
                m_equate.Draw(m_template);
    
                // Calculate the actual position on the page of where
                // the equation will be drawn by taking the cell rectangle
                // and insetting by the cell padding
                Rectangle bounds = m_equate.Bounds;
                Rectangle draw = new Rectangle(position.Left + cell.PaddingLeft,
                    position.Bottom + cell.PaddingBottom,
                    position.Left + cell.PaddingLeft + bounds.Width,
                    position.Top - cell.PaddingTop);
    
                // Calculate where, in the overall equation, is the text
                // that I want to add a button to
                Rectangle text = m_equate.WhereIsMyText(draw);
    
                CreatePushbutton(m_template.PdfWriter, text, m_tooltip, m_dest);
            }
    
            void CreatePushbutton(PdfWriter writer, Rectangle rect, string tooltip, string localDestination)
            {
                PdfContentByte cb = writer.DirectContentUnder;
                cb.SaveState();
                cb.SetColorStroke(BaseColor.BLUE);
                cb.SetLineWidth(0.5); 
                cb.SetLineDash(3, 2);
                cb.MoveTo(rect.Left, rect.Bottom - dashOffsetY);
                cb.LineTo(rect.Right, rect.Bottom - dashOffsetY);
                cb.Stroke();
                cb.RestoreState();
    
                PushbuttonField button = new PushbuttonField(writer, rect, "DefineButton-" + (++NumButtons));
                PdfFormField field = button.Field;
                field.Action = PdfAction.GotoLocalPage(localDestination, false);
                field.UserName = tooltip;
                writer.AddAnnotation(field);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 2016-01-01
      • 2023-03-27
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      相关资源
      最近更新 更多