package com.qoppa.sample; import java.awt.Color; import java.awt.geom.Rectangle2D; import java.util.Vector; import com.qoppa.pdf.PDFException; import com.qoppa.pdf.actions.Action; import com.qoppa.pdf.actions.LaunchAction; import com.qoppa.pdf.annotations.Link; import com.qoppa.pdfProcess.PDFDocument; import com.qoppa.pdfProcess.PDFPage; /** * This sample shows how to create link with a LaunchAction and * set the flag to open in a new window, the existing window, or defer to the user preference. * */ public class AddLinksLaunchAction { public static final String INPUT = "input.pdf"; public static final String OPEN_FILE = "open.pdf"; public static final String OUTPUT = "out.pdf"; public static void main(String [] args) throws Exception { PDFDocument doc = new PDFDocument(INPUT, null); // open in a new window addLink(doc.getPage(0), new Rectangle2D.Double(50, 50, 100, 30), OPEN_FILE, Action.OPEN_NEW_WINDOW); // open in this window addLink(doc.getPage(0), new Rectangle2D.Double(50, 150, 100, 30), OPEN_FILE, Action.OPEN_EXISTING_WINDOW); // open according to user preferences addLink(doc.getPage(0), new Rectangle2D.Double(50, 250, 100, 30), OPEN_FILE, Action.OPEN_USER_PREFERENCE); doc.saveDocument(OUTPUT); } /** * Add a link to the page * @param page Page to add the link to * @param bounds Location on the page to place the link * @param path File path to set in the action * @param newWindow Flag for opening the document in the current window, a new window, or according to user preference * @throws PDFException */ private static void addLink(PDFPage page, Rectangle2D bounds, String path, int newWindow) throws PDFException { // create a new link Link link = page.getDocument().getAnnotationFactory().createLink(); // show blue border around the link link.setRectangle(bounds); link.setBorderWidth(1f); link.setColor(Color.blue); // Add launch action to the link LaunchAction action = new LaunchAction(path, newWindow); Vector actionList = new Vector(); actionList.add(action); link.setActions(actionList); // Add the link to the page page.addAnnotation(link); } }