2014-01-28 33 views
0

當從頭開始創建PDF,我試圖去適應這個代碼iText:如何爲頁腳旋轉的頁腳事件設置頁面事件?

http://itextpdf.com/examples/iia.php?id=104

特別圖示那裏的OnEndPage()函數,使用頁面事件來設置一個頁腳。問題出在我的應用程序中,有些頁面是Portrait和一些Landscape,我不知道如何在該函數中實現查詢來確定頁面旋轉。

首先,當所有的頁面都是肖像時,我才能使用它。然後我添加了一些橫向頁面,並嘗試修改它,如下所示。我本來以爲一個快速和骯髒的解決辦法是簡單地通過增加中央頁腳表,

table.setHorizontalAlignment(Element.ALIGN_CENTER); 

但這似乎沒有任何效果(在橫向頁面,該表總是左對齊上論文的長邊)。然後我試圖通過查詢頁面旋轉做一個更好的解決方案,並根據其結果,設置表格的列到正確的寬度,使用,

if (???==90) 
       table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5" 
      else 
       table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 6.5" 

,但我不知道如何查詢頁面旋轉(如你所知)。任何幫助,將不勝感激。我的代碼如下。

class HeaderFooter extends PdfPageEventHelper { 
/** The header text. */ 
String footerLeft, footerRight; 
/** The template with the total number of pages. */ 
PdfTemplate total; 
/** Flag indicating true for first page */ 
Boolean firstPageFlag=true; 
... 
public void onEndPage(PdfWriter writer, Document document) { 
    if (firstPageFlag==false) { 
     /** The footer font */ 
     FontFactory.register("/home/appFonts/Arial_Narrow.ttf", "arial_narrow"); 
     Font styleFooter = FontFactory.getFont("arial_narrow", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, K.TEXT_FOOTER_FONT_SIZE, Font.UNDEFINED, BaseColor.BLACK); 

     PdfPTable table = new PdfPTable(4); 
     try { 
      //table.setHorizontalAlignment(Element.ALIGN_CENTER); // doesn't seem to have an effect for landscape pages 
      if (how to query page rotation, or other method to evaluate whether page is landscape or portrait?) 
       table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5" 
      else 
       table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 8.5" 
      table.setLockedWidth(true); 
      table.getDefaultCell().setFixedHeight(14); 
      table.getDefaultCell().setBorder(Rectangle.TOP); 
      // col1, row1 
      table.addCell(new Phrase(footerLeft, styleFooter)); 
      table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); 
      // col2, row1 
      table.addCell(new Phrase(String.format("page %d of", writer.getPageNumber()),styleFooter)); 
      // col3, row1 
      PdfPCell cell = new PdfPCell(Image.getInstance(total)); 
      cell.setBorder(Rectangle.TOP); 
      table.addCell(cell); 
      // col4, row1 
      table.addCell(new Phrase(footerRight, styleFooter)); 
      table.writeSelectedRows(0,-1,document.left(),document.bottom()-0.35f*K.PPI,writer.getDirectContent()); 
     } catch (DocumentException de) { 
      throw new ExceptionConverter(de); 
     } 
    } else 
     firstPageFlag=false; 
} 
... 
} 
+2

您正在使用'PdfReader'(在處理*現有PDF *時使用)混合頁面事件(從頭創建PDF *時使用*)。這讓很多想要回答你的問題的人感到困惑(可能會讓你自己感到困惑)。請解釋您是否從頭開始創建PDF(在這種情況下,您不應該使用「PdfReader」)或基於現有的PDF(在這種情況下,您不應該使用頁面事件)。 –

+1

@ggkmath作爲Bruno評論的附錄:如果您想將頁眉和頁腳添加到*現有* PDF,您可能需要查看示例[TwoPasses.java]的第二遍(http://itextpdf.com /examples/iia.php?id=118)。你有一個'PdfReader'並且可以使用你的頁面旋轉測試。 – mkl

+0

感謝您的意見。這是從頭開始創建PDF(不修改現有的PDF)。我修改了原始帖子以刪除對PdfReader的引用。 – ggkmath

回答

2

總結在評論中發展的問題

由於@Bruno最初闡明正確的解決方案中,OP的原始嘗試是

混合頁面事件(創建時使用PDF文件)與PdfReader(用於處理現有的PDF)。

這本來是可能實際上被作爲樣品TwoPasses.java中使用,但優選的OP一個單步方法切換到一個兩通架構使用OP的PdfReader中心測試爲景觀。

對於這種單通道方法,原始HeaderFooter頁面事件偵聽器已擴展爲在頁面構建時也存儲頁面維度信息。這是通過覆蓋onStartPage()做檢索使用

Rectangle pSize=document.getPageSize(); 

和存儲在聽衆的成員變量及其相關信息的頁面大小。最終,現在在onEndPage()中使用此信息來確定如何設置表格列。

在OP的過程中,橫向頁面是通過旋轉創建的。因此,在他的案件的相關信息是

(pSize.getRotation()==90) 

是否true(橫向)或沒有(縱向)。通常整個Rectangle包括它的旋轉和它的尺寸值將不得不被存儲並且最終被用於確定期望的頁眉和頁腳位置。

+0

感謝@mkl! – ggkmath