2011-04-19 39 views
0

任何機構知道爲什麼我的頁眉或頁邊距不能在頁面上工作/生成?它只生成與段落說「你好脖子」的pdf在Java文件中的pdf文件添加標題或邊距?

import java.awt.Desktop; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 
import java.io.*; 

public class Report { 



    public static void main(String arg[])throws Exception 
    { 


     try{ 
      File temp = File.createTempFile("tempfile", ".pdf"); 


       OutputStream file = new FileOutputStream(temp); 
       Document document = new Document(); 

       PdfWriter.getInstance(document, file); 
       document.open(); 
       document.addHeader("header1", "this is my header file"); 
       document.setMargins(50, 50, 100, 100); 
       document.add(new Paragraph("hello neck")); 
       document.close(); 
       file.close(); 

       if (Desktop.isDesktopSupported()) { 
        Desktop dtop = Desktop.getDesktop(); 

        if (dtop.isSupported(Desktop.Action.OPEN)) { 
         String temp2 = temp.getPath();  
         dtop.open(new File(temp2)); 
        } 
       } 


      } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 
} 

回答

1

錯誤類型的標題。這是爲元信息,而不是頁眉頁腳&頁腳。

認爲「內容類型」而不是「y的頁面x」。從Meta

//these two lines of code are identical 
document.addHeader("a", "b"); 
document.add(new Header("a", "b")); 

Header繼承,它處理作者/標題的/ etc /等等。標題適用於不屬於標準值之一的任意字符串。

此外,您只能在調用document.open()之前更改元數據。之後,任何更改都會被忽略(或者他們會拋出......我不記得)

但是您需要頁眉和頁腳。傳統的處理方式是通過PdfPageEvent的OnEndPage函數。如果從PdfPageEventHelper繼承,它已經刪除了PdfPageEvent接口中的所有功能,因此您只需覆蓋所需的一個。便利。

在您的OnEndPage中,您將希望使用ColumnText對象將文本寫入所提供的PdfContentByte中。