package test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.PObject; import org.icepdf.core.pobjects.Page; import org.icepdf.core.pobjects.annotations.Annotation; import org.icepdf.core.util.IncrementalUpdater; import org.icepdf.core.util.Library; import org.icepdf.core.util.Parser; import org.icepdf.ri.common.MyAnnotationCallback; import org.icepdf.ri.common.SwingController; import org.icepdf.ri.common.views.AbstractPageViewComponent; import org.icepdf.ri.common.views.AnnotationComponent; import org.icepdf.ri.common.views.annotations.AnnotationComponentFactory; public class MySwingController extends SwingController { public MySwingController() { super(); } public void loadMarkupFile(InputStream lIs) throws Exception { Document document = this.getDocument(); List pageComponents = this .getDocumentViewController().getDocumentViewModel() .getPageComponents(); Library library = document.getCatalog().getLibrary(); Parser parser = new Parser(lIs); // keep a hard reference to the objects ArrayList libraryObjects = new ArrayList(); for (Object streamObject = parser.getObject(library); streamObject != null; streamObject = parser .getObject(library)) { libraryObjects.add(streamObject); } // iterate over the stream objects look for pages so we know which // annotations should be added to which page. pageComponents = this.getDocumentViewController() .getDocumentViewModel().getPageComponents(); MyAnnotationCallback myAnnotationCallback = new org.icepdf.ri.common.MyAnnotationCallback( this.getDocumentViewController()); for (Object objects : libraryObjects) { if (objects instanceof PObject && ((PObject) objects).getObject() instanceof Page) { Page page = (Page) ((PObject) objects).getObject(); page.init(); int pageNumber = document.getPageTree().getPageNumber( page.getPObjectReference()); AbstractPageViewComponent pageViewComponent = pageComponents .get(pageNumber); // add our new page to the library List annotations = page.getAnnotations(); // hook up the new annotations for (Annotation annotation : annotations) { AnnotationComponent annotationComponent = AnnotationComponentFactory .buildAnnotationComponent(annotation, this .getDocumentViewController(), pageViewComponent, this .getDocumentViewController() .getDocumentViewModel()); myAnnotationCallback.newAnnotation(pageViewComponent, annotationComponent); } // finally show in the viewer the annotated file. pageViewComponent.init(); } } } public void saveMarkupFile() throws Exception { byte[][] updates = IncrementalUpdater.getUpdatedDocumentObjects(this .getDocument()); // write the bytes to file // ByteArrayOutputStream baos = new ByteArrayOutputStream(); String sFile = this.getDocument().getDocumentOrigin() + ".txt"; System.out.println("File: " + sFile); FileOutputStream fileOutputStream = new FileOutputStream(sFile); for (byte[] update : updates) { // fileOutputStream.write(update); fileOutputStream.write(update); } fileOutputStream.close(); } /** * Utility method for saving a copy of the currently opened PDF to a file. * This will check all valid permissions and show a file save dialog for the * user to select where to save the file to, and what name to give it. */ @Override public void saveFile() { // // Ensure we actually CAN save the document in the first place // if (!havePermissionToModifyDocument()) { // org.icepdf.ri.util.Resources.showMessageDialog( // viewer, // JOptionPane.INFORMATION_MESSAGE, // messageBundle, // "viewer.dialog.saveAs.noPermission.title", // "viewer.dialog.saveAs.noPermission.msg"); // return; // } // // this.getDocument(); if (!this.getDocument().getStateManager().isChanged() && !Document.foundIncrementalUpdater) { org.icepdf.ri.util.Resources.showMessageDialog(this .getViewerFrame(), JOptionPane.INFORMATION_MESSAGE, this .getMessageBundle(), "viewer.dialog.saveAs.noUpdates.title", "viewer.dialog.saveAs.noUpdates.msg"); return; } try { saveMarkupFile(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // } @Override public void openURL() { try { FileInputStream is = new FileInputStream(this.getDocument() .getDocumentOrigin() + ".txt"); loadMarkupFile(is); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } this.updateDocumentView(); } }