Hey everybody,

I am currently working on a program that has to write nice PDFs, and I'm using the iText library to do this.

I am nearly done, except for some major layout problems.

1. iText automatically splits tables up if it reaches the end of the page, and continues on the next one. Neither in the methods nor in the documentation I could find a way to prevent this. I have lots of quite small tables, if everyone of them is split up the whole thing is unreadable.

2.

Table tab = new Table(2);
                Image logo = Image.getInstance("Y:\\wsp2.gif");
                logo.scalePercent(70);

                Cell logoCell = new Cell(logo);
                logoCell.setHorizontalAlignment("RIGHT"); //the picture is left anyway
                tab.addCell(logoCell);

                PdfPTable pdftab = new PdfPTable(2);
                tab.setConvert2pdfptable(true);
                pdftab = tab.createPdfPTable();

                logo.scalePercent(50);
                pdftab.addCell(logo); //this picture doesnt even show up.

                pdftab.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
                pdftab.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                        writer.getDirectContent());

I have marked the lines that basically got ignored, i have no idea why. I don't really get the difference between Table and PdfPTable anyway, why have 2 models?

Thank you very much in advance,
Aztek

From the provided snipped is difficult to determinate what is going on.
So here we go with general stuff from iText online tutorials

If this doesn't help you will need to provide full coding which is dealing with table to PDF and some sample data which cause trouble

Okay, forget my old example, in the meantime ive gone all PdfPTable because they offer much more functions:

import java.io.FileOutputStream;
import com.lowagie.text.*;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;



public class PDFWriter {

    public static Document doc = new Document();
    
    static PdfWriter writer;

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws DocumentException, IOException {

            writer = PdfWriter.getInstance(doc, new FileOutputStream("Y:\\asd.pdf"));
            
            doc.open();

            PdfPTable table = new PdfPTable(4);

            table.addCell("No");
            table.addCell("Alignment");
            table.addCell("here:");

            PdfPCell cell = new PdfPCell();
            cell.addElement(new Chunk("I WANT TO BE RIGHT"));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); //this line wont work

            table.addCell(cell);

            doc.add(table);
            doc.close();

    }
}

As you can see, i want the content of the last cell to have a right alignment. But if you run the code, all the cells will be aligned left (thats default).

It works in this example, I couldnt find any differences to my code though:
http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellAlignment.java

Id be glad if you guys could help me out.

Regards,
azket

Edit: Ah well, and then there is still question 1 of my first posting left. i dont think this needs an example.

Okay, I solved the first Problem myself:

if (table.getTotalHeight() > writer.getVerticalPosition(true) - doc.bottomMargin()) {
            doc.newPage();
        }
        doc.add(table);

This seems to work fine.

But please, does anyone have a solution for the problem in my second posting?
If I got this one, I'm finally done with the whole project.

Thanks guys!

PS: Sorry for double posting, I could not edit anymore.

I'm not exactly sure why (would have to spend more time reading documentation), but component alignment does not work on Chunk. You have to do cell alignment through Paragraph

import java.io.FileOutputStream;
import com.lowagie.text.*;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


public class PDFWriter {

    public static Document doc = new Document();

    static PdfWriter writer;

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws DocumentException, IOException {

            writer = PdfWriter.getInstance(doc, new FileOutputStream("daniweb.pdf"));

            doc.open();

            PdfPTable table = new PdfPTable(4);

            table.addCell("No");
            table.addCell("Alignment");
            table.addCell("here:");

            PdfPCell cell = new PdfPCell(new Paragraph("I WANT TO BE RIGHT"));
            /*               
            cell.addElement(new Chunk("I WANT TO BE RIGHT"));
             */
            cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); //this line wont work

            table.addCell(cell);

            doc.add(table);
            doc.close();

    }
}

Ok, thanks!

Looks like the PdfPCell is pretty bugged:

PdfPCell cell = new PdfPCell(new Paragraph("I WANT TO BE RIGHT"));    //alignment works fine.

PdfPCell cell2 = new PdfPCell();
cell2.addElement(new Paragraph("ME TOO"));   //alignment wont work

PdfPCell cell3 = new PdfPCell(new Chunk("ME THREE")); //wont even compile

Well, at least I've found a way now. Thanks a bunch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.