Text in Tabellenzelle mit OpenPDF in Java zentrieren/ausrichtenJava

Java-Forum
Anonymous
 Text in Tabellenzelle mit OpenPDF in Java zentrieren/ausrichten

Post by Anonymous »

Ich verwende OpenPDF 1.3.20 mit Java und möchte die Ausrichtung eines Textes/Absatzes in einer Tabellenzelle ändern.
Egal, was ich bisher versucht habe, hat die Positionierung von Text überall verändert.
Ich habe nur erfahren, dass ein als table.addCell("sometext") hinzugefügter Text ihn in der Mitte ausrichten würde.
Da ich komplexere Inhalte hinzufügen möchte, reicht das nicht aus und ich brauche eine gute Kontrolle über die Positionierung.
Dies ist die Testklasse, die ich bisher verwendet habe. Wie kann ich die Ausrichtung einer bestimmten Zelle ändern?

Code: Select all

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class test {
public static void main(String[] args){
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld.pdf"));

// step 3: we open the document
document.open();
// step 4: we add a table to the document

Font whiteFont = new
Font(Font.HELVETICA, 18, Font.NORMAL, new Color(255, 255, 255));
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
Color blue = new Color(0, 0, 255);

PdfPCell cell = new PdfPCell();
cell.setBorderWidth(0);
cell.setBackgroundColor(blue);
// here i try to change the alignment of text in the cell
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Paragraph p = new Paragraph("test1", whiteFont);
p.setAlignment(Element.ALIGN_MIDDLE);
cell.addElement(p);
table.addCell(cell);

cell = new PdfPCell();
cell.setBorderWidth(0);
cell.setBackgroundColor(blue);
cell.addElement(new Paragraph("test2", whiteFont));
table.addCell(cell);

document.add(table);
document.close();

} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.close();
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post