package com.me.test;

import java.awt.*;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
/**
 * 通过给定的短语生成pdf文件,并且加密
 */
/**
 * First iText example: Hello World.
 */
public class HelloWorld {

	/** Path to the resulting PDF file. */
	public static final String RESULT = "f:/hello.pdf";
	public static final String pwd = "123456";
	public static final String result = "f:/Itext/b.gif";

	/**
	 * Creates a PDF file: hello.pdf
	 * 
	 * @param args
	 *            no arguments needed
	 */
	public static void main(String[] args) throws DocumentException,
			IOException {
		new HelloWorld().createPdf(RESULT);
	}

	/**
	 * Creates a PDF document.
	 * 
	 * @param filename
	 *            the path to the new PDF document
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void createPdf(String filename) throws DocumentException,
			IOException {
		// 设定文本样式
		Rectangle rec = new Rectangle(PageSize.A4);
		rec.setBackgroundColor(Color.GRAY);
		rec.setBorder(Rectangle.TOP);
		rec.setBorderColor(Color.black);
		rec.setBorderWidth(50);
		// 创建本文
		Document doc = new Document(rec, 100, 201, 20, 20);
		// 设定路径
		PdfWriter pdf = PdfWriter.getInstance(doc, new FileOutputStream(
				HelloWorld.RESULT));
		// 设定布局
		pdf.setViewerPreferences(PdfWriter.PageModeUseThumbs
				| PdfWriter.PageLayoutTwoColumnLeft | PdfWriter.HideMenubar);
		// 加密
		pdf.setEncryption(pwd.getBytes(), pwd.getBytes(), PdfWriter.ALLOW_COPY
				| PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_40);
		// 设置中文
		BaseFont base = null;
		Font fontChinese = null;
		try {
			base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.EMBEDDED);
			fontChinese = new Font(base, 18, Font.BOLD);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		doc.open();

		doc.add(new Paragraph("我是Pro", fontChinese));

		doc.close();
	}
}
最近下载更多
fesfefe  LV13 2023年11月29日
gshnlj  LV15 2022年8月24日
魔幻男灵  LV8 2022年6月12日
sixi_sixi  LV2 2022年6月2日
543539666  LV7 2022年3月3日
8战魂5无双8  LV43 2021年10月30日
406184639  LV1 2021年7月2日
zx55176670  LV2 2021年3月10日
mojunxin  LV4 2020年11月13日
EdgarLi  LV14 2020年5月18日
最近浏览更多
新哥新奇士橙  LV4 1月27日
3334004690  LV10 2024年5月27日
镜影  LV3 2024年5月21日
PaymentCodeSystem  LV11 2024年3月31日
zhos0212  LV19 2024年3月29日
NHealers  LV5 2023年12月14日
fesfefe  LV13 2023年11月26日
田仁亿 2022年11月9日
暂无贡献等级
G你太美  LV9 2022年10月13日
魔幻男灵  LV8 2022年6月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友