tanvir
tanvir

Reputation: 71

Pdf creation using java

hi i am using java to create a pdf file. I need to have a output like this:

t
h this is text 1
i this is text 2
s                                                                     
i
s
h
t
t

That means there is a text outsite the table which show in vertical form. I write a code like this:

PdfWriter.getInstance(document, new FileOutputStream("check.pdf"));
document.open();
Font cellFont = FontFactory.getFont(FontFactory.TIMES_BOLD, 6, Font.BOLD);
PdfPTable table = new PdfPTable(8);
table.getDefaultCell().setBorder(1);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cell = new PdfPCell(new Phrase("This is the text 1", cellFont));
cell.setColspan(8);
cell.setBorder(0);
cell.setHorizontalAlignment(3);
table.addCell(cell);
cell = new PdfPCell(new Phrase("This is the text 2", cellFont));
cell.setColspan(8);
cell.setBorder(0);
cell.setHorizontalAlignment(3);
table.addCell(cell);

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

This output like this:

this is text 1
this is text 2

Can any one tell me how to modify this code to get my desired output

Upvotes: 0

Views: 1385

Answers (1)

bchetty
bchetty

Reputation: 2241

From the limited information, I don't completely understand your requirements. Anyways, you can almost always find workarounds and achieve what you want. What you need is Rowspan, which is not a valid method (for valid reasons). This Rowspan can be achieved by "nesting" tables inside a table cell. I coded a fast example, as below:

String text1 = "This is the text 1";
String text2 = "This is the text 2";

Document document = new Document();

PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
Font cellFont = FontFactory.getFont(FontFactory.TIMES_BOLD, 14, Font.BOLD);

PdfPTable table = new PdfPTable(8);
table.getDefaultCell().setBorder(0);


PdfPTable nestedTable1 = new PdfPTable(1);
nestedTable1.getDefaultCell().setBorder(0);
nestedTable1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
nestedTable1.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);

for(int i=0;i<text1.length();i++) {
   PdfPCell nestedCell = new PdfPCell(new Phrase("" + text1.charAt(i), cellFont));
   nestedCell.setBorder(0);
   nestedTable1.addCell(nestedCell);
}

PdfPTable nestedTable2 = new PdfPTable(1);
nestedTable2.getDefaultCell().setBorder(0);
nestedTable2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
nestedTable2.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);

PdfPCell nestedCell1 = new PdfPCell(new Phrase(text1, cellFont));
nestedCell1.setBorder(0);
PdfPCell nestedCell2 = new PdfPCell(new Phrase(text2, cellFont));
nestedCell2.setBorder(0);

nestedTable2.addCell(nestedCell1);
nestedTable2.addCell(nestedCell2);

PdfPCell cell1 = new PdfPCell(nestedTable1);
cell1.setBorder(0);
cell1.setColspan(1);

PdfPCell cell2 = new PdfPCell(nestedTable2);
cell2.setBorder(0);
cell2.setColspan(7);

table.addCell(cell1);
table.addCell(cell2);

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

And the pdf output looks like this: enter image description here

Upvotes: 2

Related Questions