2009-08-21 89 views

回答

11

這是解決方案。感謝Dylan McClung。

PdfWriter writer = ...; 
PdfContentByte cb = writer.getDirectContent(); 
cb.saveState(); 
cb.setColorStroke(Color.black); 
cb.rectangle(x,y,x1,y1); 
cb.stroke(); 
cb.restoreState(); 
+0

什麼CB立場? – sdespolit 2011-12-01 12:13:07

+0

ContentByte,iirc ... – canon 2011-12-05 17:06:06

+1

'PdfContentByte cb = writer.getDirectContent();' – 2016-02-01 13:15:30

3

在.NET版本中,我只是創建一個帶有邊框的表格。我知道這不是Java,但可能下面的代碼會幫助你。

iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20); 
PdfPTable table; 
PdfPCell cell; 

// single element w/ border 
table = new PdfPTable(1); 
cell = new PdfPCell(new Phrase("BOLD WORDS", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 11, Font.BOLD))); 
cell.BorderWidth = 2; 
cell.Padding = 5; 
cell.PaddingTop = 3; 
cell.HorizontalAlignment = Element.ALIGN_CENTER; 
table.AddCell(cell); 
table.SetWidthPercentage(new float[1] { 598f }, PageSize.LETTER); 
table.HorizontalAlignment = Element.ALIGN_CENTER; 
document.Add(table); 
+0

的API只是畫一個表格單元格的想法並不壞 – Chris623 2013-12-06 16:24:55

1
public static void drawRectangle(PdfContentByte content, float width, float height) { 
    content.saveState(); 
    PdfGState state = new PdfGState(); 
    state.setFillOpacity(0.6f); 
    content.setGState(state); 
    content.setRGBColorFill(0xFF, 0xFF, 0xFF); 
    content.setLineWidth(3); 
    content.rectangle(0, 0, width, height); 
    content.fillStroke(); 
    content.restoreState(); 
} 

從iText的

0
private static void rect(PdfWriter writer) { 

    PdfContentByte cb = writer.getDirectContent(); 
      try { 
       cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24); 
       cb.rectangle(140f,140f,280f,420f); 
       cb.stroke(); 
      } catch (DocumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
}