Rajeev
Rajeev

Reputation: 120

Align content in PDF using itext in java

I am stuck up with this problem for sometime now. I'm drawing a pie chart to the document and displaying the jtable data. I am not able to align the contents in the pdf file. How do I align the jtable data as well as the pie chart to this pdf. I have mentioned my code below with an image. As you can see, I need to align my jtable data a little to the top, and also it has to fit the width of the page.

     private void graphical_to_pdf() {

    graph_table.setSize(graph_table.getPreferredSize());

    JTableHeader th = graph_table.getTableHeader();
    th.setSize(th.getPreferredSize());

    PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 800, 800, 0.65f);
    com.itextpdf.text.Font f = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 18.0f, com.itextpdf.text.Font.NORMAL, BaseColor.BLACK);

    Document doc = new Document(new com.itextpdf.text.Rectangle(900, 900));

    //Creating a paper to store the jtable contents
    Paper paper = new Paper();

    paper.setSize(800, 800);
    paper.setImageableArea(0, 100, 800, 800);

    PageFormat pf = new PageFormat();
    pf.setPaper(paper);
    Printable printable = graph_table.getPrintable(JTable.PrintMode.NORMAL, null, null);

    try {
        PdfWriter writer;
        writer = PdfWriter.getInstance(doc, new FileOutputStream(save_pdf.getSelectedFile().getAbsoluteFile() + ".pdf"));

        writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
        doc.open();
        int width = 375;
        int height = 300;

        JFreeChart chart = create_pie_chart_count();

        doc.add(new Paragraph("Some TExt", f));
        doc.add(new Paragraph("Some Text", f));
        doc.add(new Paragraph("Document Generated On - " + generatedDate, f));
        BufferedImage bufferedImage = chart.createBufferedImage(width, height);

        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        image.scalePercent(100f);
        image.setAlignment(image.MIDDLE);
        doc.add(image);
        PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, writer);
        writer.setOpenAction(action);

            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = img.createGraphics();

        int page = 0;

        int result = Printable.NO_SUCH_PAGE;

        PdfContentByte cb = writer.getDirectContent();
        do {
            result = printable.print(g, pf, page);
            if (result == Printable.PAGE_EXISTS) {
                cb.saveState();
                Graphics2D g2 = cb.createGraphics(650, 225);
                result = printable.print(g2, pf, page);
                g2.dispose();
                cb.restoreState();
                page++;
                doc.newPage();

            }

        } while (result == Printable.PAGE_EXISTS);
        g.dispose();
    } catch (Exception e) {
    } finally {
        doc.close();
    }
}

Upvotes: 0

Views: 1252

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Take a look at the answer to the following question: How to position a PDFGraphis2D object in iText?

As you can see, it is common not to draw objects such as the chart or the table straight to the direct content. Instead, you should create a PdfTemplate to draw to. One of the benefits of using a PdfTemplate is that you can wrap it inside an Image. Don't worry: if the PdfTemplate contains vector data, the Image will be a vector image; it won't be rasterized (which would lead to a loss of resolution).

Once you have Image objects, there are many different options to resize, position, organize them. You can get an idea of the different options when reading the answer to the following questions:

Upvotes: 1

Related Questions