how to set hyperlink in content using pdfbox

心不动则不痛 提交于 2019-12-24 07:05:51

问题


In Below code i want to add hyperlink in "Google.com"

contentStream.beginText();
contentStream.setNonStrokingColor(0,0,0);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 8);
contentStream.newLineAtOffset(315, 220);
contentStream.showText("Website: google.com");
contentStream.endText();

I want to display hyperlink in google.com and it should redirect when clicking on google.com


回答1:


try this code also worked,

contentStream.beginText();
contentStream.setNonStrokingColor(0,0,0);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 8);
contentStream.newLineAtOffset(50,220);
contentStream.showText("Website: google.com");
contentStream.endText();

// create a link annotation
PDAnnotationLink txtLink = new PDAnnotationLink();

// add an underline
PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
txtLink.setBorderStyle(underline);

PDRectangle position = new PDRectangle();
position.setLowerLeftX(75);
position.setLowerLeftY(210);
position.setUpperRightX(85);
position.setUpperRightY(220);
txtLink.setRectangle(position);

PDActionURI action = new PDActionURI();
action.setURI("http://www.Google.com");
txtLink.setAction(action);

page.getAnnotations().add(txtLink);


来源:https://stackoverflow.com/questions/50146466/how-to-set-hyperlink-in-content-using-pdfbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!