2016-11-18 71 views
0

我创建一个PDF表格,我可以管理时张贴在这里表需要一个以上的页面会发生什么:手柄或拦截新的页面事件划清界限

iTextsharp - draw a line at the end and start of a page with tables

如果表格需要另一个页面,那么我在插入新页面之前在实际页面上绘制最后一行。

现在我需要在NEW页面上画一条顶部线条,但我不知道是哪个方法调用。我试图这样:

Imports iTextSharp.text.pdf 

Public Class LineaBottom 
    Implements IPdfPTableEvent 

    Public Sub TableLayout(table As PdfPTable, widths As Single()(), heights() As Single, headerRows As Integer, rowStart As Integer, canvases() As PdfContentByte) Implements IPdfPTableEvent.TableLayout 
     'Throw New NotImplementedException() 

     Dim columns As Integer 
     Dim footer As Integer = widths.Length - table.FooterRows 
     Dim header As Integer = table.HeaderRows - table.FooterRows + 1 
     Dim ultima As Integer = footer - 1 

     If last <> -1 Then 
      Dim line As PdfContentByte 
      line = pdfWrite.DirectContent 
      line.SetLineWidth(0.5) 
      line.MoveTo(xStart, curY) 
      line.LineTo(xEnd, curY) 
      line.Stroke() 

      'canvases(PdfPTable.BASECANVAS).Rectangle(rect) 
     End If 
    End Sub 
End Class 

其中xStart和xEnd是全局变量,左边距和右边距加上或减去一个值。

我不知道如何去适应行

canvases(PdfPTable.BASECANVAS).Rectangle(rect) 

,因为该行是从Java样本绘制一个矩形,我只需要一条线

和线

If last <> -1 Then 

检测到页面的最后一行,我需要检测新页面的第一行

回答

1

我不知道如果我正确地理解你的问题,而是:

  • 如果我需要 iTextSharp的移动到新页面之前添加表时会被分裂的东西,,我不会使用IPdfPTableEvent。相反,我会用一个IPdfPTableEventSplit
  • 如果我需要添加一些表格时 iTextSharp的移动到一个新的页面,并之后被拆分,添加表的其余部分之前,我不会用一个IPdfPTableEvent 。相反,我会用一个IPdfPTableEventAfterSplit

当创建一个事件,我会通过DocumentPdfWriter对象。通过询问当前页面尺寸的Document对象,我将获得我需要的坐标,并且我将使用PdfWriter来添加行。

但是,这不是你的问题的答案。你似乎知道如何绘制一个矩形,但你不知道如何绘制一条线。这很简单:

PdfContentByte cb = canvases(PdfPTable.BASECANVAS) 
cb.MoveTo(x1, y1) 
cb.LineTo(x2, y2) 
cb.Stroke() 

可以更改各种属性,如线宽,像这样:

PdfContentByte cb = canvases(PdfPTable.BASECANVAS) 
cb.SaveState() 
cb.SetLineDash(8, 4, 0) 
cb.SetLineWidth(2.0f) 
cb.MoveTo(x1, y1) 
cb.LineTo(x2, y2) 
cb.Stroke() 
cb.RestoreState() 

至于x1x2值,y1,和y2,你有根据通过参数widthsheights传递给您的值来定义它们。

+0

感谢您的支持。我靠近找到解决方案。如果我使用Dim cb As PdfContentByte,然后cb = canvases(PdfPTable。BASECANVAS)如果首先使用“Dim myCanvas As New Canvas()”,则会出现“画布未声明”的错误,并且在https://msdn.microsoft.com/zh-cn/library/ms745163(v = vs.110).aspx?cs-save-lang = 1&cs-lang = vb#code-snippet-1我是否需要导入或声明之前的某些内容? – fedeteka