【itext学习之路】-------(第二篇)设置pdf的一些常用属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tomatocc/article/details/80666361

在上一篇文章中,我们已经成功的创建了一个简单的pdf,下面我将学习设置该pdf的常用属性,其中包括:作者,创建时间,pdf创建者,pdf生产者,关键字,标题,主题

  • 下面是我们的代码,非常简单。
package cn.tomtocc.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class pdfTest {

    public static void main(String[] args) throws FileNotFoundException,
            DocumentException {
        //实现A4纸页面 并且横向显示(不设置则为纵向)
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream("d:/demo.pdf"));
        // 打开文档
        document.open();
        // 生成第一页
        document.newPage();
        //设置作者
        document.addAuthor("tomaotcc");
        //设置创建日期
        document.addCreationDate();
        // 设置创建者
        document.addCreator("tomaotcc.com");
        // 设置生产者
        document.addProducer();
        // 设置关键字
        document.addKeywords("my");
        //设置标题
        document.addTitle("Set Attribute Example");
        //设置主题
        document.addSubject("An example to show how attributes can be added to pdf files.");
        // 加入文档内容
        document.add(new Paragraph("my first pdf demo"));
        // 关闭文档
        document.close();
        pdfWriter.close();
    }
}
  • 然后我们打开生成的pdf,然后鼠标右键—–>文档属性
  • 这里写图片描述

  • 然后我们就可以看到我们刚才设置的pdf文档属性了

  • 这里写图片描述

到这里为止,我们设置pdf的属性就完成了,接下来,我们将要学习pdf文档加密。

【itext学习之路】系列教程

【itext学习之路】—–(第一篇)创建一个简单的pdf文档
【itext学习之路】—–(第二篇)设置pdf的一些常用属性
【itext学习之路】—–(第三篇)对pdf文档进行加密和权限设置
【itext学习之路】—–(第四篇)给pdf增加文本水印和图片水印
【itext学习之路】—–(第五篇)对pdf进行盖章/签章/数字签名

猜你喜欢

转载自blog.csdn.net/tomatocc/article/details/80666361