Java PDFBOX:使用PDFBOX进行索引
我需要为PDF文档创建一个索引&基于我想要显示页面的索引Java PDFBOX:使用PDFBOX进行索引,java,spring,pdf-generation,open-source,pdfbox,Java,Spring,Pdf Generation,Open Source,Pdfbox,我需要为PDF文档创建一个索引&基于我想要显示页面的索引 问题是页码是动态创建的,因此无法使用静态页码,有人能给我推荐链接或示例吗?这是我的代码,我可以在单击索引页上的页码时重定向到不同的页面 public class LinkPdf { static final float INCH = 72; public static List<PDAnnotation> annotations; @SuppressWarnings("null") public static void m
问题是页码是动态创建的,因此无法使用静态页码,有人能给我推荐链接或示例吗?这是我的代码,我可以在单击索引页上的页码时重定向到不同的页面
public class LinkPdf {
static final float INCH = 72;
public static List<PDAnnotation> annotations;
@SuppressWarnings("null")
public static void main(String[] args) throws Exception {
PDRectangle position = null;
PDDocument document = new PDDocument();
try {
for (int i = 0; i < 10; i++) {
PDPage page = new PDPage();
document.addPage(page);
}
PDPage page1 = document.getPage(0);
HashMap<Integer, String> pgs = new HashMap<Integer, String>();
for (int i = 0; i < document.getNumberOfPages(); i++) {
pgs.put(i, "Jump to Page" + i);
}
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contents = new PDPageContentStream(document,
page1);
contents.beginText();
contents.setFont(font, 18);
contents.newLineAtOffset(50, 600);
contents.setLeading(20f);
contents.showText("PDFBox");
position = new PDRectangle();
for (int i = 0; i < document.getNumberOfPages(); i++) {
contents.newLine();
contents.showText(pgs.get(i));
contents.newLine();
}
contents.endText();
contents.close();
PDRectangle[] pos1 = new PDRectangle[document.getNumberOfPages()];
for(int i=0;i<document.getNumberOfPages();i++){
pos1[i]=new PDRectangle();
pos1[i].setLowerLeftX(50);
pos1[i].setLowerLeftY(575-(i*40));
pos1[i].setUpperRightX(INCH + 100);
pos1[i].setUpperRightY(585-(i*40));
getPage(document, page1, pgs.get(i), i, pos1[i]);
}
document.save("D:/link.pdf");
System.out.println("Completed");
} finally {
document.close();
}
}
public static void getPage(PDDocument document, PDPage page1, String txt,
int pageno, PDRectangle position) throws IOException {
annotations = page1.getAnnotations();
PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
borderThick.setWidth(INCH / 12); // 12th inch
PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
borderThin.setWidth(INCH / 72); // 1 point
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
borderULine.setWidth(INCH / 72); // 1 point
float pw = page1.getMediaBox().getUpperRightX();
float ph = page1.getMediaBox().getUpperRightY();
float textWidth = PDType1Font.TIMES_BOLD.getStringWidth("PDFBox") / 1000 * 18;
PDAnnotationLink pageLink = new PDAnnotationLink();
pageLink.setBorderStyle(borderULine);
textWidth = PDType1Font.TIMES_BOLD.getStringWidth(txt) / 1000 * 18;
pageLink.setRectangle(position);
PDActionGoTo actionGoto = new PDActionGoTo();
PDPageDestination dest = new PDPageFitWidthDestination();
dest.setPage(document.getPage(pageno));
actionGoto.setDestination(dest);
pageLink.setAction(actionGoto);
annotations.add(pageLink);
}
}
公共类链接PDF{
静态最终浮动英寸=72;
公共静态列表注释;
@抑制警告(“空”)
公共静态void main(字符串[]args)引发异常{
PDRectangle位置=空;
PDDocument文档=新PDDocument();
试一试{
对于(int i=0;i<10;i++){
PDPage page=新PDPage();
文件。添加页(第页);
}
PDPage page1=document.getPage(0);
HashMap pgs=新的HashMap();
对于(int i=0;i 对于(int i=0;i@mkl你能给我举个例子吗?老实说,我不太了解你的任务以及你执行任务时遇到的问题。@mkl我想用锚链接创建目录。点击链接时,应该会显示参考页。你是否尝试过源代码下载中的AddAnnotations.java示例?如果是,请重写你的问题澄清问题所在。