Added a new method to IncrementalUpdater which will write each PDF object's byte's to an byte[]. The method getUpdatedDocumentObjects() can be used to write out the changed PDF objects made by the most recent PDF edits.
The following code is n example of how to read the bytes back into the same PDF file.
try {
// get the updated bites
byte[][] updates = IncrementalUpdater.getUpdatedDocumentObjects(document);
// write the bytes to file
FileOutputStream fileOutputStream = new FileOutputStream("changes.txt");
for (byte[] update : updates)
{
fileOutputStream.write(update);
}
fileOutputStream.close();
controller.closeDocument();
// read the changes back into the document.
controller.openDocument(filePath);
FileInputStream fileInputStream = new FileInputStream("changes.txt");
Library library = document.getCatalog().getLibrary();
Parser parser = new Parser(fileInputStream);
// keep a hard reference to the objects
ArrayList<Object> libraryObjects = new ArrayList<Object>();
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 =
controller.getDocumentViewController()
.getDocumentViewModel().getPageComponents();
myAnnotationCallback = new org.icepdf.ri.common.MyAnnotationCallback(
controller.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<Annotation> annotations = page.getAnnotations();
// hook up the new annotations
for (Annotation annotation : annotations)
{
AnnotationComponent annotationComponent =
AnnotationComponentFactory.buildAnnotationComponent(
annotation,
controller.getDocumentViewController(),
pageViewComponent,
controller.getDocumentViewController().getDocumentViewModel());
myAnnotationCallback.newAnnotation(pageViewComponent, annotationComponent);
}
pageViewComponent.init();
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (PDFException e) {
e.printStackTrace();
}
Added a new method to IncrementalUpdater which will write each PDF object's byte's to an byte[]. The method getUpdatedDocumentObjects() can be used to write out the changed PDF objects made by the most recent PDF edits.
The following code is n example of how to read the bytes back into the same PDF file.
try {
{ fileOutputStream.write(update); }// get the updated bites
byte[][] updates = IncrementalUpdater.getUpdatedDocumentObjects(document);
// write the bytes to file
FileOutputStream fileOutputStream = new FileOutputStream("changes.txt");
for (byte[] update : updates)
fileOutputStream.close();
controller.closeDocument();
// read the changes back into the document.
controller.openDocument(filePath);
FileInputStream fileInputStream = new FileInputStream("changes.txt");
Library library = document.getCatalog().getLibrary();
{ libraryObjects.add(streamObject); }Parser parser = new Parser(fileInputStream);
// keep a hard reference to the objects
ArrayList<Object> libraryObjects = new ArrayList<Object>();
for (Object streamObject = parser.getObject(library);
streamObject != null; streamObject = parser.getObject(library))
// iterate over the stream objects look for pages so we know which
// annotations should be added to which page.
pageComponents =
controller.getDocumentViewController()
.getDocumentViewModel().getPageComponents();
myAnnotationCallback = new org.icepdf.ri.common.MyAnnotationCallback(
controller.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
{ AnnotationComponent annotationComponent = AnnotationComponentFactory.buildAnnotationComponent( annotation, controller.getDocumentViewController(), pageViewComponent, controller.getDocumentViewController().getDocumentViewModel()); myAnnotationCallback.newAnnotation(pageViewComponent, annotationComponent); }List<Annotation> annotations = page.getAnnotations();
// hook up the new annotations
for (Annotation annotation : annotations)
pageViewComponent.init();
}
}
} catch (IOException e)
{ e.printStackTrace(); } catch (PDFException e) { e.printStackTrace(); }