package org.icepdf.os.examples.annotation.creation;
/*
* Copyright 2006-2017 ICEsoft Technologies Canada Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import org.icepdf.core.pobjects.Destination;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Reference;
import org.icepdf.core.pobjects.actions.ActionFactory;
import org.icepdf.core.pobjects.actions.GoToAction;
import org.icepdf.core.pobjects.actions.URIAction;
import org.icepdf.core.pobjects.annotations.Annotation;
import org.icepdf.core.pobjects.annotations.AnnotationFactory;
import org.icepdf.core.pobjects.annotations.BorderStyle;
import org.icepdf.core.pobjects.annotations.LinkAnnotation;
import org.icepdf.core.pobjects.graphics.text.WordText;
import org.icepdf.core.search.DocumentSearchController;
import org.icepdf.core.util.Library;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.common.views.AbstractPageViewComponent;
import org.icepdf.ri.common.views.AnnotationComponent;
import org.icepdf.ri.common.views.DocumentViewControllerImpl;
import org.icepdf.ri.common.views.annotations.AnnotationComponentFactory;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* The <code>NewAnnotationPostPageLoad</code> class is an example of how to use
* <code>DocumentSearchController</code> to find search terms in a
* Document and convert the found words to annotations.
* <p/>
* A file specified at the command line is
* opened in a JFrame which contains the viewer component and any number
* of search terms can be specefied after the file name.
* <p/>
* Example:
* SearchHighlight "c:\DevelopersGuide.pdf" "PDF" "ICEsoft" "ICEfaces" "ICEsoft technologies"
* <p/>
* The file that is opened in the Viewer RI will have the new annotations created
* around the found search terms. The example creates a URIActions for each
* annotation but optionally can be compiled to build GotoActions to 'goto'
* the last page of the document when executed.
* <p/>
* The annotation are created after the Document view is created so we
* have to create new annotation slightly differently then if we where adding
* them before teh view was created.
*
* @since 4.0
*/
public class NewAnnotationPostPageLoad {
public static void main(final String[] args) {
if (args.length < 2) {
System.out.println("At least two command line arguments must " +
"be specified. ");
System.out.println("<filename> <term1> ... <termN>");
}
// Get a file from the command line to open
final String filePath = args[0];
// get search terms from command line
final String[] terms = new String[args.length - 1];
for (int i = 1, max = args.length; i < max; i++) {
terms[i - 1] = args[i];
}
try {
final SwingController controller = new SwingController();
// startup the viewer.
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
/**
* Create a new instance so we can view the modified file.
*/
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
JFrame applicationFrame = new JFrame();
applicationFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
applicationFrame.getContentPane().add(viewerComponentPanel);
// add interactive mouse link annotation support via callback
org.icepdf.ri.common.MyAnnotationCallback myAnnotationCallback =
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController());
controller.getDocumentViewController().setAnnotationCallback(
myAnnotationCallback);
// set document view mode
controller.getDocumentViewController().setViewType(
DocumentViewControllerImpl.ONE_COLUMN_VIEW);
// Now that the GUI is all in place, we can try opening the PDF
controller.openDocument(filePath);
// show the document and the new annotations.
applicationFrame.pack();
applicationFrame.setVisible(true);
/*
* Start of a simple search for the loaded file and collect word
* data for annotation creation.
*/
// get the search controller
DocumentSearchController searchController =
controller.getDocumentSearchController();
// add a specified search terms.
for (String term : terms) {
searchController.addSearchTerm(term, false, false);
}
// search the pages in the document or a subset
Document document = controller.getDocument();
// set the max number of pages to search and create annotations for.
int pageCount = 25;
if (pageCount > document.getNumberOfPages()) {
pageCount = document.getNumberOfPages();
}
/*
* Apply the search -> annotation resulst after the gui is build
*/
// new annotation look and feel
// list of founds words to print out
ArrayList<WordText> foundWords;
List<AbstractPageViewComponent> pageComponents =
controller.getDocumentViewController()
.getDocumentViewModel().getPageComponents();
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
// get the search results for this page
foundWords = searchController.searchPage(pageIndex);
if (foundWords != null) {
// get the current page lock and start adding the annotations
AbstractPageViewComponent pageViewComponent =
pageComponents.get(pageIndex);
for (WordText wordText : foundWords) {
// create a new link annotation
LinkAnnotation linkAnnotation = (LinkAnnotation)
AnnotationFactory.buildAnnotation(
document.getPageTree().getLibrary(),
Annotation.SUBTYPE_LINK,
wordText.getBounds().getBounds());
BorderStyle borderStyle = new BorderStyle();
borderStyle.setBorderStyle(BorderStyle.BORDER_STYLE_SOLID);
borderStyle.setStrokeWidth(2.0f);
linkAnnotation.setBorderStyle(borderStyle);
linkAnnotation.setColor(Color.red);
AnnotationComponent annotationComponent =
AnnotationComponentFactory.buildAnnotationComponent(
linkAnnotation,
controller.getDocumentViewController(),
pageViewComponent,
controller.getDocumentViewController().getDocumentViewModel());
// create a new URI action
org.icepdf.core.pobjects.actions.Action action =
createURIAction(document.getPageTree().getLibrary(),
"http://www.icepdf.org");
// or create a new goTo Annotation that links to the page
// number represented by pageCount.
// org.icepdf.core.pobjects.actions.Action action =
// createGoToAction(
// document.getPageTree().getLibrary(),
// document, document.getNumberOfPages() - 1);
// add the action to the annotation
linkAnnotation.addAction(action);
// add it to the pageComponent, not the page, as we won't
// see it until the page is re-initialized.
controller.getDocumentViewController().getAnnotationCallback()
.newAnnotation(pageViewComponent, annotationComponent);
}
}
// removed the search highlighting
searchController.clearSearchHighlight(pageIndex);
// The save button can be used in the UI to save a copy of the
// document.
}
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* Utility for creation a URI action
*
* @param library document library reference
* @param uri uri that actin will launch
* @return new URIAction object instance.
*/
private static org.icepdf.core.pobjects.actions.Action createURIAction(
Library library, String uri) {
URIAction action = (URIAction)
ActionFactory.buildAction(
library,
ActionFactory.URI_ACTION);
action.setURI(uri);
return action;
}
/**
* Utility for creation a GoTo action
*
* @param library document library reference
* @param pageIndex page index to go to.
* @return new GoToAction object instance.
*/
private static org.icepdf.core.pobjects.actions.Action createGoToAction(
Library library, Document document, int pageIndex) {
GoToAction action = (GoToAction)
ActionFactory.buildAction(
library,
ActionFactory.GOTO_ACTION);
Reference pageReference = document.getPageTree()
.getPageReference(pageIndex);
List destArray = Destination.destinationSyntax(pageReference,
Destination.TYPE_FIT);
action.setDestination(new Destination(library, destArray));
return action;
}
}