You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gilad Denneboom <gi...@gmail.com> on 2012/03/11 22:32:15 UTC

Duplicating an existing link annotation

Hi all,

I'm trying to duplicate an existing link annotation (PDAnnotationLink) from
one page to others. I'm using this code:

    private static PDAnnotationLink duplicateLink(PDAnnotationLink link,
PDPage page) {
        PDAnnotationLink newLink = new PDAnnotationLink();
        newLink.setAction(link.getAction());
        newLink.setColour(link.getColour());
        newLink.setRectangle(link.getRectangle());
        newLink.setAppearance(link.getAppearance());
        newLink.setAppearanceStream(link.getAppearanceStream());
        newLink.setHighlightMode(link.getHighlightMode());
        newLink.setAnnotationFlags(link.getAnnotationFlags());
        newLink.setBorderStyle(link.getBorderStyle());
        newLink.setPage(page);
        return newLink;
    }

This works when I run the tool on a file I've created and added some links
to. HOWEVER, if I then run the result file through the same code, the
following Exception is thrown:
Exception in thread "main" java.lang.ClassCastException:
org.apache.pdfbox.cos.COSObject cannot be cast to
org.apache.pdfbox.cos.COSDictionary
    at
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getBorderStyle(PDAnnotationLink.java:131)

Since this is a RunTimeException I can't even catch it and my application
fails... Any help with this will be appreciated.
Am I creating the new link in an incorrect way?

Thanks in advance, Gilad.

Re: Duplicating an existing link annotation

Posted by Michel Onwordi <mo...@polytechnic.edu.na>.
Please stop sending me mail
On Wed, 14 Mar 2012 17:55:07 -0400
  "Sheila M. Morrissey" <Sh...@ithaka.org> wrote:
> I wonder if the PDBorderStyleDictionary didn't end up serialized as 
>an indirect object --
> Maybe something like the following would work:
> 
> PDBorderStyleDictionary bsd = null;
> COSObjectable cob = newLink.getBorderStyle();
> if (cob instanceof COSObject){
> 	COSObject cos = (COSObject)cob;
> 	COSBase base = cos.getObject();
> 	if (base instanceof PDBorderStyleDictionary){
> 		bsd = (PDBorderStyleDictionary) base;
> 	}
> }
> else if (cob instanceof PDBorderStyleDictionary){
> 	bsd = PDBorderStyleDictionary cob;
> }
> If (bsd != null){
>  // off you go
> }
> else {
>  // hmmm.. what happened to my dictionary?
> }
> 
> Sheila
> 
> 
> -----Original Message-----
>From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com] 
> Sent: Sunday, March 11, 2012 5:32 PM
> To: users@pdfbox.apache.org
> Subject: Duplicating an existing link annotation
> 
> Hi all,
> 
> I'm trying to duplicate an existing link annotation 
>(PDAnnotationLink) from
> one page to others. I'm using this code:
> 
>    private static PDAnnotationLink duplicateLink(PDAnnotationLink 
>link,
> PDPage page) {
>        PDAnnotationLink newLink = new PDAnnotationLink();
>        newLink.setAction(link.getAction());
>        newLink.setColour(link.getColour());
>        newLink.setRectangle(link.getRectangle());
>        newLink.setAppearance(link.getAppearance());
>        newLink.setAppearanceStream(link.getAppearanceStream());
>        newLink.setHighlightMode(link.getHighlightMode());
>        newLink.setAnnotationFlags(link.getAnnotationFlags());
>        newLink.setBorderStyle(link.getBorderStyle());
>        newLink.setPage(page);
>        return newLink;
>    }
> 
> This works when I run the tool on a file I've created and added some 
>links
> to. HOWEVER, if I then run the result file through the same code, 
>the
> following Exception is thrown:
> Exception in thread "main" java.lang.ClassCastException:
> org.apache.pdfbox.cos.COSObject cannot be cast to
> org.apache.pdfbox.cos.COSDictionary
>    at
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getBorderStyle(PDAnnotationLink.java:131)
> 
> Since this is a RunTimeException I can't even catch it and my 
>application
> fails... Any help with this will be appreciated.
> Am I creating the new link in an incorrect way?
> 
> Thanks in advance, Gilad.
____________________________________
Disclaimer: 
Everything in this email and its attachments relating to the official business of the Polytechnic of Namibia is proprietary to the Polytechnic of Namibia. If the email is used other than for official business of the Polytechnic of Namibia or the views and opinions expressed in the email are not authorised by the Polytechnic of Namibia, the views and opinions expressed are those of the individual sending the email.

The content of this email is confidential, legally privileged and protected by law. The person addressed in the email is the sole authorised recipient. Please notify the sender immediately if this email and its attachments have unintentionally reached you; do not read, copy, disseminate or use the content in any way and delete the email and any copies of it.

Whilst all reasonable precautions are taken to ensure the accuracy and integrity of the information, and that this email and its attachments are free from any virus, the Polytechnic of Namibia accepts no liability however arising or responsibility whatsoever in this regard, and in keeping with good computing practice, the scanning of files and attachments is advised. 


RE: Duplicating an existing link annotation

Posted by "Sheila M. Morrissey" <Sh...@ithaka.org>.
Reply at haste, repent at leisure-should look more like this:


PDAnnotationLink newLink = new PDAnnotationLink();
            PDBorderStyleDictionary bsd = null;
            Object cob = newLink.getBorderStyle();
            if (cob instanceof COSObject){
                   COSObject cos = (COSObject)cob;
                   COSBase base = cos.getObject();
                   if (base instanceof COSDictionary){
                     COSDictionary dic = (COSDictionary)base;
                     Set<COSName> keys = dic.keySet();
                     bsd = new PDBorderStyleDictionary(dic);

                     // or bsd = (PDBorderStyleDictionary) cob;
                   }
            }
            else if (cob instanceof PDBorderStyleDictionary){
                   bsd = (PDBorderStyleDictionary)cob;
            }
            if (bsd != null){
            // off you go
            }
            else {
            // hmmm.. what happened to my dictionary?
            }
I do think it might be a good idea to refactor the PDAnnotationLink.getBorderStyle() method so that when it retrieves the dictionary object for border style, it not assume it gets a direct object back - then, when we use the PDModel code, we'd know the lower level stuff has been handled -

smm

From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
Sent: Thursday, March 15, 2012 5:12 AM
To: Sheila M. Morrissey
Cc: users@pdfbox.apache.org
Subject: Re: Duplicating an existing link annotation

Hi Sheila,

Thanks for the input... However, this code doesn't compile.
This line:
if (base instanceof PDBorderStyleDictionary){
Produces the following compilation error:
Incompatible conditional operand types COSBase and PDBorderStyleDictionary

Any ideas on how this can be solved?

Thanks again, Gilad.
On Wed, Mar 14, 2012 at 10:55 PM, Sheila M. Morrissey <Sh...@ithaka.org>> wrote:
I wonder if the PDBorderStyleDictionary didn't end up serialized as an indirect object --
Maybe something like the following would work:

PDBorderStyleDictionary bsd = null;
COSObjectable cob = newLink.getBorderStyle();
if (cob instanceof COSObject){
       COSObject cos = (COSObject)cob;
       COSBase base = cos.getObject();
       if (base instanceof PDBorderStyleDictionary){
               bsd = (PDBorderStyleDictionary) base;
       }
}
else if (cob instanceof PDBorderStyleDictionary){
       bsd = PDBorderStyleDictionary cob;
}
If (bsd != null){
 // off you go
}
else {
 // hmmm.. what happened to my dictionary?
}

Sheila


-----Original Message-----
From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com<ma...@gmail.com>]
Sent: Sunday, March 11, 2012 5:32 PM
To: users@pdfbox.apache.org<ma...@pdfbox.apache.org>
Subject: Duplicating an existing link annotation

Hi all,

I'm trying to duplicate an existing link annotation (PDAnnotationLink) from
one page to others. I'm using this code:

   private static PDAnnotationLink duplicateLink(PDAnnotationLink link,
PDPage page) {
       PDAnnotationLink newLink = new PDAnnotationLink();
       newLink.setAction(link.getAction());
       newLink.setColour(link.getColour());
       newLink.setRectangle(link.getRectangle());
       newLink.setAppearance(link.getAppearance());
       newLink.setAppearanceStream(link.getAppearanceStream());
       newLink.setHighlightMode(link.getHighlightMode());
       newLink.setAnnotationFlags(link.getAnnotationFlags());
       newLink.setBorderStyle(link.getBorderStyle());
       newLink.setPage(page);
       return newLink;
   }

This works when I run the tool on a file I've created and added some links
to. HOWEVER, if I then run the result file through the same code, the
following Exception is thrown:
Exception in thread "main" java.lang.ClassCastException:
org.apache.pdfbox.cos.COSObject cannot be cast to
org.apache.pdfbox.cos.COSDictionary
   at
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getBorderStyle(PDAnnotationLink.java:131)

Since this is a RunTimeException I can't even catch it and my application
fails... Any help with this will be appreciated.
Am I creating the new link in an incorrect way?

Thanks in advance, Gilad.


Re: Duplicating an existing link annotation

Posted by Gilad Denneboom <gi...@gmail.com>.
Hi Sheila,

Thanks for the input... However, this code doesn't compile.
This line:
if (base instanceof PDBorderStyleDictionary){
Produces the following compilation error:
Incompatible conditional operand types COSBase and PDBorderStyleDictionary

Any ideas on how this can be solved?

Thanks again, Gilad.

On Wed, Mar 14, 2012 at 10:55 PM, Sheila M. Morrissey <
Sheila.Morrissey@ithaka.org> wrote:

> I wonder if the PDBorderStyleDictionary didn't end up serialized as an
> indirect object --
> Maybe something like the following would work:
>
> PDBorderStyleDictionary bsd = null;
> COSObjectable cob = newLink.getBorderStyle();
> if (cob instanceof COSObject){
>        COSObject cos = (COSObject)cob;
>        COSBase base = cos.getObject();
>        if (base instanceof PDBorderStyleDictionary){
>                bsd = (PDBorderStyleDictionary) base;
>        }
> }
> else if (cob instanceof PDBorderStyleDictionary){
>        bsd = PDBorderStyleDictionary cob;
> }
> If (bsd != null){
>  // off you go
> }
> else {
>  // hmmm.. what happened to my dictionary?
> }
>
> Sheila
>
>
> -----Original Message-----
> From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
> Sent: Sunday, March 11, 2012 5:32 PM
> To: users@pdfbox.apache.org
> Subject: Duplicating an existing link annotation
>
> Hi all,
>
> I'm trying to duplicate an existing link annotation (PDAnnotationLink) from
> one page to others. I'm using this code:
>
>    private static PDAnnotationLink duplicateLink(PDAnnotationLink link,
> PDPage page) {
>        PDAnnotationLink newLink = new PDAnnotationLink();
>        newLink.setAction(link.getAction());
>        newLink.setColour(link.getColour());
>        newLink.setRectangle(link.getRectangle());
>        newLink.setAppearance(link.getAppearance());
>        newLink.setAppearanceStream(link.getAppearanceStream());
>        newLink.setHighlightMode(link.getHighlightMode());
>        newLink.setAnnotationFlags(link.getAnnotationFlags());
>        newLink.setBorderStyle(link.getBorderStyle());
>        newLink.setPage(page);
>        return newLink;
>    }
>
> This works when I run the tool on a file I've created and added some links
> to. HOWEVER, if I then run the result file through the same code, the
> following Exception is thrown:
> Exception in thread "main" java.lang.ClassCastException:
> org.apache.pdfbox.cos.COSObject cannot be cast to
> org.apache.pdfbox.cos.COSDictionary
>    at
>
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getBorderStyle(PDAnnotationLink.java:131)
>
> Since this is a RunTimeException I can't even catch it and my application
> fails... Any help with this will be appreciated.
> Am I creating the new link in an incorrect way?
>
> Thanks in advance, Gilad.
>

RE: Duplicating an existing link annotation

Posted by "Sheila M. Morrissey" <Sh...@ithaka.org>.
I wonder if the PDBorderStyleDictionary didn't end up serialized as an indirect object --
Maybe something like the following would work:

PDBorderStyleDictionary bsd = null;
COSObjectable cob = newLink.getBorderStyle();
if (cob instanceof COSObject){
	COSObject cos = (COSObject)cob;
	COSBase base = cos.getObject();
	if (base instanceof PDBorderStyleDictionary){
		bsd = (PDBorderStyleDictionary) base;
	}
}
else if (cob instanceof PDBorderStyleDictionary){
	bsd = PDBorderStyleDictionary cob;
}
If (bsd != null){
  // off you go
}
else {
  // hmmm.. what happened to my dictionary?
}

Sheila


-----Original Message-----
From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com] 
Sent: Sunday, March 11, 2012 5:32 PM
To: users@pdfbox.apache.org
Subject: Duplicating an existing link annotation

Hi all,

I'm trying to duplicate an existing link annotation (PDAnnotationLink) from
one page to others. I'm using this code:

    private static PDAnnotationLink duplicateLink(PDAnnotationLink link,
PDPage page) {
        PDAnnotationLink newLink = new PDAnnotationLink();
        newLink.setAction(link.getAction());
        newLink.setColour(link.getColour());
        newLink.setRectangle(link.getRectangle());
        newLink.setAppearance(link.getAppearance());
        newLink.setAppearanceStream(link.getAppearanceStream());
        newLink.setHighlightMode(link.getHighlightMode());
        newLink.setAnnotationFlags(link.getAnnotationFlags());
        newLink.setBorderStyle(link.getBorderStyle());
        newLink.setPage(page);
        return newLink;
    }

This works when I run the tool on a file I've created and added some links
to. HOWEVER, if I then run the result file through the same code, the
following Exception is thrown:
Exception in thread "main" java.lang.ClassCastException:
org.apache.pdfbox.cos.COSObject cannot be cast to
org.apache.pdfbox.cos.COSDictionary
    at
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink.getBorderStyle(PDAnnotationLink.java:131)

Since this is a RunTimeException I can't even catch it and my application
fails... Any help with this will be appreciated.
Am I creating the new link in an incorrect way?

Thanks in advance, Gilad.