2016-09-21 17 views
0

iText的PDF版本7的問題,我使用的iText PDF生成包含應當設置如下表PDF文件:與C#

------------------------------------------------ 
|     HEADER      | 
------------------------------------------------ 
| some data goes here | more data here   | 
------------------------------------------------ 
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 | 
------------------------------------------------ 
| 1 | SDF  wer qwerwq | weqr | WERQW | 
------------------------------------------------ 
|  |      |  |  | 
|  |      |  |  | 
|  |      |  |  | 
|  |      |  |  | 
------------------------------------------------ 
| footer information       | 
------------------------------------------------ 

但是表被繪製如下:

我試過下面的例子,但它們都是用Java編寫的,而C#的對象模型看起來略有不同。其具有爲1的「西1」值的行下面各行跨列2,3分裂,和4

注意要點:

  1. 對於頭單元格我設置的水平取向通過調用cell.SetHorizo​​ntalAlignment(Horizo​​ntalAlignment.CENTER)
  2. 我需要設置的一些文字的顏色爲紅色
  3. 我使用table.AddCell方法增加該小區
  4. 我設置的邊界(根據文檔,這是默認的單元格)Bord er.NO_BORDER。
  5. 這是用C#編寫
  6. 我已經下載了最新版本的iText的(7.0.1版本)
  7. 我創建了一個自定義的CellRender但似乎沒有任何效果的web應用程序。
  8. 最初我使用的是iText 5,但我需要更好地控制表格的渲染,因爲我需要知道頁面到底有多遠。
  9. 這是我用來創建單元代碼:

    PdfFont cellFont = font; 
    
        if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD && (fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) 
        { 
         cellFont = fontBoldItalic; 
        } 
        else if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD) 
        { 
         cellFont = fontBold; 
        } 
        else if ((fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) 
        { 
         cellFont = fontItalic; 
        } 
    
        Color fontColor = Color.BLACK; 
        if ((fontStyle & FONT_STYLE_RED) == FONT_STYLE_RED) 
        { 
         fontColor = Color.RED; 
        } 
    
        Text text = new Text(content); 
        text.SetFont(cellFont); 
        text.SetFontColor(fontColor); 
        text.SetFontSize(fontSize); 
    
        if ((fontStyle & FONT_STYLE_UNDERLINE) == FONT_STYLE_UNDERLINE) 
        { 
         text.SetUnderline(); 
        } 
    
        Cell cell = new Cell(rowspan, colspan); 
        cell.Add(new Paragraph(text)); 
        //cell.SetNextRenderer(new CellBorders(cell, borders)); 
    
        return cell; 
    

這是怎樣的表創建和表添加到該文件在Web方法的末尾:

 Table table = new Table(6); 
     table.SetWidthPercent(100); 
     table.SetPadding(3); 
     table.SetSpacingRatio(1); 
     table.SetBorder(Border.NO_BORDER); 
+1

對於頭單元格,有你嘗試設置文本或段落'TextAlignment的水平對齊。 CENTER'('SetHorizo​​ntalAlignment'方法)? –

+1

至於邊框,您可以在單元格本身指定邊框屬性。 ('小區#SetBorder/SetBorderLeft/...')。 –

+1

在第一個註釋中出現小錯誤,使用的方法是SetTextAlignment()' –

回答

2

您這裏有兩個問題:

  1. 文本未正確對齊(標題) 使用SetTextAlignment()方法設置單元格中的文本對齊。 SetHorizontalAlignment設置包裝文本的容器的對齊方式。

  2. 邊框不按預期顯示。 首先,在表格中定義邊框並沒有在iText 7中定義默認的單元格行爲,就像它在iText 5中一樣。由於默認的tableRenderer不使用border屬性,所以除非您定義了自定義渲染器,否則table#SetBorder(Border.NO_BORDER)將不起作用,並且利用你自己的財產。

正確的方式來定義自定義邊框是做對單個細胞的水平。在這裏,你需要記住的是,由於邊界重疊,則需要將所有的重疊邊框NO_BORDER,如果你不希望邊框顯示:會導致細胞之間沒有邊界

for(int j = 0; j<3; j++){ 
     Cell dCell = new Cell(); 
     //When setting borders to NO_BORDER, remember to set that border to NO_BORDER in all adjoining cells 
     dCell.SetBorderLeft(Border.NO_BORDER); 
     dCell.SetBorderRight(Border.NO_BORDER); 
     dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,j))); 
     //Dashed, striped, grooved and more can be specified 
     if(i != 4) dCell.SetBorderBottom(new DashedBorder(1f)); 
     tab.AddCell(dCell); 
    } 

以上片段條目。

for(int k = 4; k<6;k++){ 
     Cell d2Cell = new Cell(); 
     //Only the right-most border will not show, since the left-most borders of the neighbouring cells still get drawn 
     d2Cell.SetBorderRight(Border.NO_BORDER); 
     d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,k)).SetFontColor(Color.RED)); 
     //Specific borders apart from NO_BORDER do override the default 
     if(i!=4) d2Cell.SetBorderBottom(new DashedBorder(1f)); 
     tab.AddCell(d2Cell); 
    } 

上面的片段只在最右邊的單元格的右邊沒有邊框。

下面是構造一個表,如一個在問題的自包含的代碼片段:

public void CreateTable(string dest) 
    { 
     PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); 
     Document doc = new Document(pdfDoc); 

     Table tab = new Table(6); 
     //Table should take up the entire width 
     tab.SetWidthPercent(100); 
     tab.SetPadding(3); 
     //Because table takes up the entire width, this has no visual effect, but otherwise it will center the table 
     tab.SetHorizontalAlignment(HorizontalAlignment.CENTER); 


     //Header cell 
     Cell hCell = new Cell(1, 6); 
     hCell.Add(new Paragraph("Centered Header")); 
     //Text is aligned by calling SetTextAlignment 
     hCell.SetTextAlignment(TextAlignment.CENTER); 

     tab.AddCell(hCell); 
     //Subheaders 
     Cell shCellL = new Cell(1, 3); 
     shCellL.Add(new Paragraph("Left aligned data")); 
     shCellL.SetTextAlignment(TextAlignment.LEFT); 

     Cell shCellR = new Cell(1, 3); 
     shCellR.Add(new Paragraph("Right aligned data")); 
     shCellR.SetTextAlignment(TextAlignment.RIGHT); 

     tab.AddCell(shCellL); 
     tab.AddCell(shCellR); 
     //col names 
     for (int i = 0; i < 6; i++) 
     { 
      Cell colName = new Cell(); 
      colName.Add(new Paragraph(String.Format("Col {0}", i))); 
      colName.SetTextAlignment(TextAlignment.CENTER); 
      tab.AddCell(colName); 
     } 
     //data cols 
     for (int i = 1; i < 5; i++) 
     { 
      Cell nC = new Cell(); 
      nC.Add(new Paragraph("" + i)); 
      tab.AddCell(nC); 
      for (int j = 0; j < 3; j++) 
      { 
       Cell dCell = new Cell(); 
       //When Setting borders to NO_BORDER, remember to Set that border to NO_BORDER in all adjoining cells 
       dCell.SetBorderLeft(Border.NO_BORDER); 
       dCell.SetBorderRight(Border.NO_BORDER); 
       dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, j))); 
       //Dashed, striped, grooved and more can be specified 
       if (i != 4) dCell.SetBorderBottom(new DashedBorder(1f)); 
       tab.AddCell(dCell); 
      } 
      for (int k = 4; k < 6; k++) 
      { 
       Cell d2Cell = new Cell(); 
       //Only the rightmost border will not show, since the left-most borders of the neighbouring cells still get drawn 
       d2Cell.SetBorderRight(Border.NO_BORDER); 
       d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, k)).SetFontColor(Color.RED)); 
       //Specific borders apart from NO_BORDER do override the default 
       if (i != 4) d2Cell.SetBorderBottom(new DashedBorder(1f)); 
       tab.AddCell(d2Cell); 
      } 
     } 

     //footer cell 
     Cell fCell = new Cell(1, 6); 
     fCell.Add(new Paragraph("footer")); 
     tab.AddCell(fCell); 

     //Add table to document 
     doc.Add(new Paragraph("Complex Table example")); 
     doc.Add(tab); 
     doc.Close(); 
    }