use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class AnnotatedImage method main.
/**
* Adds some annotated images to a PDF file.
* @param args no arguments needed
*/
public static void main(String[] args) {
System.out.println("images and annotations");
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// step 2:
// we create a writer that listens to the document
PdfWriter.getInstance(document, new FileOutputStream("annotated_images.pdf"));
// step 3: we open the document
document.open();
// step 4: we add some content
Image jpeg = Image.getInstance("otsoe.jpg");
jpeg.setAnnotation(new Annotation("picture", "This is my dog", 0, 0, 0, 0));
jpeg.setAbsolutePosition(100f, 550f);
document.add(jpeg);
Image wmf = Image.getInstance("iText.wmf");
wmf.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
wmf.setAbsolutePosition(100f, 200f);
document.add(wmf);
} catch (Exception de) {
de.printStackTrace();
}
// step 5: we close the document
document.close();
}
use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class PdfContentByte method addImage.
/**
* Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>
* is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)
* use addImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline.
* @param image the <CODE>Image</CODE> object
* @param a an element of the transformation matrix
* @param b an element of the transformation matrix
* @param c an element of the transformation matrix
* @param d an element of the transformation matrix
* @param e an element of the transformation matrix
* @param f an element of the transformation matrix
* @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise
* @throws DocumentException on error
*/
public void addImage(Image image, float a, float b, float c, float d, float e, float f, boolean inlineImage) throws DocumentException {
try {
if (image.getLayer() != null)
beginLayer(image.getLayer());
if (image.isImgTemplate()) {
writer.addDirectImageSimple(image);
PdfTemplate template = image.getTemplateData();
float w = template.getWidth();
float h = template.getHeight();
addTemplate(template, a / w, b / w, c / h, d / h, e, f);
} else {
content.append("q ");
content.append(a).append(' ');
content.append(b).append(' ');
content.append(c).append(' ');
content.append(d).append(' ');
content.append(e).append(' ');
content.append(f).append(" cm");
if (inlineImage) {
content.append("\nBI\n");
PdfImage pimage = new PdfImage(image, "", null);
if (image instanceof ImgJBIG2) {
byte[] globals = ((ImgJBIG2) image).getGlobalBytes();
if (globals != null) {
PdfDictionary decodeparms = new PdfDictionary();
decodeparms.put(PdfName.JBIG2GLOBALS, writer.getReferenceJBIG2Globals(globals));
pimage.put(PdfName.DECODEPARMS, decodeparms);
}
}
for (PdfName key : pimage.getKeys()) {
PdfObject value = pimage.get(key);
String s = abrev.get(key);
if (s == null)
continue;
content.append(s);
boolean check = true;
if (key.equals(PdfName.COLORSPACE) && value.isArray()) {
PdfArray ar = (PdfArray) value;
if (ar.size() == 4 && PdfName.INDEXED.equals(ar.getAsName(0)) && ar.getPdfObject(1).isName() && ar.getPdfObject(2).isNumber() && ar.getPdfObject(3).isString()) {
check = false;
}
}
if (check && key.equals(PdfName.COLORSPACE) && !value.isName()) {
PdfName cs = writer.getColorspaceName();
PageResources prs = getPageResources();
prs.addColor(cs, writer.addToBody(value).getIndirectReference());
value = cs;
}
value.toPdf(null, content);
content.append('\n');
}
content.append("ID\n");
pimage.writeContent(content);
content.append("\nEI\nQ").append_i(separator);
} else {
PdfName name;
PageResources prs = getPageResources();
Image maskImage = image.getImageMask();
if (maskImage != null) {
name = writer.addDirectImageSimple(maskImage);
prs.addXObject(name, writer.getImageReference(name));
}
name = writer.addDirectImageSimple(image);
name = prs.addXObject(name, writer.getImageReference(name));
content.append(' ').append(name.getBytes()).append(" Do Q").append_i(separator);
}
}
if (image.hasBorders()) {
saveState();
float w = image.getWidth();
float h = image.getHeight();
concatCTM(a / w, b / w, c / h, d / h, e, f);
rectangle(image);
restoreState();
}
if (image.getLayer() != null)
endLayer();
Annotation annot = image.getAnnotation();
if (annot == null)
return;
float[] r = new float[unitRect.length];
for (int k = 0; k < unitRect.length; k += 2) {
r[k] = a * unitRect[k] + c * unitRect[k + 1] + e;
r[k + 1] = b * unitRect[k] + d * unitRect[k + 1] + f;
}
float llx = r[0];
float lly = r[1];
float urx = llx;
float ury = lly;
for (int k = 2; k < r.length; k += 2) {
llx = Math.min(llx, r[k]);
lly = Math.min(lly, r[k + 1]);
urx = Math.max(urx, r[k]);
ury = Math.max(ury, r[k + 1]);
}
annot = new Annotation(annot);
annot.setDimensions(llx, lly, urx, ury);
PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, new Rectangle(llx, lly, urx, ury));
if (an == null)
return;
addAnnotation(an);
} catch (Exception ee) {
throw new DocumentException(ee);
}
}
use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class SAXiTextHandler method addImage.
protected void addImage(Image img) throws EmptyStackException {
// if there is an element on the stack...
Element current = stack.pop();
// added directly
if (current instanceof Section || current instanceof Cell) {
((TextElementArray) current).add(img);
stack.push(current);
} else // ... if it is a Phrase, we have to wrap the Image in a new Chunk
if (current instanceof Phrase) {
((TextElementArray) current).add(new Chunk(img, 0, 0));
stack.push(current);
} else // ...if not, we need to to a lot of stuff
{
Stack<Element> newStack = new Stack<>();
while (!(current instanceof Section || current instanceof Cell)) {
newStack.push(current);
if (current instanceof Anchor) {
img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference()));
}
current = stack.pop();
}
((TextElementArray) current).add(img);
stack.push(current);
while (!newStack.empty()) {
stack.push(newStack.pop());
}
}
}
use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class SAXiTextHandler method handleStartingTags.
/**
* This method deals with the starting tags.
*
* @param name the name of the tag
* @param attributes the list of attributes
*/
public void handleStartingTags(String name, Properties attributes) {
if (ignore || ElementTags.IGNORE.equals(name)) {
ignore = true;
return;
}
// maybe there is some meaningful data that wasn't between tags
if (currentChunk != null && isNotBlank(currentChunk.getContent())) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
} catch (EmptyStackException ese) {
if (bf == null) {
current = new Paragraph("", new Font());
} else {
current = new Paragraph("", new Font(this.bf));
}
}
current.add(currentChunk);
stack.push(current);
currentChunk = null;
}
// chunks
if (ElementTags.CHUNK.equals(name)) {
currentChunk = ElementFactory.getChunk(attributes);
if (bf != null) {
currentChunk.setFont(new Font(this.bf));
}
return;
}
// symbols
if (ElementTags.ENTITY.equals(name)) {
Font f = new Font();
if (currentChunk != null) {
handleEndingTags(ElementTags.CHUNK);
f = currentChunk.getFont();
}
currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID), f);
return;
}
// phrases
if (ElementTags.PHRASE.equals(name)) {
stack.push(ElementFactory.getPhrase(attributes));
return;
}
// anchors
if (ElementTags.ANCHOR.equals(name)) {
stack.push(ElementFactory.getAnchor(attributes));
return;
}
// paragraphs and titles
if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
stack.push(ElementFactory.getParagraph(attributes));
return;
}
// lists
if (ElementTags.LIST.equals(name)) {
stack.push(ElementFactory.getList(attributes));
return;
}
// listitems
if (ElementTags.LISTITEM.equals(name)) {
stack.push(ElementFactory.getListItem(attributes));
return;
}
// cells
if (ElementTags.CELL.equals(name)) {
stack.push(ElementFactory.getCell(attributes));
return;
}
// tables
if (ElementTags.TABLE.equals(name)) {
Table table = ElementFactory.getTable(attributes);
float[] widths = table.getProportionalWidths();
for (int i = 0; i < widths.length; i++) {
if (widths[i] == 0) {
widths[i] = 100.0f / widths.length;
}
}
try {
table.setWidths(widths);
} catch (BadElementException bee) {
// this shouldn't happen
throw new ExceptionConverter(bee);
}
stack.push(table);
return;
}
// sections
if (ElementTags.SECTION.equals(name)) {
Element previous = stack.pop();
Section section;
try {
section = ElementFactory.getSection((Section) previous, attributes);
} catch (ClassCastException cce) {
throw new ExceptionConverter(cce);
}
stack.push(previous);
stack.push(section);
return;
}
// chapters
if (ElementTags.CHAPTER.equals(name)) {
stack.push(ElementFactory.getChapter(attributes));
return;
}
// images
if (ElementTags.IMAGE.equals(name)) {
try {
Image img = ElementFactory.getImage(attributes);
try {
addImage(img);
return;
} catch (EmptyStackException ese) {
// to the document
try {
document.add(img);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
// annotations
if (ElementTags.ANNOTATION.equals(name)) {
Annotation annotation = ElementFactory.getAnnotation(attributes);
TextElementArray current;
try {
try {
current = (TextElementArray) stack.pop();
try {
current.add(annotation);
} catch (Exception e) {
document.add(annotation);
}
stack.push(current);
} catch (EmptyStackException ese) {
document.add(annotation);
}
return;
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
// newlines
if (isNewline(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
current.add(Chunk.NEWLINE);
stack.push(current);
} catch (EmptyStackException ese) {
if (currentChunk == null) {
try {
document.add(Chunk.NEWLINE);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else {
currentChunk.append("\n");
}
}
return;
}
// newpage
if (isNewpage(name)) {
TextElementArray current;
try {
current = (TextElementArray) stack.pop();
Chunk newPage = new Chunk("");
newPage.setNewPage();
if (bf != null) {
newPage.setFont(new Font(this.bf));
}
current.add(newPage);
stack.push(current);
} catch (EmptyStackException ese) {
document.newPage();
}
return;
}
if (ElementTags.HORIZONTALRULE.equals(name)) {
TextElementArray current;
LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
try {
current = (TextElementArray) stack.pop();
current.add(hr);
stack.push(current);
} catch (EmptyStackException ese) {
try {
document.add(hr);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
return;
}
// documentroot
if (isDocumentRoot(name)) {
String key;
String value;
// pagesize and orientation specific code suggested by Samuel Gabriel
// Updated by Ricardo Coutinho. Only use if set in html!
Rectangle pageSize = null;
String orientation = null;
for (Object o : attributes.keySet()) {
key = (String) o;
value = attributes.getProperty(key);
try {
// margin specific code suggested by Reza Nasiri
if (ElementTags.LEFT.equalsIgnoreCase(key))
leftMargin = Float.parseFloat(value + "f");
if (ElementTags.RIGHT.equalsIgnoreCase(key))
rightMargin = Float.parseFloat(value + "f");
if (ElementTags.TOP.equalsIgnoreCase(key))
topMargin = Float.parseFloat(value + "f");
if (ElementTags.BOTTOM.equalsIgnoreCase(key))
bottomMargin = Float.parseFloat(value + "f");
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
if (ElementTags.PAGE_SIZE.equals(key)) {
try {
Field pageSizeField = PageSize.class.getField(value);
pageSize = (Rectangle) pageSizeField.get(null);
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else if (ElementTags.ORIENTATION.equals(key)) {
try {
if ("landscape".equals(value)) {
orientation = "landscape";
}
} catch (Exception ex) {
throw new ExceptionConverter(ex);
}
} else {
try {
document.add(new Meta(key, value));
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
if (pageSize != null) {
if ("landscape".equals(orientation)) {
pageSize = pageSize.rotate();
}
document.setPageSize(pageSize);
}
document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
if (controlOpenClose)
document.open();
}
}
use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class SimplePdfTest method testSimplePdf.
@Test
void testSimplePdf() throws Exception {
// create document
Document document = PdfTestBase.createTempPdf("testSimplePdf.pdf");
try {
// new page with a rectangle
document.open();
document.newPage();
Annotation ann = new Annotation("Title", "Text");
Rectangle rect = new Rectangle(100, 100);
document.add(ann);
document.add(rect);
} finally {
// close document
if (document != null)
document.close();
}
}
Aggregations