标签:void 包括 ima 书签 graph base public ref style
概述本文介绍通过Java程序来操作PDF书签,根据对书签的不同操作要求,分以下情况来介绍:
程序使用辅助工具:Free Spire.PDF for Java(免费版),
方法1:可下载jar包,解压并将lib文件夹下的jar文件导入java程序(手动导入)
方法2: 通过创建Maven项目程序,在pom.xml中配置maven仓库路径并指定Free Spire.PDF for Java 的依赖,配置完成后,在IDEA中,点击“Import Changes”导入JAR包:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<version>2.6.3</version>
</dependency>
</dependencies>
(Ecllipse的导入方法,参考这篇文章)
jar导入结果如下图所示:
1. 添加书签到PDF
import com.spire.pdf.*;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfTextStyle;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;
import java.awt.geom.Point2D;
public class AddBookmark {
public static void main(String[] args) {
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile("test.pdf");
//获取第一页
PdfPageBase page = pdf.getPages().get(0);
//添加书签
PdfBookmark bookmark = pdf.getBookmarks().add("第一部分 调查研究概述");
bookmark.setDisplayStyle(PdfTextStyle.Bold);//设置字体样式
bookmark.setColor(new PdfRGBColor(new Color(139, 69, 19)));//设置字体颜色
PdfDestination destination = new PdfDestination(page, new Point2D.Float(0, 0));//设置书签的目标页面和位置
bookmark.setAction(new PdfGoToAction(destination));
//添加二级子书签
PdfBookmark childBookmark = bookmark.add("第一节 菌种特性研究");
childBookmark.setColor(new PdfRGBColor(new Color(255, 127, 80)));//设置字体颜色
childBookmark.setDisplayStyle(PdfTextStyle.Italic);//设置字体样式
PdfDestination childDestination = new PdfDestination(page, new Point2D.Float(0, 100));//设置子书签的目标页面和位置
childBookmark.setAction(new PdfGoToAction(childDestination));
//添加三级子书签
PdfBookmark childBookmark1 = childBookmark.add("1. 菌种特性概念");
childBookmark1.setColor(new PdfRGBColor(new Color(125, 152, 100)));//设置字体颜色
childBookmark1.setDisplayStyle(PdfTextStyle.Italic);//设置字体样式
PdfDestination childDestination1 = new PdfDestination(page, new Point2D.Float(50, 100));//设置子书签的目标页面和位置
childBookmark1.setAction(new PdfGoToAction(childDestination1));
//保存文档
pdf.saveToFile("AddBookmarks.pdf");
pdf.dispose();
}
}
书签添加效果:
2. 编辑PDF中已有的书签
import com.spire.pdf.*;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfTextStyle;
import com.spire.pdf.graphics.PdfRGBColor;
import java.awt.*;
public class ModifyBookmark {
public static void main(String[] args) {
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile("AddBookmarks.pdf");
//获取第一个书签,修改书签标题、字体颜色、字体样式
PdfBookmark bookmark = pdf.getBookmarks().get(0);
bookmark.setTitle("新标题");
bookmark.setColor(new PdfRGBColor(new Color(255,20,147)));
bookmark.setDisplayStyle(PdfTextStyle.Bold);
//修改子书签的字体、颜色和字体样式等
for (int i = 0 ; i< pdf.getBookmarks().getCount(); i++ ) {
//修改二级子书签
PdfBookmark childBookmark = bookmark.get(0);
childBookmark.setColor(new PdfRGBColor(new Color(148,0,211)));
childBookmark.setDisplayStyle(PdfTextStyle.Italic);
//修改三级子书签
PdfBookmark childBookmark1 = childBookmark.get(0);
childBookmark1.setColor(new PdfRGBColor(new Color(255,215,0)) );
childBookmark1.setDisplayStyle(PdfTextStyle.Italic);
}
//保存文档
pdf.saveToFile("ModifyBookmarks.pdf");
pdf.close();
}
}
3. 删除PDF中的书签
import com.spire.pdf.*;
public class DeleteBookmark {
public static void main(String[] args) {
//创建PdfDocument实例,加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("AddBookmarks.pdf");
//删除第一个书签(包括删除其包含的子书签)
pdf.getBookmarks().removeAt(0);
/*//仅删除子书签
PdfBookmark childBookmark = pdf.getBookmarks().get(0);
childBookmark.removeAt(0);*/
//保存文档
pdf.saveToFile("RemoveBookmark.pdf");
pdf.dispose();
}
}
4. 读取PDF中的书签
import com.spire.pdf.*;
import com.spire.pdf.bookmarks.PdfBookmark;
import com.spire.pdf.bookmarks.PdfBookmarkCollection;
import java.io.FileWriter;
import java.io.IOException;
public class GetBookmark {
public static void main(String[] args) {
//加载包含书签的PDF文档
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("AddBookmarks.pdf");
//获取书签集合
PdfBookmarkCollection bookmarkCollection = pdf.getBookmarks();
//实例化StringBuilder类
StringBuilder stringbuilder = new StringBuilder();
//定义方法获取书签
GetBookmarkTitle(bookmarkCollection, stringbuilder);
//声明txt文件,并将获得的多级书签写入到文件.txt
FileWriter writer;
try {
writer = new FileWriter("读取的书签.txt");
writer.write(stringbuilder.toString());
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
pdf.dispose();
}
//定义方法获取书签标题
static void GetBookmarkTitle(PdfBookmarkCollection bookmarkCollection, StringBuilder stringbuilder)
{
if (bookmarkCollection.getCount()> 0)
{
for(int i = 0 ; i< bookmarkCollection.getCount(); i++ )
{
PdfBookmark parentBookmark = bookmarkCollection.get(i);
stringbuilder.append(parentBookmark.getTitle());
//递归文档多级书签
GetBookmarkTitle(parentBookmark, stringbuilder);
}
}
}
}
书签读取结果:
(本文完)
标签:void 包括 ima 书签 graph base public ref style
原文地址:https://blog.51cto.com/eiceblue/2513593