閱讀33 返回首頁    go 阿裏雲 go 技術社區[雲棲]


ITEXT實例學習與研究(三) 之 頁邊距的設計 摘要

頁邊距:

記住一個值,72!

當創建一個文件時,你還可以定義上、下、左、右頁邊距:

Document document = new Document(PageSize.A5, 36, 72, 108,180);

在示例代碼0104中你可以看到該文檔有一個0.5英寸的左邊距和1英寸的右邊距,上邊距為1.5英寸,下邊距為2.5英寸。

說明:

當創建一個矩形或設置邊距時,你可能希望知道該用什麼度量單位:厘米、英寸或象素,事實上,默認的度量係統以排版單位磅為基礎得出其他單位的近似值,如1英寸=72磅,如果你想在A4頁麵的PDF中創建一個矩形,你需要計算以下數據:

21 厘米 / 2.54 = 8.2677 英寸

8.2677英寸* 72 = 595 磅

29.7 厘米 / 2.54 = 11.6929 英寸

11.6929英寸* 72 = 842 磅

默認邊距為36磅即半英寸。

如果你修改了頁麵尺寸,僅僅影響到下一頁,如果你修改了頁邊距,則影響到全部,故慎用。



import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Chap0104 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 4: Margins");
        
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A5, 36, 72, 108, 180);
        
        try {
            
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            
            PdfWriter.getInstance(document, new FileOutputStream("Chap0104.pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add a paragraph to the document
            Paragraph paragraph = new Paragraph();
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            for (int i = 0; i < 20; i++) {
                paragraph.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. ");
            }
            document.add(paragraph);
            
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        // step 5: we close the document
        document.close();
    }
}


摘要

在你寫入任何實際數據之前,你可能希望通過以下幾種方法寫入一些關於本文檔的摘要:

public boolean addTitle(String title)

public boolean addSubject(String subject)

public boolean addKeywords(String keywords)

public boolean addAuthor(String author)

public boolean addCreator(String creator)

public boolean addProducer()

public boolean addCreationDate()

public boolean addHeader(String name, Stringcontent)

你可以選擇自己的標題、主題、關鍵字、作者、創建程序,但以下產品信息將始終被添加:iTextSharp (或者iTextSharp的引用)和創建時間(實際上這兩種方法是自動調用的)。

你還可以將自定義的名稱添加為“報頭信息”,但是這對於PdfWriter沒有任何作用,如果看看實例代碼0101產生的pdf文件的“文檔屬性”,我們可以看到僅僅有PDF創建程序和產品日期,而示例代碼0106的“文檔屬性”框中有更多的信息。



打開document前要做的事:

你隻能在Open方法調用之前添加摘要,這是iText開發工具提供的一個選擇。

在HTML中,報頭信息被放在文檔前麵報頭標識中間,調用Open方法將導致報頭信息寫入流,因而在Document被打開後無法更改這些數據。

PDF報頭信息不包括摘要,看起來有類似於:

%PDF-1.2

該行顯示生成的文檔是一個版本為1.2的PDF格式的文件,在PDF中,摘要保存在PdfInfo對象中,當文檔關閉時已經寫入PdfWriter中了,因此,沒有關於為什麼不能修改庫來滿足任何時候添加或更改摘要的技術原因



import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

import com.lowagie.text.html.HtmlWriter;

public class Chap0106 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 6: Meta Information");
        
        // step 1: creation of a document-object
        Document document = new Document();
        
        try {
            
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            
            PdfWriter.getInstance(document, new FileOutputStream("Chap0106.pdf"));
            HtmlWriter.getInstance(document, System.out);
            
            // step 3: we add some metadata and open the document
            
            document.addTitle("Hello World example");
            document.addSubject("This example explains step 3 in Chapter 1");
            document.addKeywords("Metadata, iText, step 3, tutorial");
            document.addCreator("My program using iText");
            document.addAuthor("Bruno Lowagie");
            document.addHeader("Expires", "0");
            document.open();
            
            // step 4: we add a paragraph to the document
            document.add(new Paragraph("Hello World"));
            
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        // step 5: we close the document
        document.close();
    }
}








最後更新:2017-04-02 22:16:24

  上一篇:go 開發那點事係列二 - ClassLoader trouble shooting references
  下一篇:go ITEXT實例學習與研究(二) 之 創建一個細長的淺黃色背景的頁麵以及縱向頁麵與橫向頁麵之間的切換