You are viewing limited content. For full access, please sign in.

Question

Question

Is it possible to copy annotations from page 1 of a document to page 2

asked on July 18, 2018

We often have to replace the image of a page in a document that has a number of annotations on it with a revised page.  We currently do this by inserting the new revised version as a new page in the document.  But then we have to manually recreate all the annotations currently on page 1 to the new page before we can delete the old page.

 

Clearly, this is very inefficient and we are looking for suggestions....

0 0

Answer

SELECTED ANSWER
replied on July 19, 2018

To expand on Jason's response, you can also move annotations from one page to another, using the MoveAnnotationTo method:


PageInfo srcPage = doc.GetPageInfo(srcPageNum);
PageInfo dstPage = doc.GetPageInfo(dstPageNum);

foreach (AnnotationBase ann in srcPage.GetAnnotations())
{
    ann.MoveAnnotationTo(dstPage);
}

srcPage.Save();
dstPage.Save();
doc.Save();

 

2 0
replied on July 19, 2018

Am I correct in assuming this would preserve the annotation metadata in the same way as replacing the underlying image?

If so, I think this would be the best option assuming they only need to keep the new page; reading/writing the image is almost certainly going to use more resources on the Workflow server than just moving the annotations.

0 0
replied on July 19, 2018

Yes, this method moves the annotation and all of its properties. The LF web client uses it in the doc viewer to move annotations to another page.

1 0
replied on July 19, 2018

Very nice. Thanks for the additional information!

0 0
replied on July 20, 2018

Great!  Thank you both.  Honestly, I was thinking this wasn't an option.  So, this is all great news!

0 0

Replies

replied on July 18, 2018 Show version history

The best option will depend on whether you want to keep both pages or just replace the underlying image from page 1 with the image from page 2.

In either case, your best option is probably going to be a workflow that runs an SDK Script activity to make the changes.

If you want users to be able to run this from the client, then you can set it up as a Business Process and they can copy the annotations by running the process.

Option 1 - Copy annotations and keep both pages

// Get document info object
// This configuration uses the workflow's starting entry
DocumentInfo doc = (DocumentInfo)this.BoundEntryInfo;

// Lock document to prevent other users/processes from making changes
doc.Lock(LockType.Exclusive);

// Check page count to avoid errors
if(doc.PageCount > 1){
    try
    {
        // Add all annotations from page 1 to page 2
        doc.GetPageInfo(2).AddAnnotations(doc.GetPageInfo(1).GetAnnotations());
        
        // Save changes
        doc.Save();
    }
    finally
    {
        // Always release the document even if an error occurs
        doc.Unlock();
        doc.Dispose();
    }
}

 

Option 2 - Replace page 1 image with page 2 image and only keep the updated page

// Get document info object
// This configuration uses the workflow's starting entry
DocumentInfo doc = (DocumentInfo)this.BoundEntryInfo;

// Lock document to prevent other users/processes from making changes
doc.Lock(LockType.Exclusive);

// Check page count to avoid errors
if(doc.PageCount > 1){
    try
    {
        // Create read stream to get page 2 image
        using(LaserficheReadStream rs = doc.GetPageInfo(2).ReadPagePart(PagePart.Image)){
            // Create stream writer for page 1 image
            using(Stream writer = doc.GetPageInfo(1).WritePagePart(PagePart.Image, (int)rs.Length)){
                // Seek to beginning of stream to make sure all data is captured
                rs.Seek(0, SeekOrigin.Begin);
                byte[] buffer = new byte[0x8000];
                int count;

                // Write new image data to page 1
                while((count = rs.Read(buffer, 0, buffer.Length)) > 0){
                    writer.Write(buffer, 0, count);
                }
            }
        }
        
        // Delete page 2
        doc.DeletePage(2);
        
        // Save changes
        doc.Save();
    }
    finally
    {
        // Always release the document even if an error occurs
        doc.Unlock();
        doc.Dispose();
    }
}

I kind of slapped these together so you may need to make adjustments, but I tested both and they worked as expected so they will at least point you in the right direction.

NOTE: If you use Option 1, the annotations will show as being created by the Workflow user account. If you use Option 2, you're not actually changing the annotations so their original creator/metadata will remain intact.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.