Bottomborder for a subtable cell and for the gener

2019-09-17 21:19发布

问题:

I have used one parent table that is table_body. One child table that is sub_table(as i would like to place this sub_table inside a PdfPCell in the parent table. ). I want to draw a line when a row is complete for the parent table as per my requirement. But, the issue is bottomborder for the subtable and for the general pdfPcell 'body_cell_bottomBorder' is not getting aligned in a single straight line.

PdfPTable table_body = new PdfPTable(2); //  main table
table_body.getDefaultCell().setBorder(Rectangle.NO_BORDER); // set border to none
table_body.setWidthPercentage(100.0f);
table_body.setWidths(new float[] {3.0f,1.0f,});
table_body.setSpacingBefore(6);

PdfPTable sub_table = new PdfPTable(1); // sub table
sub_table.getDefaultCell().setBorder(Rectangle.NO_BORDER); // set border to none
body_cell_bottomBorder.setPhrase(new Phrase("Example",font_body)); // this cell has the bottom border only
Image image = Image.getInstance(BarCode.createBarcode("example"));
body_cell = new PdfPCell(image, true);  // this cell has no border at all
body_cell.setBorder(Rectangle.NO_BORDER); 
sub_table.addCell(body_cell); // added one row in the sub table
sub_table.addCell(body_cell_bottomBorder); // added second row in the sub table and also want a bottom border
table_body.addCell(sub_table);   // added subtable into the parent table pdfpcell

body_cell_bottomBorder.setPhrase(new Phrase(RPL,font_body)); // now adding second column value in parent table pdfPcell and want a bottom border only
table_body.addCell(body_cell_bottomBorder); // added to the parent table

Problem is these two cells of parent table do not make a complete single straight line which i want.

回答1:

One way:

  • In the 'child' PdfPTable as each PdfPCell is added, explicitly set Rectangle.NO_BORDER.
  • When you're done with the 'child' table, wrap in a PdfPCell and set border(s), then add to 'parent' PdfPTable.

Example in C#, since this question is tagged w/itextsharp:

string text = "0000";
int topBottomBorder = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;    
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open(); 
  PdfPTable parentTable = new PdfPTable(2);
  PdfPCell cell = new PdfPCell() {
    Phrase = new Phrase(text), Border = topBottomBorder
  };
  parentTable.AddCell(cell);

// in 'child' PdfPTable *ALL* PdfPCells set *WITH NO* border
  PdfPTable childTable = new PdfPTable(1);
  childTable.AddCell(new PdfPCell
    (
      (new BarcodeQRCode(text, 1, 1, null)).GetImage()
    ) 
    { Border = Rectangle.NO_BORDER, PaddingTop = 1 }
  );
  childTable.AddCell(new PdfPCell() { // row 2
    Border = Rectangle.NO_BORDER, Phrase = new Phrase(text)
  });

// 1. wrap childTable in PdfPCell and set top & bottom borders
  PdfPCell childTableCell = new PdfPCell(childTable) {
    Border = topBottomBorder
  };
// 2. add to main PdfPTable           
  parentTable.AddCell(childTableCell);
  document.Add(parentTable);
}

Result: