2014-01-09 63 views
7

PDFBOXを使用して作成したハイパーリンクをPDFに追加すると、「ここをクリック」というテキスト例をクリックするとURLにリダイレクトされます。 PDAnnotationLinkPDActionURIを使ってみましたが、それをcontentstreamに追加するにはどうすればよいですか?pdfboxを使用してpdfにハイパーリンクを追加する方法

PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); 
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); 
PDAnnotationLink txtLink = new PDAnnotationLink(); 
txtLink.setBorderStyle(borderULine); 
txtLink.setColour(colourBlue); 

// add an action 
PDActionURI action = new PDActionURI(); 
action.setURI("www.google.com"); 
txtLink.setAction(action); 

contentStream.beginText(); 
contentStream.moveTextPositionByAmount(400, y-30); 
contentStream.drawString(txtLink);----error 
contentStream.endText(); 
+0

「ここをクリック」を追加する方法の任意のアイデアは、リンクに? – decal

答えて

7

それeasier to add hyperlinksますPDFBox-Layoutと呼ばれるライブラリがあり、次のコード

PDRectangle position = new PDRectangle(); 
    position.setLowerLeftX(10); 
    position.setLowerLeftY(20); 
    position.setUpperRightX(100); 
    position.setUpperRightY(10); 
    txtLink.setRectangle(position); 
    page.getAnnotations().add(txtLink); 
+0

これはコンテンツストリームに何かを追加していませんが、注釈はコンテンツストリームの一部ではありません... – fabian

1

を使用contentStreamに追加する:

Document document = new Document(); 

Paragraph paragraph = new Paragraph(); 
paragraph.addMarkup(
    "This is a link to {link[https://github.com/ralfstuckert/pdfbox-layout]}PDFBox-Layout{link}", 
18f, BaseFont.Helvetica); 
document.add(paragraph); 

final OutputStream outputStream = new FileOutputStream("link.pdf"); 
document.save(outputStream);