Anbringen einer Image-Annotation via API
Anbringen einer Image-Annotation via API
Beispiel zum Anbringen einer Image-Annotation, hierfür muss zuerst eine passende Image-Annotationsdefinition im Annotationsprofil angelegt werden:
Definition einer Image-Annotation im Annotationsprofil
<annotation-type name="Image" class="com.levigo.jadice.annotation.ImageAnnotation">
<renderer class="com.levigo.jadice.annotation.internal.renderer.ImageAnnotationRenderer" />
<wrangler class="com.levigo.jadice.swing.internal.annotation.wranglers.RectangleAnnotationWrangler" />
<labels>
<label locale="en">Image</label>
<label locale="de">Bild</label>
</labels>
</annotation-type>
Anbringen einer Image-Annotation
final Document document = <Jadice-Document Instanz>
// Seitenindex (0-basierend)
final int pageIndex = 0;
// PageSegment mit Dokumentinhalt holen auf die die Image-Anno aufgebracht werden soll
final AbstractPageSegment ps = (AbstractPageSegment) document.getPage(pageIndex).getPageSegment(DocumentLayer.DEFAULT);
// AnnotationPagesegment holen (wird neu erstellt, falls noch nicht vorhanden)
final AnnotationPageSegment aps = Annotations.getAnnotationPageSegment(document.getPage(pageIndex), true);
// Annotationsprofil laden (hier: Jadice-Annotationsformat)
final URL profileUrl = JadiceAnnotationReader.class.getResource("/default-annotation-profile.xml");
final AnnotationProfile annotationProfile = AnnotationProfile.load(profileUrl);
// Annotation anlegen
final ImageAnnotation a = new ImageAnnotation();
// Annotation einem Basistyp zuordnen (siehe default-annotation-profile.xml Datei)
a.setType(annotationProfile.getType("Image"));
try {
// Bild via Reader-Klasse laden
final Reader reader = new Reader();
reader.read(<Bilddatenstrom>);
// Seite holen und der Annotation setzen
final Page page = reader.getDocument().getPage(0);
a.setPage(page);
// Originale Bildgrösse setzen
final float w = (float) page.getSize().getWidth();
final float h = (float) page.getSize().getHeight();
a.setSize(w, h);
// Grösse kann nicht verändert werden
a.setAllowResize(false);
} catch (final Exception e) {
// Fehler beim Bild laden
e.printStackTrace();
throw new IllegalStateException("Ladefehler");
}
// Position
final float x = <x-Position in Pixel> * (Document.BASE_RESOLUTION /
ps.getResolution().getX());
final float y = <y-Position in Pixel> * (Document.BASE_RESOLUTION /
ps.getResolution().getX());
a.setLocation(x, y);
// Inhalt füllen -> damit das Bild mit der Maus bewegt werden kann
a.setFilled(true);
// Optional: schwarzer Rand setzen
//a.setStrokeColor(Color.BLACK);
//a.setLineWidth(3);
//a.setLinePainted(true);
// Anno der Seite hinzufügen
aps.addAnnotation(a);