0

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

2
  • 3
    what information is the left hand side vertical text based on ? I don't see it within your code Commented Jan 3, 2012 at 10:03
  • in the left side of the table there will be a text ex: here "this is htt" I do not know how to add this text outside my table in this way Commented Jan 3, 2012 at 11:28

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply. I actually need output not like this. This is a sample of my requirement. I need this kind of output. So that in my main table i need to add this information at the middle of the pdf file. I already done the file but unable to do this. This is the sample output [link] imageshack.us/photo/my-images/339/screenshoot.png
tanvir, if you use the logic from my above code, you can create that report, as shown in your above link. If you have a lot of reports that you have to generate, you can consider reporting frameworks like Jasper Reports, where you can design the report in iReport report designer and call the Jasper report engine to generate reports.
@tanvir That is an awfully small image. Could you post a larger example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.