Itext表定位

时间:2012-11-26 14:10:22

标签: pdf itext

我正在使用java开发一个itext项目。基本上,在我的项目中,我动态地在每个页面上放置2个徽标(每个页面顶部有一个徽标,页面底部有一个地址图像)。但是,当我想在页面上添加一个表时,如果表有这么多行,那么在某个点上,表的行就会出现在addres图像上,但是我希望这些行换行到另一个页面,所以行表格和地址图片不混合。

以下是我想要解释的图片:

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

感谢您的帮助。

这是我的主要内容:

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();
            }

        }
    });
}

1 个答案:

答案 0 :(得分:2)

这是一个非问题。当iText不适合当前页面时会自动中断表格,并将剩余部分转发到下一页。

您正在使用页面事件添加页眉和页脚,这没关系,但是:

  1. 您应该定义Document对象的边距,以便它们考虑您要添加为页眉/页脚的图像的高度。
  2. 您在table对象中添加的内容并不多,但我发现您使用的ColumnText绝对位置似乎没有考虑任何边距。
  3. 无论如何:你的代码编写得不好。你应该清理它。例如:您定义页面事件的方式是疯狂的。您应该使用PdfPageEventHelper。我很确定除了我之外没有其他人会仔细查看当前状态下的代码。清理它!