//设置文件属性的PDF

package com.wf.zhang.test;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter; public class test01 { public static void main(String[] args) {
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("设置文件属性的PDF.pdf"));
document.open();
//设置宋体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字大小 颜色
Font font = new Font(bfChinese, 20, Font.NORMAL, BaseColor.RED); //准备Person类
Person person = new Person();
person.setName("娃哈哈");
person.setAge(99);
person.setAdress("杭州西湖区文一路"); document.add(new Paragraph(person.toString(), font)); //Set attributes here
document.addAuthor("没有正经的人"); //作者
document.addCreationDate(); //创建时间
document.addCreator("销售报表.com"); //创建程序
document.addTitle("新的标题"); //标题
document.addSubject("第二次+++++++++生成pdf"); //主题 document.close();
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
} }

//带图片的PDF

package com.wf.zhang.test;

import java.io.FileOutputStream;
import java.net.URL; import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter; public class test02 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("带图片的PDF.pdf"));
document.open(); //设置宋体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字大小 颜色
Font font = new Font(bfChinese, 20, Font.NORMAL, BaseColor.RED); //准备Person类
Person person = new Person();
person.setName("娃哈哈");
person.setAge(99);
person.setAdress("杭州西湖区文一路"); document.add(new Paragraph("带图片的123456PDF"+ "\r\n" +person.toString(), font)); // Add Image
Image image1 = Image.getInstance("temp.jpg");
// Fixed Positioning
image1.setAbsolutePosition(100f, 550f); //图片在PDF中的绝对位置
// Scale to new height and new width of image
image1.scaleAbsolute(100, 100); //图片在PDF中的大小
// Add to document
document.add(image1); String imageUrl = "http://www.eclipse.org/xtend/images/java8_logo.png";
Image image2 = Image.getInstance(new URL(imageUrl));
document.add(image2); document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

//带表格的PDF

package com.wf.zhang.test;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter; public class test03 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("表格的PDF.pdf"));
document.open(); //设置宋体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字大小 颜色
Font font = new Font(bfChinese, 20, Font.NORMAL, BaseColor.RED); PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(10f); // Space before table
table.setSpacingAfter(10f); // Space after table // Set Column widths
float[] columnWidths = { 1f, 1f, 1f };
table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph("序号",font));
cell1.setBorderColor(BaseColor.BLUE);
cell1.setPaddingLeft(10);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph("日期",font));
cell2.setBorderColor(BaseColor.GREEN);
cell2.setPaddingLeft(10);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell3 = new PdfPCell(new Paragraph("销售量",font));
cell3.setBorderColor(BaseColor.RED);
cell3.setPaddingLeft(10);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); // To avoid having the cell border and the content overlap, if you
// are having thick cell borders
// cell1.setUserBorderPadding(true);
// cell2.setUserBorderPadding(true);
// cell3.setUserBorderPadding(true); table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3); document.add(table); document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

//创建列表项的PDF

package com.wf.zhang.test;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.GreekList;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.RomanList;
import com.itextpdf.text.ZapfDingbatsList;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter; public class test04 {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("创建列表项的PDF.pdf"));
document.open(); //设置宋体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); //设置字大小 颜色
Font font = new Font(bfChinese, 20, Font.NORMAL, BaseColor.RED); document.add(new Paragraph("首页",font)); // Add ordered list
List orderedList = new List(List.ORDERED);
orderedList.add(new ListItem("第一行",font));
orderedList.add(new ListItem("第二行",font));
orderedList.add(new ListItem("第三行",font));
document.add(orderedList); // Add un-ordered list
List unorderedList = new List(List.UNORDERED);
unorderedList.add(new ListItem("第一条",font));
unorderedList.add(new ListItem("第二条",font));
unorderedList.add(new ListItem("第三条",font));
document.add(unorderedList); // Add roman list
RomanList romanList = new RomanList();
romanList.add(new ListItem("第一项",font));
romanList.add(new ListItem("第二项",font));
romanList.add(new ListItem("第三项",font));
document.add(romanList); // Add Greek list
GreekList greekList = new GreekList();
greekList.add(new ListItem("第一点",font));
greekList.add(new ListItem("第二点",font));
greekList.add(new ListItem("第三点",font));
document.add(greekList); // ZapfDingbatsList List Example
ZapfDingbatsList zapfDingbatsList = new ZapfDingbatsList(43, 30);
zapfDingbatsList.add(new ListItem("第一步",font));
zapfDingbatsList.add(new ListItem("第二步",font));
zapfDingbatsList.add(new ListItem("第三步",font));
document.add(zapfDingbatsList); // List and Sublist Examples
List nestedList = new List(List.UNORDERED);
nestedList.add(new ListItem("Item 1")); List sublist = new List(true, false, 30);
sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6)));
sublist.add("A");
sublist.add("B");
nestedList.add(sublist); nestedList.add(new ListItem("Item 2")); sublist = new List(true, false, 30);
sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6)));
sublist.add("C");
sublist.add("D");
nestedList.add(sublist); document.add(nestedList); document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

//带样式的PDF 在wps 会变成两页  不知道是什么鬼  使用网站的图片

package com.wf.zhang.test;

import java.io.FileOutputStream;

import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.PdfWriter; public class test05 {
public static void main(String[] args) {
Font blueFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0));
Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0));
Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0));
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("带样式的PDF.pdf"));
document.open();
// document.add(new Paragraph("Styling Example")); // Paragraph with color and font styles
Paragraph paragraphOne = new Paragraph("Some colored paragraph text", redFont);
document.add(paragraphOne); // Create chapter and sections
Paragraph chapterTitle = new Paragraph("Chapter Title", yellowFont);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0); Paragraph sectionTitle = new Paragraph("Section Title", redFont);
Section section1 = chapter1.addSection(sectionTitle); Paragraph sectionContent = new Paragraph("Section Text content", blueFont);
section1.add(sectionContent); document.add(chapter1); document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

//用户密码  和所有者密码都能打开PDF

package com.wf.zhang.test;

import java.io.File;
import java.io.FileOutputStream; import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter; public class test06 { private static String USER_PASSWORD = "123456"; //用户密码
private static String OWNER_PASSWORD = "asdfgh"; //所以者密码 public static void main(String[] args) {
try {
java.io.OutputStream file = new FileOutputStream(new File("加密的PDF.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file); writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128); document.open();
document.add(new Paragraph("Password Protected pdf example !!"));
document.close();
file.close(); } catch (Exception e) {
e.printStackTrace();
}
} }

//具有权限的PDF

package com.wf.zhang.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream; import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter; public class test07 { public static void main(String[] args) {
try {
OutputStream file = new FileOutputStream(new File("具有权限的PDF.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file); writer.setEncryption("".getBytes(), "".getBytes(),
PdfWriter.ALLOW_PRINTING , //Only printing allowed; Try to copy text !!
PdfWriter.ENCRYPTION_AES_128); document.open();
document.add(new Paragraph("Limited Access File !!"));
document.close();
file.close(); } catch (Exception e) {
e.printStackTrace();
}
}
}

//文字加图片的PDF  修改之前的HelloWorldPDF

package com.wf.zhang.test;

import java.io.FileOutputStream;
import java.io.IOException; import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper; public class test08 {
public static void main(String[] args) {
try {
// Read file using PdfReader
PdfReader pdfReader = new PdfReader("HelloWorld.pdf"); // Modify file using PdfReader
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorld-modified.pdf")); Image image = Image.getInstance("temp.jpg");
image.scaleAbsolute(100, 50);
image.setAbsolutePosition(100f, 500f); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
PdfContentByte content = pdfStamper.getUnderContent(i);
content.addImage(image);
} pdfStamper.close(); } catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}

最新文章

  1. ShareDrop – 苹果 AirDrop 服务的 HTML5 实现
  2. Delphi常用系统函数总结
  3. 9.19 JS数组
  4. Ubuntu 10.04 32位桌面版+OpnERP 6.1.1
  5. 37.寻找丑数[Ugly numbers]
  6. Mac OS实用技巧
  7. ftp自动上传下载文件脚本
  8. performance_schema 变量
  9. Sublime Text 2 介紹
  10. tomcat警告setting property &#39;debug&#39; to &#39;0&#39; did not find a matching property
  11. 组件嵌套时报:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
  12. mkfs -t ext3 错误/dev/sdxx is apparently in use by the system; 解决方法
  13. maven添加oracle驱动
  14. 【git】强制覆盖本地代码(与git远程仓库保持一致)
  15. 附003.Docker Compose命令详解
  16. Python高级网络编程系列之基础篇
  17. MyEclipse *的安装步骤和破解(32位和64位皆适用)(图文详解)
  18. 常用工具类系列之DateUtil
  19. 廖雪峰Java1-2程序基础-8字符和字符串
  20. Delphi Webbrowser使用方法详解(一)

热门文章

  1. 分布式算法-一致性HASH
  2. springboot+k8s+抛弃springcloud.eureka
  3. springboot中实现kafa指定offset消费
  4. Orleans[NET Core 3.1] 学习笔记(三)( 2 )客户端配置
  5. 自建CA证书搭建https服务器
  6. Java 打印Word文档
  7. JS Math对象、日期对象、函数、定时器
  8. JS---案例:图标跟着鼠标飞(有bug)
  9. onTouchEvent中,跟随手指滑动的view出现抖动
  10. iOS核心动画高级技巧 - 7