博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iText操作PDF基础-3
阅读量:7171 次
发布时间:2019-06-29

本文共 3048 字,大约阅读时间需要 10 分钟。

hot3.png

Anchor 跳转

Anchor(描点,标记):文档内部或者外部文档连接标记。实现和web中href连接作用一样的功能。主要有2种实现方式:使用Anchor对象直接设置引用和目标,或者使用Chunk对象引用。

 
1 public static void main(String[] args) throws FileNotFoundException, 2 DocumentException { 3 Document document = new Document(); // 创建一个文档 4 PdfWriter.getInstance(document, new FileOutputStream( " D:/demo.pdf " )); // 获取PdfWriter实例 5 document.open(); // 打开文档 6 Anchor intentAnchor = new Anchor( " Intent Link " ); 7 intentAnchor.setName( " intentLink " ); 8 9 Anchor intentAnchorDest = new Anchor( " Intent Link Destination " ); 10 intentAnchorDest.setName( " intentLinkDest " ); 11 12 intentAnchor.setReference( " #intentLinkDest " ); // 注意#号 13 14 Chunk chunk = new Chunk( " Chunk link " ); 15 chunk.setLocalGoto( " intentLinkDest " ); // 这里没有#号 16 // chunk.setLocalDestination("chunkDest"); // 将这个Chunk对象做一个目标地址 17 document.add(intentAnchor); 18 document.add(chunk); 19 document.newPage(); 20 document.add(intentAnchorDest); 21 document.close(); // 关闭文档 22 23 } 24

Chapter And Section 章节目录 书签

添加章节和小节,可以通过书签快速定位到相关的章节。

 
1 public static void main(String[] args) { 2 try { 3 Document document = new Document(); // 创建一个文档 4 PdfWriter 5 .getInstance(document, new FileOutputStream( " D:/demo.pdf " )); // 获取PdfWriter实例 6 document.open(); // 打开文档 7 8 for ( int i = 0 ; i < 3 ; i ++ ) { 9 Chunk chunk = new Chunk( " Chapter: " + i); 10 Paragraph pha = new Paragraph(); 11 pha.add(chunk); 12 // 每个Chapter都会创建一个新的页面 13 Chapter chapter = new Chapter(pha, i); 14 for ( int j = 0 ; j < 3 ; j ++ ) { 15 Paragraph secPar = new Paragraph(); 16 Chunk secChunk = new Chunk( " Section : " + j); 17 secPar.add(secChunk); 18 // Section构造行数都是protected, 19 // 通过Chapter.addSection(Paragraph pra)获取 20 Section sec = chapter.addSection(secPar); 21 sec.setIndentationLeft(10f); 22 } 23 document.add(chapter); 24 } 25 26 document.close(); // 关闭文档 27 } catch (Exception e) { 28 System.out.println(e.getMessage()); 29 } 30 } 31

查看结果:

Rectangle和Image

Rectangle

 
1 public static void main(String[] args) { 2 try { 3 Document document = new Document(); // 创建一个文档 4 PdfWriter 5 .getInstance(document, new FileOutputStream( " D:/demo.pdf " )); // 获取PdfWriter实例 6 document.open(); // 打开文档 7 8 Rectangle rec = new Rectangle(0f, 806f, 32f, 842f); 9 rec.setBackgroundColor(BaseColor.RED); 10 document.add(rec); 11 document.close(); // 关闭文档 12 } catch (Exception e) { 13 System.out.println(e.getMessage()); 14 } 15 } 16

结果:

Image

 
public static void main(String[] args) { try { Document document = new Document(); // 创建一个文档 PdfWriter .getInstance(document, new FileOutputStream( " D:/demo.pdf " )); // 获取PdfWriter实例 document.open(); // 打开文档 // Image是一个抽象对,无法直接实例化,可以通过众多静态方法获取实例。 String jpegPath = " d:/1.jpg " ; Image jpeg = Image.getInstance(jpegPath); jpeg.scalePercent(20f); // 缩放 jpeg.setRotation( - 30f); // 旋转 Image jpeg2 = Image.getInstance(jpegPath); jpeg2.scaleToFit(10f, 10f); // 适配大小 document.add(jpeg); document.add(jpeg2); document.close(); // 关闭文档 } catch (Exception e) { System.out.println(e.getMessage()); } }

查看结果:

转载于:https://my.oschina.net/smellok/blog/74105

你可能感兴趣的文章
罗森伯格应邀主讲CDCC百家大讲堂38期
查看>>
How to Install Nextcloud 13 Server on Debian 9
查看>>
[深入理解文件系统之一] IO系统调用
查看>>
Java之implements
查看>>
【资料收集】林内域或者林间域之间的账户、计算机迁移
查看>>
更新windows SID工具,对于虚拟机复制很有用
查看>>
安装TOMCAT
查看>>
-bash: lsof: command not found 解决方法
查看>>
《.NET应用架构设计:原则、模式与实践》新书博客--试读-2.1.2 设计原则实战
查看>>
大家技术探讨
查看>>
使用Myeclipse自带的xFire来实现WebService
查看>>
《UNIX环境高级编程》apue.h 头文件的问题
查看>>
系统分析师证书求挂靠,请联系qq 369681392
查看>>
ubuntu中root与user相互切换
查看>>
(转载)Http 请求处理流程
查看>>
GetVersion和GetVersionEx
查看>>
软工实践第一次作业
查看>>
php采集利器snoopy应用技巧
查看>>
我的友情链接
查看>>
安装虚拟机shell脚本
查看>>