2017-06-12 57 views
0

我在iTextSharp生成的PDF文件中绘制方程。没有什么复杂的,这样的东西:iText(Sharp) - 按钮字段里面的模板里面的单元格

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,我没有一个Chunk来调用SetGenericTag(),并且我不知道方程在页面上的位置。

正在创建一个PdfTemplate错误的方式去这里?我是否需要使用其他方法创建我的公式,以便可以获得一个Chunk并调用SetGenericTag()?还是有一些方法来找出方程的位置,所以我可以创建我的按钮字段?

=====后来添加

我才意识到,我可以稍后移动模板的附图中,OnCloseDocument()处理过程中,我可以做图纸,我认为在这一点上它的完成所有的布局,因此它确切知道模板的绘制位置。但我不知道怎么问我结束了一个颇为曲折,但功能的解决方案的基础上,增加一个CellEvent到包含公式的单元格在那里它被绘制模板...

回答

0

。它假定包含公式的单元格包含只有的等式。这不是一个错误的假设,但我想要一个更一般的解决方案。

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); 
     } 
    }