Adnan Bal
Adnan Bal

Reputation: 213

Itext table positioning

I'm developing an itext project with java. Basicly, in my project, i put 2 logos on every page dynamicly(one logo on the top of the each pages and one address image on the bottom of the pages). But, when i want to add a table on the page, if the table has so many rows, on the certain point, the rows of the table becomes on the addres image, but i want the rows wrap to another page, so the rows of the table and the address image do not mix.

Here's the picture what i want to explain:

http://img801.imageshack.us/img801/8074/tablejj.jpg

Thanks for your help.

Here's my main fonc.:

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {

                final denem2321 frame = new denem2321();
                frame.setVisible(true);

                // ///////////////

                frame.getMntmNewMenuItem_1().addActionListener(
                        new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent e) {
                                final Teklif frame1 = new Teklif(list
                                        .getSelectedItem(), contactName
                                        .get(list.getSelectedItem()),
                                        hashMapReferansNameAndUrl);

                                frame1.setVisible(true);
                                JOptionPane.showMessageDialog(
                                        frame,
                                        list.getSelectedItem()
                                                + " firması için bir teklif hazırlayınız");

                                String imageUrl = "/Users/nofear/workspace/deneme23/pics/a4-ust.png";
                                String imageUrlAlt = "pics/a4-alt.png";


                                try {
                                    imageust = Image
                                            .getInstance(imageUrl);
                                } catch (BadElementException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                } catch (MalformedURLException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                } catch (IOException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                }


                                try {
                                    imageAlt = Image
                                            .getInstance(imageUrlAlt);
                                } catch (BadElementException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                } catch (MalformedURLException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                } catch (IOException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                }


                                imageust.setAbsolutePosition(
                                        0f,
                                        document.getPageSize()
                                                .getHeight()
                                                - imageust
                                                        .getHeight()-15
                                                );
                                imageAlt.setAbsolutePosition(
                                        0f, 0);






                                System.out
                                        .println("HEIGTG"
                                                + (document
                                                        .getPageSize()
                                                        .getHeight() - imageust
                                                        .getHeight()));





                                frame1.getButtonAl().addActionListener(
                                        new ActionListener() {

                                            @Override
                                            public void actionPerformed(
                                                    ActionEvent arg0) {

                                                try {
                                                    final PdfWriter writer = PdfWriter
                                                            .getInstance(
                                                                    document,
                                                                    new FileOutputStream(
                                                                            "output.pdf"));
                                                    document.open();



                                                    final PdfContentByte cb = writer
                                                            .getDirectContent();

                                                    writer.setPageEvent(new PdfPageEvent() {

                                                        @Override
                                                        public void onStartPage(PdfWriter arg0, Document arg1) {



                                                        }

                                                        @Override
                                                        public void onSectionEnd(PdfWriter arg0, Document arg1, float arg2) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onSection(PdfWriter arg0, Document arg1, float arg2, int arg3,
                                                                Paragraph arg4) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onParagraphEnd(PdfWriter arg0, Document arg1, float arg2) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onParagraph(PdfWriter arg0, Document arg1, float arg2) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onOpenDocument(PdfWriter arg0, Document arg1) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onGenericTag(PdfWriter arg0, Document arg1, Rectangle arg2,
                                                                String arg3) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onEndPage(PdfWriter arg0, Document arg1) {

                                                            try {
                                                                cb.addImage(imageust);
                                                                cb.addImage(imageAlt);
                                                            } catch (DocumentException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                            }

                                                        }

                                                        @Override
                                                        public void onCloseDocument(PdfWriter arg0, Document arg1) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onChapterEnd(PdfWriter arg0, Document arg1, float arg2) {
                                                            // TODO Auto-generated method stub

                                                        }

                                                        @Override
                                                        public void onChapter(PdfWriter arg0, Document arg1, float arg2,
                                                                Paragraph arg3) {
                                                            // TODO Auto-generated method stub

                                                        }
                                                    });




                                                    // now draw a line below
                                                    // the headline
                                                    cb.setLineWidth(1f);


                                                    cb.stroke();

                                                    // ////////////////////////////////////

                                                    try {
                                                        bfArial = BaseFont
                                                                .createFont(
                                                                        "C:/Windows/Fonts/arial.ttf",
                                                                        "Cp1254",
                                                                        BaseFont.NOT_EMBEDDED);
                                                    } catch (DocumentException e1) {
                                                        // TODO
                                                        // Auto-generated
                                                        // catch block
                                                        e1.printStackTrace();
                                                    } catch (IOException e1) {
                                                        // TODO
                                                        // Auto-generated
                                                        // catch block
                                                        e1.printStackTrace();
                                                    }



                                                    Font baslikFont = new Font(
                                                            bfArial, 24,
                                                            Font.BOLD);

                                                    Font pdfBaslikFont = new Font(
                                                            bfArial, 16,
                                                            Font.NORMAL);

                                                    Font pdfTarihFont = new Font(
                                                            bfArial, 12,
                                                            Font.NORMAL);

                                                    Font altBaslik = new Font(
                                                            bfArial, 12,
                                                            Font.BOLD);

                                                    Font normalWritingFont = new Font(
                                                            bfArial, 12,
                                                            Font.NORMAL);



                                                    final ColumnText ct = new ColumnText(
                                                            cb);


                                                    Calendar currentDate = Calendar
                                                            .getInstance();
                                                    SimpleDateFormat formatter = new SimpleDateFormat(
                                                            "yyyy/MMM/dd");
                                                    String dateNow = formatter
                                                            .format(currentDate
                                                                    .getTime());


                                                    Phrase firmaBaslik = new Phrase(frame1.getTextField_teklifHazirlananFirmaAdi().getText(),
                                                            baslikFont);

                                                    Phrase pdfAltBaslik = new Phrase("WEB SİTESİ TASARIM ve PROGRAMLAMA ANLAŞMASI",pdfBaslikFont);

                                                    Phrase tarih = new Phrase(dateNow, pdfTarihFont);

                                                    Phrase alanAdlari = new Phrase("Sitenin Yayınlanacağı Alan Adları: ",altBaslik);
                                                    Phrase alanAdlarIcerik = new Phrase(frame1.getTextArea_alanAdlari().getText(),normalWritingFont);

                                                    Phrase webHosting = new Phrase("\n\nWebhosting & Yayınlama: ",altBaslik);
                                                    Phrase weHostingIcerik = new Phrase(frame1.getTextArea_webHosting().getText(),normalWritingFont);

                                                    Phrase siteDil = new Phrase("\n\nSitenin Dili: ",altBaslik);
                                                    Phrase siteDilIcerik = new Phrase(frame1.getTextArea_dil().getText(),normalWritingFont);

                                                    Phrase amac = new Phrase("\n\nAmaç: ",altBaslik);
                                                    Phrase amacIcerik = new Phrase(frame1.getTextArea_amac().getText(),normalWritingFont);

                                                    Phrase kullilanProgs = new Phrase("\n\nKullanılacak Programlar: ",altBaslik);
                                                    Phrase kullanilanProgsIcerik = new Phrase(frame1.getTextArea_kullanilacakProgramlar().getText(),normalWritingFont);

                                                    ct.setSimpleColumn(
                                                            firmaBaslik,
                                                            60,
                                                            750, /* width */
                                                            document.getPageSize()
                                                                    .getWidth() - 40,
                                                            30,
                                                            25,
                                                            Element.ALIGN_CENTER);
                                                    ct.go();

                                                    ct.setSimpleColumn(
                                                            pdfAltBaslik,
                                                            60,
                                                            650, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            25,
                                                            Element.ALIGN_CENTER);
                                                    ct.go();

                                                    ct.setSimpleColumn(
                                                            tarih,
                                                            60,
                                                            625, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            25,
                                                            Element.ALIGN_CENTER);
                                                    ct.go();



                                                    ct.setSimpleColumn(
                                                            alanAdlari ,
                                                            60,
                                                            550, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            25,
                                                            Element.ALIGN_LEFT);
                                                    ct.setSimpleColumn(
                                                            alanAdlarIcerik ,
                                                            60,
                                                            550, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            webHosting ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            25,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            weHostingIcerik ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            siteDil ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            siteDilIcerik ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            amac ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            amacIcerik ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            kullilanProgs ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);

                                                    ct.setSimpleColumn(
                                                            kullanilanProgsIcerik ,
                                                            60,
                                                            525, /* width */
                                                            document.getPageSize()
                                                            .getWidth() - 40,
                                                            30,
                                                            13,
                                                            Element.ALIGN_LEFT);


                                                    ct.go();


                                                    PdfPTable table = new PdfPTable(
                                                            2)          ;                                           
                                                    table.setTotalWidth(document
                                                            .getPageSize()
                                                            .getWidth() - 5);
                                                    table.getDefaultCell()
                                                            .setPadding(2);
                                                    table.getDefaultCell()
                                                            .setBorderWidth(1);
                                                    table.getDefaultCell()
                                                            .setHorizontalAlignment(
                                                                    Element.ALIGN_CENTER);

                                                    float[] columnWidths = {
                                                            10f, 2f, };

                                                    try {
                                                        table.setWidths(columnWidths);
                                                    } catch (DocumentException e) {
                                                        // TODO Auto-generated
                                                        // catch block
                                                        e.printStackTrace();
                                                    }
                                                    // table.setSpacingBefore(5f);

                                                    // Code 2
                                                    table.getDefaultCell()
                                                            .setGrayFill(0.9f);

                                                    table.addCell("WEB TASARIM VE PROGRAMLAMA");

                                                    table.addCell("TUTAR");
                                                    table.getDefaultCell()
                                                            .setBackgroundColor(
                                                                    new BaseColor(
                                                                            255,
                                                                            255,
                                                                            255));




                                                    document.add(table);
                                                    document.newPage();
                                                    document.add(new Paragraph("Yeni Sayfa"));





                                                } catch (Exception e) {
                                                } finally {
                                                    document.close();

                                                }

                                            }
                                        });


                            }
                        });





            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
}

Upvotes: 0

Views: 1914

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

This is a non-question. iText breaks tables automatically when they don't fit the current page, and forwards the remaining part to the next page.

You're adding headers and footers using page events, which is OK, but:

  1. you should define the margings of the Document object so that they take into account the height of the images you're adding as header / footer.
  2. you're not really adding much in your table object, but I see you using ColumnText with absolute positions that don't seem to take into account any margins.

In any case: your code is not well-written. You should clean it up. For instance: the way you define the page event is madness. You should use PdfPageEventHelper. I'm pretty sure nobody else but me will take a closer look at your code in its current state. Clean it up!

Upvotes: 2

Related Questions