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();
    }
}








目录
相关文章
|
6月前
|
Web App开发 JSON 前端开发
SAP UI5 进阶 - JSON 模型字段里的值,显示在最终 UI5 界面上的奥秘分析试读版
SAP UI5 进阶 - JSON 模型字段里的值,显示在最终 UI5 界面上的奥秘分析试读版
30 0
|
3月前
|
JavaScript 前端开发 定位技术
HTML新特性【账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置、添加覆盖物、自定义标注图标、添加文本标注】(四)-全面详解(学习总结---从入门到深化)(上)
HTML新特性【账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置、添加覆盖物、自定义标注图标、添加文本标注】(四)-全面详解(学习总结---从入门到深化)
33 0
|
3月前
|
JSON 定位技术 数据格式
HTML新特性【账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置、添加覆盖物、自定义标注图标、添加文本标注】(四)-全面详解(学习总结---从入门到深化)(下)
HTML新特性【账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置、添加覆盖物、自定义标注图标、添加文本标注】(四)-全面详解(学习总结---从入门到深化)
31 0
|
3月前
|
JSON 定位技术 API
HTML新特性【规划公交路线、规划步行路线、定位、自定义视角动画、账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置】(五)-全面详解(学习总结---从入门到深化)(下)
HTML新特性【规划公交路线、规划步行路线、定位、自定义视角动画、账号和获取密钥、初始化、变更地图类型、添加控件、改变控件位置】(五)-全面详解(学习总结---从入门到深化)
33 0
|
6月前
38EasyUI 数据网格- 创建页脚摘要
38EasyUI 数据网格- 创建页脚摘要
21 0
|
9月前
|
定位技术
Echarts实战案例代码(27):地理坐标图视觉引导线及富文本提示框的案例
Echarts实战案例代码(27):地理坐标图视觉引导线及富文本提示框的案例
215 0
|
前端开发 搜索推荐 索引
重学前端 5 # 如何运用语义类标签来呈现Wiki网页?
重学前端 5 # 如何运用语义类标签来呈现Wiki网页?
89 0
重学前端 5 # 如何运用语义类标签来呈现Wiki网页?
|
编解码 定位技术
ArcGIS制作论文研究区域示意图的方法
本文介绍基于ArcMap软件,绘制论文中研究区域示意图、概况图等的方法~
1019 1
ArcGIS制作论文研究区域示意图的方法
|
移动开发 前端开发 HTML5
Web前端开发笔记——第二章 HTML语言 第六节 区域标签、列表标签
Web前端开发笔记——第二章 HTML语言 第六节 区域标签、列表标签
Web前端开发笔记——第二章 HTML语言 第六节 区域标签、列表标签
|
JavaScript 开发者
组件案例-实现评论的发表和自动刷新列表|学习笔记
快速学习组件案例-实现评论的发表和自动刷新列表
65 0

热门文章

最新文章