2016-11-18 70 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