Hmm... I didn't notice this question was sent to me personally. I'm forwarding it to the list so that it's archived.
1T3XT info wrote: > [email protected] wrote: >> I have finally tracked down the reason for the exception. > > Thanks for providing the detailed information. > I think you're on the right track. > >> I build a vector array of images with these parameters prior >> to the creation of the document. > > These are specific instances of the com.lowagie.text.Image class. > > > Each image can have it's own unique annotation and can be scaled, > > rotated etc. > > OK. > >> My PDF class extends PdfPageEventHelper and outputs the images >> during the onStartpage, > > onStartPage: that's a bad idea; it's better to add them all > in onEndPage. > >> onendpage events by reading the vector array and adding the images >> into the PdfContentByte direct content. > >> // Create new instance >> iPDFImage img = new iPDFImage(); > > This is a new instance, but it's empty. > >> // Initialise counter >> int x = 0; >> for (int i = 0; i < imgvector.size(); i++) { >> // Retrieve vector object >> img = (iPDFImage) imgvector.get(i); > > The newly created instance is replaced by an existing instance. > >> // Set position >> >> img.pdfimage.setAbsolutePosition(Float.valueOf(img.getImageX().trim()).floatValue(), >> >> document.getPageSize().getHeight() - >> Float.valueOf(img.getImageY().trim()).floatValue()); >> // Set annotation >> boolean urlnotblank = >> StringUtils.isNotBlank(img.getUrl().trim()); >> // Image provided >> if (urlnotblank == true) { >> img.pdfimage.setAnnotation(new Annotation(0, >> 0, 0, 0, img.getUrl().trim())); > > Every time the image is added, you add a new Annotation object. > On the same image instance retrieved from the Vector... > Maybe that's what's causing the problem. > >> } >> >> // Add image >> if (img.getUnder().equals(Yes)) { >> cbu.addImage(img.pdfimage); >> } >> else { >> cb.addImage(img.pdfimage); >> } >> // Increment vector element >> x = x + 1; >> } >> } >> catch(Exception e) { >> throw new ExceptionConverter(e); >> } >> } >> >> >> The line which sets the image annotation is shown below: >> >> img.pdfimage.setAnnotation(new Annotation(0, 0, 0, 0, >> img.getUrl().trim())); >> >> The position fields are set to 0,0,0,0 to pick up the necessary fields >> from the image. >> >> If I attach an annotation to an image that appears only once in the >> document then everything works ok. If I attempt to annotate an image >> that appears several times in the same document then the exception >> occurs. I find this a little odd since each image is a different >> instance of class iPDFImage. > > Are you sure? Aren't you reusing the same image instances from the > vector? (I'm not sure because I don't see your complete code, but I > think you're reusing Image instances.) > >> I can only presume that the annotation is 'shared'. > > You may be violating something by adding a new annotation to the > same image instance. > >> I presume that what is happening is that because 0,0,0,0 is specified >> and the image appears on several pages, that the program is getting >> confused and issuing an exception - hence the sharing violation message. > > I don't know for sure, but I think your assumption is very close > to what actually happens. > >> I have not tried specifying these values based upon the image class >> variables as yet because I have another question. > > That's why I didn't talk about a solution yet ;-) > >> The image mentioned above that works can be rotated to any angle - >> but when I hover the mouse over the image, it does not recognise >> that the image has been rotated and instead treats the rotated image >> as a square. > > That's a known limitation. > >> Is there a way to specify a matrix mask for annotation so that the >> selection is within the bounds of the selected image? > > Class Annotation is too limited for what you want; you'll have > to switch to PdfAnnotation. This means that you'll have to add > the Image without an annotation, and then add the PdfAnnotation > separately. > > Annotations are defined using a rectangle (Rect), but for link > annotations (assuming that you want the images to be clickable), > you can define QuadPoints. > > Let me copy/paste a fragment of the PDF Reference: > > QuadPoints array (Optional; PDF 1.6) An array of 8 × n numbers > specifying the coordinates of nquadrilaterals in default user space that > comprise the region in which the link should be activated. The > coordinates for each quadrilateral are given in the order > x1 y1 x2 y2 x3 y3 x4 y4 > specifying the four vertices of the quadrilateral in counterclockwise > order. For orientation purposes, such as when applying an underline > border style, the bottom of a quadrilateral is the line formed by (x1 , > y1) and (x2 , y2). > If this entry is not present or the viewer application does not > recognize it, the region specified by the Rect entry should be used. > QuadPoints should be ignored if any coordinate in the array lies outside > the region specified by Rect. > > When you have a PdfAnnotation object in iText, with a specific Rect > (calculated based on the position and dimension of the Image), you > need to add the quadpoints like this: > > PdfArray array = new PdfArray(); > for (int k = 0; k < quadPoints.length; ++k) > array.add(new PdfNumber(quadPoints[k])); > annot.put(PdfName.QUADPOINTS, array); > > Finally, let me quote from "iText in Action": "A rotated image often > needs more space. In this case, the methods to return the plain > width/height still give you the dimensions of the image itself, > but the scaled width/height methods return the dimensions of the > rectangle that is needed to display the image." > > In other words: you'll need getScaledWidth() and getScaledHeight() > to define the outer rectangle, and you'll need to do some math > using the rotation angle to define the quadpoints inside this > rectangle. -- This answer is provided by 1T3XT BVBA http://www.1t3xt.com/ - http://www.1t3xt.info ------------------------------------------------------------------------------ OpenSolaris 2009.06 is a cutting edge operating system for enterprises looking to deploy the next generation of Solaris that includes the latest innovations from Sun and the OpenSource community. Download a copy and enjoy capabilities such as Networking, Storage and Virtualization. Go to: http://p.sf.net/sfu/opensolaris-get _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
