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


ITEXT實例學習與研究(二) 之 創建一個細長的淺黃色背景的頁麵以及縱向頁麵與橫向頁麵之間的切換

iTextSharp.text.Document-object共有三個構造函數:

public Document();

public Document(Rectangle pageSize);

public Document(Rectangle pageSize,

int marginLeft,

int marginRight,

int marginTop,

int marginBottom);

第一個構造函數以A4頁麵作為參數調用第二個構造函數,第二個構造函數以每邊36磅頁邊距為參數調用第三個構造函數

u       頁麵尺寸:

你可以通過指定的顏色和大小創建你自己的頁麵,下麵的示例代碼創建一個細長的淺黃色背景的頁麵:

Rectangle pageSize = new Rectangle(144, 720);

//pageSize.BackgroundColor = new Color(0xFF, 0xFF, 0xDE);

pageSize.setBorderColor(newBaseColor(0xFF, 0xFF, 0xDE));

Document document = new Document(pageSize);

通常,你不必創建這樣的頁麵,而可以從下麵頁麵尺寸中選擇:

A0-A10, LEGAL, LETTER, HALFLETTER,_11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA 和 FLSE


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

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

public class Chap0102 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 2: PageSize");
        
        // step 1: creation of a document-object
        Rectangle pageSize = new Rectangle(144, 720);
        pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
        Document document = new Document(pageSize);
        
        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("Chap0102.pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add some paragraphs to the document
            for (int i = 0; i < 5; i++) {
                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();
    }
}

大多數情況下使用縱向頁麵,如果希望使用橫向頁麵,你隻須使用rotate()函數:

Document document = new Document(PageSize.A4.rotate());

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

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

public class Chap0103 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 3: PageSize");
        
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4.rotate());
        
        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("Chap0103.pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add some phrases to the document
            for (int i = 0; i < 20; i++) {
                document.add(new Phrase("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. "));
            }
            
        }
        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 ITEXT實例學習與研究(三) 之 頁邊距的設計 摘要
  下一篇:go iOS開發中的對象係統基礎