2012-11-26 56 views
0

我正在開發一個java的itext項目。基本上,在我的項目中,我動態地在每個頁面上放置了2個徽標(每個頁面頂部有一個徽標,頁面底部有一個地址圖像)。但是,當我想在頁面上添加一個表格時,如果表格有這麼多行,在某些點上,表格的行變成了地址圖像,但是我希望這些行換行到另一個頁面,所以行表格和地址圖像不混合。圖文表定位

這裏就是我想解釋圖片:

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

感謝您的幫助。

這裏是我的主fonc:

公共靜態無效的主要(字串[] 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(); 
      } 

     } 
    }); 
} 

回答

2

這是一個非問題。當iText不適合當前頁面時,它會自動分割表格,並將剩餘部分轉發到下一頁面。

您要添加使用頁面事件,這是OK的頁眉和頁腳,但:

  1. 你應該讓他們考慮到你的圖像的高度定義文檔對象的margings添加爲頁眉/頁腳。
  2. 你實際上並沒有在你的table對象中增加很多,但我看到你使用的是帶有絕對位置的ColumnText,這似乎沒有考慮到任何邊距。

無論如何:你的代碼寫得不好。你應該清理它。例如:你定義頁面事件的方式是瘋狂的。您應該使用PdfPageEventHelper。我非常確定沒有其他人,但我會仔細看看當前狀態下的代碼。清理!