Frage

Ich bin mit der Apache PDFBox Java-Bibliothek PDF-Dateien zu erstellen. Gibt es eine Möglichkeit, eine Datentabelle mit PDFBox zu schaffen? Wenn es keine solche API ist, es zu tun, würde ich verlangen, manuell den Tisch usw. mit drawLine zu ziehen, Vorschläge, wie um dies zu realisieren?

War es hilfreich?

Lösung

Quelle : Erstellen von Tabellen mit PDFBox

Das folgende Verfahren zeichnet eine Tabelle mit dem angegebenen Tabelleninhalt. Es ist ein bisschen wie ein Hack und wird für kleine Textzeichenfolgen arbeiten. Dabei spielt es keine Zeilenumbruch durchführen, aber Sie können eine Vorstellung davon, wie es gemacht wird. Probieren Sie es aus!

/**
 * @param page
 * @param contentStream
 * @param y the y-coordinate of the first row
 * @param margin the padding on left and right of table
 * @param content a 2d array containing the table data
 * @throws IOException
 */
public static void drawTable(PDPage page, PDPageContentStream contentStream, 
                            float y, float margin, 
                            String[][] content) throws IOException {
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = 20f;
    final float tableWidth = page.findMediaBox().getWidth() - margin - margin;
    final float tableHeight = rowHeight * rows;
    final float colWidth = tableWidth/(float)cols;
    final float cellMargin=5f;

    //draw the rows
    float nexty = y ;
    for (int i = 0; i <= rows; i++) {
        contentStream.drawLine(margin, nexty, margin+tableWidth, nexty);
        nexty-= rowHeight;
    }

    //draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.drawLine(nextx, y, nextx, y-tableHeight);
        nextx += colWidth;
    }

    //now add the text        
    contentStream.setFont( PDType1Font.HELVETICA_BOLD , 12 );        

    float textx = margin+cellMargin;
    float texty = y-15;        
    for(int i = 0; i < content.length; i++){
        for(int j = 0 ; j < content[i].length; j++){
            String text = content[i][j];
            contentStream.beginText();
            contentStream.moveTextPositionByAmount(textx,texty);
            contentStream.drawString(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty-=rowHeight;
        textx = margin+cellMargin;
    }
}

Verbrauch:

PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage( page );

PDPageContentStream contentStream = new PDPageContentStream(doc, page);

String[][] content = {{"a","b", "1"}, 
                      {"c","d", "2"}, 
                      {"e","f", "3"}, 
                      {"g","h", "4"}, 
                      {"i","j", "5"}} ;

drawTable(page, contentStream, 700, 100, content);
contentStream.close();
doc.save("test.pdf" );

Andere Tipps

Ich habe eine kleine api für die Erstellung von Tabellen mit PDFBox. Es kann auf Github zu finden ( https://github.com/dhorions/boxable ).

Eine Probe eines erzeugten pdf finden Sie hier http://goo.gl/a7QvRM .

Für Hinweise oder Anregungen sind willkommen.

Die akzeptierte Antwort ist schön, aber es funktioniert mit Apache PDFBox 1.x nur für Apache PDFBox 2.x Sie müssen ein wenig modifizieren, um die Code-Bit macht es richtig.

So, hier ist der gleiche Code, aber das ist kompatibel mit Apache PDFBox 2.x :

Das Verfahren drawTable:

public static void drawTable(PDPage page, PDPageContentStream contentStream,
    float y, float margin, String[][] content) throws IOException {
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = 20.0f;
    final float tableWidth = page.getMediaBox().getWidth() - 2.0f * margin;
    final float tableHeight = rowHeight * (float) rows;
    final float colWidth = tableWidth / (float) cols;

    //draw the rows
    float nexty = y ;
    for (int i = 0; i <= rows; i++) {
        contentStream.moveTo(margin, nexty);
        contentStream.lineTo(margin + tableWidth, nexty);
        contentStream.stroke();
        nexty-= rowHeight;
    }

    //draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.moveTo(nextx, y);
        contentStream.lineTo(nextx, y - tableHeight);
        contentStream.stroke();
        nextx += colWidth;
    }

    //now add the text
    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12.0f);

    final float cellMargin = 5.0f;
    float textx = margin + cellMargin;
    float texty = y - 15.0f;
    for (final String[] aContent : content) {
        for (String text : aContent) {
            contentStream.beginText();
            contentStream.newLineAtOffset(textx, texty);
            contentStream.showText(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty -= rowHeight;
        textx = margin + cellMargin;
    }
}

Die Verwendung aktualisiert, um die versuchen-with-Ressourcen Anweisung, um die Ressourcen zu schließen richtig:

try (PDDocument doc = new PDDocument()) {
    PDPage page = new PDPage();
    doc.addPage(page);

    try (PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
        String[][] content = {{"a", "b", "1"},
            {"c", "d", "2"},
            {"e", "f", "3"},
            {"g", "h", "4"},
            {"i", "j", "5"}};
        drawTable(page, contentStream, 700.0f, 100.0f, content);
    }
    doc.save("test.pdf");
}

Da ich hatte das gleiche Problem vor einiger Zeit habe ich eine kleine Bibliothek für ihn zu bauen begonnen, die ich auch auf dem Laufenden zu halten versuche.

Es nutzt Apache PDFBox 2.x und kann hier gefunden werden: https://github.com/vandeseer/easytable

Es ermöglicht ganz einige Anpassungen wie die Schriftart einstellen, Hintergrundfarbe, Polsterung usw. auf der Zellebene, vertikale und horizontale Ausrichtung, Zelle spanning, Zeilenumbruch und Bilder in den Zellen.

Zeichnung Tabellen über mehrere Seiten ist möglich.

Sie können diese Tabellen erstellen, wie zum Beispiel:

 image description hier

eingeben

Der Code für dieses Beispiel finden Sie hier - andere Beispiele im selben Ordner wie gut.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top