use of com.lowagie.text.Annotation in project itext2 by albfernandez.
the class AnnotatedImageTest method main.
/**
* Adds some annotated images to a PDF file.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// step 2:
// we create a writer that listens to the document
PdfWriter.getInstance(document, PdfTestBase.getOutputStream("annotated_images.pdf"));
// step 3: we open the document
document.open();
// step 4: we add some content
Image jpeg = Image.getInstance(PdfTestBase.RESOURCES_DIR + "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(PdfTestBase.RESOURCES_DIR + "iText.wmf");
wmf.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
wmf.setAbsolutePosition(100f, 200f);
document.add(wmf);
// step 5: we close the document
document.close();
}
use of com.lowagie.text.Annotation in project itext2 by albfernandez.
the class RtfTest method main.
/**
* Creates 1 RTF file.
*/
@Test
public void main() throws Exception {
Document doc = new Document();
RtfWriter2 writer2 = RtfWriter2.getInstance(doc, PdfTestBase.getOutputStream("testNew.rtf"));
writer2.setAutogenerateTOCEntries(true);
Table headerTable = new Table(3);
headerTable.addCell("Test Cell 1");
headerTable.addCell("Test Cell 2");
headerTable.addCell("Test Cell 3");
HeaderFooter header = new RtfHeaderFooter(headerTable);
RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is the footer on the title page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_FIRST_PAGE);
footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is a left side page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_LEFT_PAGES);
footer.setHeaderFooter(new RtfHeaderFooter(new Phrase("This is a right side page")), com.lowagie.text.rtf.headerfooter.RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
doc.setHeader(header);
doc.setFooter(footer);
doc.open();
Paragraph p = new Paragraph();
p.add(new RtfTableOfContents("UPDATE ME!"));
doc.add(p);
p = new Paragraph("", new RtfFont("Staccato222 BT"));
p.add(new Chunk("Hello! "));
p.add(new Chunk("How do you do?"));
doc.add(p);
p.setAlignment(Element.ALIGN_RIGHT);
doc.add(p);
Anchor a = new Anchor("http://www.uni-klu.ac.at");
a.setReference("http://www.uni-klu.ac.at");
doc.add(a);
Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR + "pngnow.png");
doc.add(new Chunk(img, 0, 0));
doc.add(new Annotation("Mark", "This works!"));
Chunk c = new Chunk("");
c.setNewPage();
doc.add(c);
List subList = new List(true, 40);
subList.add(new ListItem("Sub list 1"));
subList.add(new ListItem("Sub list 2"));
List list = new List(true, 20);
list.add(new ListItem("Test line 1"));
list.add(new ListItem("Test line 2 - This is a really long test line to test that linebreaks are working the way they are supposed to work so a really really long line of drivel is required"));
list.add(subList);
list.add(new ListItem("Test line 3 - \t\u20ac\t 60,-"));
doc.add(list);
list = new List(false, 20);
list.add(new ListItem("Bullet"));
list.add(new ListItem("Another one"));
doc.add(list);
doc.newPage();
Chapter chapter = new Chapter(new Paragraph("This is a Chapter"), 1);
chapter.add(new Paragraph("This is some text that belongs to this chapter."));
chapter.add(new Paragraph("A second paragraph. What a surprise."));
Section section = chapter.addSection(new Paragraph("This is a subsection"));
section.add(new Paragraph("Text in the subsection."));
doc.add(chapter);
com.lowagie.text.rtf.field.RtfTOCEntry rtfTOC = new com.lowagie.text.rtf.field.RtfTOCEntry("Table Test");
doc.add(rtfTOC);
Table table = new Table(3);
table.setPadding(2);
table.setAlignment(Element.ALIGN_LEFT);
table.setSpacing(2);
Cell emptyCell = new Cell("");
Cell cellLeft = new Cell("Left Alignment");
cellLeft.setHorizontalAlignment(Element.ALIGN_LEFT);
Cell cellCenter = new Cell("Center Alignment");
cellCenter.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cellRight = new Cell("Right Alignment");
cellRight.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cellLeft);
table.addCell(cellCenter);
table.addCell(cellRight);
Cell cellSpanHoriz = new Cell("This Cell spans two columns");
cellSpanHoriz.setColspan(2);
table.addCell(cellSpanHoriz);
table.addCell(emptyCell);
Cell cellSpanVert = new Cell("This Cell spans two rows");
cellSpanVert.setRowspan(2);
table.addCell(emptyCell);
table.addCell(cellSpanVert);
table.addCell(emptyCell);
table.addCell(emptyCell);
table.addCell(emptyCell);
Cell cellSpanHorizVert = new Cell("This Cell spans both two columns and two rows");
cellSpanHorizVert.setColspan(2);
cellSpanHorizVert.setRowspan(2);
table.addCell(emptyCell);
table.addCell(cellSpanHorizVert);
table.addCell(emptyCell);
RtfCell cellDotted = new RtfCell("Dotted border");
cellDotted.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_DOTTED, 1, new Color(0, 0, 0)));
RtfCell cellEmbossed = new RtfCell("Embossed border");
cellEmbossed.setBorders(new RtfBorderGroup(Rectangle.BOX, RtfBorder.BORDER_EMBOSS, 1, new Color(0, 0, 0)));
RtfCell cellNoBorder = new RtfCell("No border");
cellNoBorder.setBorders(new RtfBorderGroup());
table.addCell(cellDotted);
table.addCell(cellEmbossed);
table.addCell(cellNoBorder);
doc.add(table);
for (int i = 0; i < 300; i++) {
doc.add(new Paragraph("Dummy line to get multi-page document. Line " + (i + 1)));
}
doc.close();
}
use of com.lowagie.text.Annotation in project itext2 by albfernandez.
the class SimplePdfTest method testSimplePdf.
@Test
public void testSimplePdf() throws FileNotFoundException, DocumentException {
// create document
Document document = PdfTestBase.createPdf("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();
}
}
use of com.lowagie.text.Annotation in project itext2 by albfernandez.
the class SAXiTextHandler method addImage.
protected void addImage(Image img) throws EmptyStackException {
// if there is an element on the stack...
Object current = stack.pop();
// added directly
if (current instanceof Chapter || current instanceof Section || current instanceof Cell) {
((TextElementArray) current).add(img);
stack.push(current);
return;
} else // ...if not, we need to to a lot of stuff
{
Stack newStack = new Stack();
while (!(current instanceof Chapter || 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());
}
return;
}
}
use of com.lowagie.text.Annotation in project OpenPDF by LibrePDF.
the class PdfDocument method add.
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @param element the element to add
* @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean add(Element element) throws DocumentException {
if (writer != null && writer.isPaused()) {
return false;
}
try {
switch(element.type()) {
// Information (headers)
case Element.HEADER:
info.addkey(((Meta) element).getName(), ((Meta) element).getContent());
break;
case Element.TITLE:
info.addTitle(((Meta) element).getContent());
break;
case Element.SUBJECT:
info.addSubject(((Meta) element).getContent());
break;
case Element.KEYWORDS:
info.addKeywords(((Meta) element).getContent());
break;
case Element.AUTHOR:
info.addAuthor(((Meta) element).getContent());
break;
case Element.CREATOR:
info.addCreator(((Meta) element).getContent());
break;
case Element.PRODUCER:
info.addProducer(((Meta) element).getContent());
break;
case Element.CREATIONDATE:
// you can not set the creation date, only reset it
info.addCreationDate();
break;
// content (text)
case Element.CHUNK:
{
// if there isn't a current line available, we make one
if (line == null) {
carriageReturn();
}
// we cast the element to a chunk
PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction);
// we try to add the chunk to the line, until we succeed
{
PdfChunk overflow;
while ((overflow = line.add(chunk)) != null) {
carriageReturn();
chunk = overflow;
chunk.trimFirstSpace();
}
}
pageEmpty = false;
if (chunk.isAttribute(Chunk.NEWPAGE)) {
newPage();
}
break;
}
case Element.ANCHOR:
{
leadingCount++;
Anchor anchor = (Anchor) element;
String url = anchor.getReference();
leading = anchor.getLeading();
if (url != null) {
anchorAction = new PdfAction(url);
}
// we process the element
element.process(this);
anchorAction = null;
leadingCount--;
break;
}
case Element.ANNOTATION:
{
if (line == null) {
carriageReturn();
}
Annotation annot = (Annotation) element;
Rectangle rect = new Rectangle(0, 0);
if (line != null)
rect = new Rectangle(annot.llx(indentRight() - line.widthLeft()), annot.ury(indentTop() - currentHeight - 20), annot.urx(indentRight() - line.widthLeft() + 20), annot.lly(indentTop() - currentHeight));
PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, rect);
annotationsImp.addPlainAnnotation(an);
pageEmpty = false;
break;
}
case Element.PHRASE:
{
leadingCount++;
// we cast the element to a phrase and set the leading of the document
leading = ((Phrase) element).getLeading();
// we process the element
element.process(this);
leadingCount--;
break;
}
case Element.PARAGRAPH:
{
leadingCount++;
// we cast the element to a paragraph
Paragraph paragraph = (Paragraph) element;
addSpacing(paragraph.getSpacingBefore(), leading, paragraph.getFont());
// we adjust the parameters of the document
alignment = paragraph.getAlignment();
leading = paragraph.getTotalLeading();
carriageReturn();
// we don't want to make orphans/widows
if (currentHeight + line.height() + leading > indentTop() - indentBottom()) {
newPage();
}
indentation.indentLeft += paragraph.getIndentationLeft();
indentation.indentRight += paragraph.getIndentationRight();
carriageReturn();
PdfPageEvent pageEvent = writer.getPageEvent();
if (pageEvent != null && !isSectionTitle)
pageEvent.onParagraph(writer, this, indentTop() - currentHeight);
// if a paragraph has to be kept together, we wrap it in a table object
if (paragraph.getKeepTogether()) {
carriageReturn();
// fixes bug with nested tables not shown
// Paragraph#getChunks() doesn't contain the nested table element
PdfPTable table = createInOneCell(paragraph);
indentation.indentLeft -= paragraph.getIndentationLeft();
indentation.indentRight -= paragraph.getIndentationRight();
this.add(table);
indentation.indentLeft += paragraph.getIndentationLeft();
indentation.indentRight += paragraph.getIndentationRight();
} else {
line.setExtraIndent(paragraph.getFirstLineIndent());
element.process(this);
carriageReturn();
addSpacing(paragraph.getSpacingAfter(), paragraph.getTotalLeading(), paragraph.getFont());
}
if (pageEvent != null && !isSectionTitle)
pageEvent.onParagraphEnd(writer, this, indentTop() - currentHeight);
alignment = Element.ALIGN_LEFT;
indentation.indentLeft -= paragraph.getIndentationLeft();
indentation.indentRight -= paragraph.getIndentationRight();
carriageReturn();
leadingCount--;
break;
}
case Element.SECTION:
case Element.CHAPTER:
{
// Chapters and Sections only differ in their constructor
// so we cast both to a Section
Section section = (Section) element;
PdfPageEvent pageEvent = writer.getPageEvent();
boolean hasTitle = section.isNotAddedYet() && section.getTitle() != null;
// if the section is a chapter, we begin a new page
if (section.isTriggerNewPage()) {
newPage();
}
if (hasTitle) {
float fith = indentTop() - currentHeight;
int rotation = pageSize.getRotation();
if (rotation == 90 || rotation == 180)
fith = pageSize.getHeight() - fith;
PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith);
while (currentOutline.level() >= section.getDepth()) {
currentOutline = currentOutline.parent();
}
PdfOutline outline = new PdfOutline(currentOutline, destination, section.getBookmarkTitle(), section.isBookmarkOpen());
currentOutline = outline;
}
// some values are set
carriageReturn();
indentation.sectionIndentLeft += section.getIndentationLeft();
indentation.sectionIndentRight += section.getIndentationRight();
if (section.isNotAddedYet() && pageEvent != null)
if (element.type() == Element.CHAPTER)
pageEvent.onChapter(writer, this, indentTop() - currentHeight, section.getTitle());
else
pageEvent.onSection(writer, this, indentTop() - currentHeight, section.getDepth(), section.getTitle());
// the title of the section (if any has to be printed)
if (hasTitle) {
isSectionTitle = true;
add(section.getTitle());
isSectionTitle = false;
}
indentation.sectionIndentLeft += section.getIndentation();
// we process the section
element.process(this);
flushLines();
// some parameters are set back to normal again
indentation.sectionIndentLeft -= (section.getIndentationLeft() + section.getIndentation());
indentation.sectionIndentRight -= section.getIndentationRight();
if (section.isComplete() && pageEvent != null)
if (element.type() == Element.CHAPTER)
pageEvent.onChapterEnd(writer, this, indentTop() - currentHeight);
else
pageEvent.onSectionEnd(writer, this, indentTop() - currentHeight);
break;
}
case Element.LIST:
{
// we cast the element to a List
List list = (List) element;
if (list.isAlignindent()) {
list.normalizeIndentation();
}
// we adjust the document
indentation.listIndentLeft += list.getIndentationLeft();
indentation.indentRight += list.getIndentationRight();
// we process the items in the list
element.process(this);
// some parameters are set back to normal again
indentation.listIndentLeft -= list.getIndentationLeft();
indentation.indentRight -= list.getIndentationRight();
carriageReturn();
break;
}
case Element.LISTITEM:
{
leadingCount++;
// we cast the element to a ListItem
ListItem listItem = (ListItem) element;
addSpacing(listItem.getSpacingBefore(), leading, listItem.getFont());
// we adjust the document
alignment = listItem.getAlignment();
indentation.listIndentLeft += listItem.getIndentationLeft();
indentation.indentRight += listItem.getIndentationRight();
leading = listItem.getTotalLeading();
carriageReturn();
// we prepare the current line to be able to show us the listsymbol
line.setListItem(listItem);
// we process the item
element.process(this);
addSpacing(listItem.getSpacingAfter(), listItem.getTotalLeading(), listItem.getFont());
// if the last line is justified, it should be aligned to the left
if (line.hasToBeJustified()) {
line.resetAlignment();
}
// some parameters are set back to normal again
carriageReturn();
indentation.listIndentLeft -= listItem.getIndentationLeft();
indentation.indentRight -= listItem.getIndentationRight();
leadingCount--;
break;
}
case Element.RECTANGLE:
{
Rectangle rectangle = (Rectangle) element;
graphics.rectangle(rectangle);
pageEmpty = false;
break;
}
case Element.PTABLE:
{
PdfPTable ptable = (PdfPTable) element;
if (ptable.size() <= ptable.getHeaderRows())
// nothing to do
break;
// before every table, we add a new line and flush all lines
ensureNewLine();
flushLines();
addPTable(ptable);
pageEmpty = false;
newLine();
break;
}
case Element.MULTI_COLUMN_TEXT:
{
ensureNewLine();
flushLines();
MultiColumnText multiText = (MultiColumnText) element;
float height = multiText.write(writer.getDirectContent(), this, indentTop() - currentHeight);
currentHeight += height;
text.moveText(0, -1f * height);
pageEmpty = false;
break;
}
case Element.TABLE:
{
if (element instanceof SimpleTable) {
PdfPTable ptable = ((SimpleTable) element).createPdfPTable();
if (ptable.size() <= ptable.getHeaderRows())
// nothing to do
break;
// before every table, we add a new line and flush all lines
ensureNewLine();
flushLines();
addPTable(ptable);
pageEmpty = false;
break;
} else if (element instanceof Table) {
try {
PdfPTable ptable = ((Table) element).createPdfPTable();
if (ptable.size() <= ptable.getHeaderRows())
// nothing to do
break;
// before every table, we add a new line and flush all lines
ensureNewLine();
flushLines();
addPTable(ptable);
pageEmpty = false;
break;
} catch (BadElementException bee) {
// constructing the PdfTable
// Before the table, add a blank line using offset or default leading
float offset = ((Table) element).getOffset();
if (Float.isNaN(offset))
offset = leading;
carriageReturn();
lines.add(new PdfLine(indentLeft(), indentRight(), alignment, offset));
currentHeight += offset;
addPdfTable((Table) element);
}
} else {
return false;
}
break;
}
case Element.JPEG:
case Element.JPEG2000:
case Element.JBIG2:
case Element.IMGRAW:
case Element.IMGTEMPLATE:
{
// carriageReturn(); suggestion by Marc Campforts
add((Image) element);
break;
}
case Element.YMARK:
{
DrawInterface zh = (DrawInterface) element;
zh.draw(graphics, indentLeft(), indentBottom(), indentRight(), indentTop(), indentTop() - currentHeight - (leadingCount > 0 ? leading : 0));
pageEmpty = false;
break;
}
case Element.MARKED:
{
MarkedObject mo;
if (element instanceof MarkedSection) {
mo = ((MarkedSection) element).getTitle();
if (mo != null) {
mo.process(this);
}
}
mo = (MarkedObject) element;
mo.process(this);
break;
}
default:
return false;
}
lastElementType = element.type();
return true;
} catch (Exception e) {
throw new DocumentException(e);
}
}
Aggregations