[ 
https://issues.apache.org/jira/browse/PDFBOX-45?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15890614#comment-15890614
 ] 

Tilman Hausherr commented on PDFBOX-45:
---------------------------------------

Why signing always works and other updates (like the one in the stackoverflow 
issue) sometimes doesn't:

COSWriter does updates properly only if there is a chain of objects marked for 
update to the ones we want to be updated. When adding an annotation, we usually 
mark the page and the annotation array, and sometimes the document catalog. 
However the page tree is not marked for update. In one file I have, the page 
tree has several levels until the actual page is reached. 

So in the stackoverflow issue, I ended up doing this hard coding:

{code}
COSBase item = document.getPages().getCOSObject().getItem(COSName.KIDS);
((COSUpdateInfo) item).setNeedToBeUpdated(true);
COSArray kids = (COSArray) item;
kids.setNeedToBeUpdated(true);
((COSUpdateInfo) kids.get(0)).setNeedToBeUpdated(true);
{code}

The code in the stackoverflow issue has this line 
{code}
document.getPages().getCOSObject().setNeedToBeUpdated(true);
{code}
that is a dictionary that contains the array that contains the array that 
points to our page, and this explains why simple PDFs have no trouble.

But why is visual signing always working? This is because we add the signature 
field/annotation as a top level field. And by calling
{code}
signatureField.getWidgets().get(0).setPage(page);
{code}
in PDDocument we make sure that the page (which has also an update mark) is 
reached.

So either we'd have to (1) build an update chain, or (2) improve {{COSWriter}} 
to recursively search for "orphans", or (3) pass a list of objects to 
{{saveIncremental}}, and then to pass these to the {{COSWriter}} constructor, 
and call {{addObjectToWrite}} for each of them.

Until then here's some better code for (1):
{code}
COSDictionary dict = page.getCOSObject();
while (dict.containsKey(COSName.PARENT))
{
    COSBase parent = dict.getDictionaryObject(COSName.PARENT);
    if (parent instanceof COSDictionary)
    {
        dict = (COSDictionary) parent;
        dict.setNeedToBeUpdated(true);
    }
}
{code}
After investigating and writing all this, now I see there's a comment by mkl 
that's just the same :-(

> Support incremental save
> ------------------------
>
>                 Key: PDFBOX-45
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-45
>             Project: PDFBox
>          Issue Type: New Feature
>          Components: Writing
>            Assignee: Andreas Lehmkühler
>             Fix For: 2.1.0
>
>
> [imported from SourceForge]
> http://sourceforge.net/tracker/index.php?group_id=78314&atid=552835&aid=1157431
> Originally submitted by purplish_cat on 2005-03-05 12:28.
> After opening a PDF file and changing objects out of it, 
> allow to save the changes incrementally to the same file 
> instead of creating a completely new file.
> [comment on SourceForge]
> Originally sent by benlitchfield.
> Logged In: YES 
> user_id=601708
> See forum thread at
> https://sourceforge.net/forum/message.php?msg_id=3032112



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to