2016-03-27 29 views
0

İlk defa iTextSharp() kullanmaya çalışıyorum ve pdf belgesinde bir tablo oluşturma sorunu yaşıyorum. Gerçekten, ilk satırı ve ilk sütunun başlığını yazmak için ilk hücreyi çapraz olarak bölmek istiyorum.Tablo hücrelerini çapraz olarak bölme

Fotoğraf 1 2 Ben

teşekkür ederiz yapmak istediğim şey ben Resmi yapabileceğini budur. Picture 1 Picture 2

cevap

1

Sen official documentation belgelenen gibi bir hücre olayı kullanarak bu özel hücre oluşturmak gerekir.

enter image description here

Bu hücre olay için sözde kodu:

sana C# dönüşebilen ve bu şuna benzer bir tablo yaratacak bazı sözde kodu vereceğiz

public void createPdf(String dest) throws IOException, DocumentException { 
    Document document = new Document(); 
    PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(6); 
    table.getDefaultCell().setMinimumHeight(30); 
    PdfPCell cell = new PdfPCell(); 
    cell.setCellEvent(new Diagonal("Gravity", "Occ")); 
    table.addCell(cell); 
    table.addCell("1"); 
    table.addCell("2"); 
    table.addCell("3"); 
    table.addCell("4"); 
    table.addCell("5"); 
    for (int i = 0; i < 5;) { 
     table.addCell(String.valueOf(++i)); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell(""); 
    } 
    document.add(table); 
    document.close(); 
} 
:
class Diagonal implements PdfPCellEvent { 
    protected String columns; 
    protected String rows; 

    public Diagonal(String columns, String rows) { 
     this.columns = columns; 
     this.rows = rows; 
    } 

    public void cellLayout(PdfPCell cell, Rectangle position, 
     PdfContentByte[] canvases) { 
     PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; 
     ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, 
      new Phrase(columns), position.getRight(2), position.getTop(12), 0); 
     ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, 
      new Phrase(rows), position.getLeft(2), position.getBottom(2), 0); 
     canvas = canvases[PdfPTable.LINECANVAS]; 
     canvas.moveTo(position.getLeft(), position.getTop()); 
     canvas.lineTo(position.getRight(), position.getBottom()); 
     canvas.stroke(); 
    } 
} 

Bu nasıl hücre olayını kullanmak gösterir sözde koddur

Şimdi bu sahte kodu (aslında Java koduyla çalışan) C# 'ye dönüştürmek size kalmıştır.

+0

Buna çok şükür, şimdi nasıl yapacağımı biliyorum! – user6054254

+0

Gelecekte bu eğitimi görecek kullanıcılara tavsiyem: http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables – user6054254

+1

Merhaba @ user6054254 Sorununuzu çözdüyse * yanıtı * kabul etmek . –

İlgili konular