2009-06-12 8 views
25

Ich schreibe ein Programm, das eine pdf- oder rtf-Datei mit einer Tabelle erzeugt, mit iText. Ich habe die iText-Klasse-Tabelle und -Zelle verwendet, anstatt die spezifischere RtfTable oder pdfTable, so dass beide Dateien am Ende generiert werden können. Ich musste den Zellenabstand auf einen Wert von -1 setzen, sonst war zu viel Platz zwischen jeder Datenzeile auf dem Druckbogen. Allerdings versuche ich jetzt, Grenzen hinzuzufügen (speziell in der PDF-Datei), und die Zellen sind nicht mit dem Text ausgerichtet. Der untere Rand jeder Zelle schneidet direkt durch den Text. Es umgibt den Text nur, wenn der Zellenabstand auf 2 oder höher eingestellt ist. Unten ist ein Beispiel von dem, was ich tue: Gibt esiText Zellgrenzen schneiden durch Text

Document document = new Document(); 
    Paragraph paragraph = new Paragraph(); 
    Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL); 
    try{ 
    PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf")); 
    document.open(); 

    Table table = new Table(3); 
    table.setPadding(-1); 
    table.setWidth(90); 
    Cell cell1 = new Cell(); 
    cell1.setBorder(Rectangle.BOX); 
    cell1.setVerticalAlignment(ElementTags.ALIGN_TOP); 
    table.setDefaultCell(cell1); 
    paragraph = new Paragraph("header", iTextFont); 
    Cell cell = new Cell(paragraph); 
    cell.setHeader(true); 
    cell.setColspan(3); 
    table.addCell(cell); 
    paragraph = new Paragraph("example cell", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("one", iTextFont); 
      table.addCell(cell); 
    paragraph = new Paragraph("two", iTextFont); 
    cell = new Cell(paragraph); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Does this start a new row?", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Four", iTextFont); 
    table.addCell(paragraph); 
    paragraph = new Paragraph("Five", iTextFont); 
    table.addCell(paragraph); 
    document.add(table); 
    } catch (Exception e) { 
    //handle exception 
    } 
    document.close(); 

    } 

eine Möglichkeit, dieses Problem zu lösen, wobei entweder die gesamte Grenze nach unten einen Tropfen zu bewegen (ohne die Textplatzierung zu beeinflussen), oder die Räume, um loszuwerden, zwischen jede Zeile (der Abstand scheint nur ein Problem oberhalb des Textes zu sein, nicht unten), ohne den Zellenabstand auf -1 zu setzen?

+0

Haben Sie eine Lösung gefunden? Ich habe die gleichen Probleme. Ich möchte, dass meine Zellen weniger hoch sind, was durch das Auffüllen verursacht wird, aber wenn ich die Auffüllung reduziere, schneidet der Text den unteren Rand ab. –

+2

Nevermind, ich bin zu PdfPTable gewechselt und es ist jetzt in Ordnung. Da ich nur PDFs erzeuge, ist diese Lösung ausreichend für mich. –

+0

Bitte fügen Sie die importierten Bibliotheken in Ihrem Java ein, ich habe versucht, Sie zu implementieren Beispiel "vollständige Klasse zur Verfügung stellen" – shareef

Antwort

0

sollten Sie PdfPTable verwenden es viele Verfahren hat, die nützlich ist und gut verpackt Komponente ich diese Antwort geschrieben, so jemand das gleiche Problem Gesichter wissen, wo starten es nicht die typische Antwort auf die Frage sein könnte, aber hier kommt es ...

Organizing content in tables
The PDF output

import java.io.FileOutputStream; 
import java.io.IOException; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 

public class Spacing { 

    /** The resulting PDF file. */ 
    public static final String RESULT = "results/part1/chapter04/spacing.pdf"; 

    /** 
    * Main method. 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public static void main(String[] args) 
     throws DocumentException, IOException { 
     // step 1 
     Document document = new Document(); 
     // step 2 
     PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 
     // step 3 
     document.open(); 
     // step 4 
     PdfPTable table = new PdfPTable(2); 
     table.setWidthPercentage(100); 
     Phrase p = new Phrase(
      "Dr. iText or: How I Learned to Stop Worrying " + 
      "and Love the Portable Document Format."); 
     PdfPCell cell = new PdfPCell(p); 
     table.addCell("default leading/spacing"); 
     table.addCell(cell); 
     table.addCell("absolute leading: 20"); 
     cell.setLeading(20f, 0f); 
     table.addCell(cell); 
     table.addCell("absolute leading: 3; relative leading: 1.2"); 
     cell.setLeading(3f, 1.2f); 
     table.addCell(cell); 
     table.addCell("absolute leading: 0; relative leading: 1.2"); 
     cell.setLeading(0f, 1.2f); 
     table.addCell(cell); 
     table.addCell("no leading at all"); 
     cell.setLeading(0f, 0f); 
     table.addCell(cell); 
     cell = new PdfPCell(new Phrase(
      "Dr. iText or: How I Learned to Stop Worrying and Love PDF")); 
     table.addCell("padding 10"); 
     cell.setPadding(10); 
     table.addCell(cell); 
     table.addCell("padding 0"); 
     cell.setPadding(0); 
     table.addCell(cell); 
     table.addCell("different padding for left, right, top and bottom"); 
     cell.setPaddingLeft(20); 
     cell.setPaddingRight(50); 
     cell.setPaddingTop(0); 
     cell.setPaddingBottom(5); 
     table.addCell(cell); 
     p = new Phrase("iText in Action Second Edition"); 
     table.getDefaultCell().setPadding(2); 
     table.getDefaultCell().setUseAscender(false); 
     table.getDefaultCell().setUseDescender(false); 
     table.addCell("padding 2; no ascender, no descender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(true); 
     table.getDefaultCell().setUseDescender(false); 
     table.addCell("padding 2; ascender, no descender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(false); 
     table.getDefaultCell().setUseDescender(true); 
     table.addCell("padding 2; descender, no ascender"); 
     table.addCell(p); 
     table.getDefaultCell().setUseAscender(true); 
     table.getDefaultCell().setUseDescender(true); 
     table.addCell("padding 2; ascender and descender"); 
     cell.setPadding(2); 
     cell.setUseAscender(true); 
     cell.setUseDescender(true); 
     table.addCell(p); 
     document.add(table); 
     // step 5 
     document.close(); 
    } 
} 
1

eine Klasse oder gemeinsame Methoden schreiben Sie Ihre Tabelle erstellen - egal ob Sie mit Tabelle oder P dfPTable.

Diese Methoden werden Standardausrichtung, Messung basierend auf Oberlängen/Unterlängen usw. für Sie behandeln. Sie bieten auch einen allgemeinen Ort, um einen "3pt leeren Absatz" oder was auch immer andere Standardformatierungen, die Sie benötigen können.

OO-Software soll nicht dazu dienen, sich wiederholende und möglicherweise inkonsistente Codeabschnitte zu überdecken.

Hoffe, das hilft.