com.lowagie.text.Section Java Examples

The following examples show how to use com.lowagie.text.Section. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Helper method to create a Chapter/Section object.
 * 
 * @param attributes
 */
private static void setSectionParameters(Section section, Properties attributes) {
	String value;
	value = attributes.getProperty(ElementTags.NUMBERDEPTH);
	if (value != null) {
		section.setNumberDepth(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.INDENT);
	if (value != null) {
		section.setIndentation(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
	if (value != null) {
		section.setIndentationLeft(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
	if (value != null) {
		section.setIndentationRight(Float.parseFloat(value + "f"));
	}
}
 
Example #2
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Helper method to create a Chapter/Section object.
 * @param attributes
 */
private static void setSectionParameters(Section section,
		Properties attributes) {
	String value;
	value = attributes.getProperty(ElementTags.NUMBERDEPTH);
	if (value != null) {
		section.setNumberDepth(Integer.parseInt(value));
	}
	value = attributes.getProperty(ElementTags.INDENT);
	if (value != null) {
		section.setIndentation(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
	if (value != null) {
		section.setIndentationLeft(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
	if (value != null) {
		section.setIndentationRight(Float.parseFloat(value + "f"));
	}
}
 
Example #3
Source File: SAXiTextHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
	// if there is an element on the stack...
	Object current = stack.pop();
	// ...and it's a Chapter or a Section, the Image can be
	// added directly
	if (current instanceof Chapter || current instanceof Section || current instanceof Cell) {
		((TextElementArray) current).add(img);
		stack.push(current);
		return;
	}
	// ...if not, we need to to a lot of stuff
	else {
		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;
	}
}
 
Example #4
Source File: HtmlWriter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Writes the HTML representation of a section.
 *
 * @param section the section to write
 * @param indent the indentation
 * @throws IOException
 */

protected void writeSection(Section section, int indent) throws IOException {
	if (section.getTitle() != null) {
		int depth = section.getDepth() - 1;
		if (depth > 5) {
			depth = 5;
		}
		Properties styleAttributes = new Properties();
		if (section.getTitle().hasLeading()) {
			styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, section.getTitle().getTotalLeading() + "pt");
		}
		// start tag
		addTabs(indent);
		writeStart(HtmlTags.H[depth]);
		write(section.getTitle().getFont(), styleAttributes);
		String alignment = HtmlEncoder.getAlignment(section.getTitle().getAlignment());
		if (!"".equals(alignment)) {
			write(HtmlTags.ALIGN, alignment);
		}
		writeMarkupAttributes(markup);
		os.write(GT);
		currentfont.push(section.getTitle().getFont());
		// contents
		for (Iterator i = section.getTitle().iterator(); i.hasNext();) {
			write((Element) i.next(), indent + 1);
		}
		// end tag
		addTabs(indent);
		writeEnd(HtmlTags.H[depth]);
		currentfont.pop();
	}
	for (Iterator i = section.iterator(); i.hasNext();) {
		write((Element) i.next(), indent);
	}
}
 
Example #5
Source File: SAXiTextHandler.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void addImage(Image img) throws EmptyStackException {
    // if there is an element on the stack...
    Object current = stack.pop();
    // ...and it's a Chapter or a Section, the Image can be
    // added directly
    if (current instanceof Chapter
            || current instanceof Section
            || current instanceof Cell) {
        ((TextElementArray) current).add(img);
        stack.push(current);
        return;
    }
    // ...if not, we need to to a lot of stuff
    else {
        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;
    }
}
 
Example #6
Source File: HtmlWriter.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
* Writes the HTML representation of a section.
*
* @param   section     the section to write
* @param   indent      the indentation
* @throws IOException
*/
   
   protected void writeSection(Section section, int indent) throws IOException {
       if (section.getTitle() != null) {
           int depth = section.getDepth() - 1;
           if (depth > 5) {
               depth = 5;
           }
           Properties styleAttributes = new Properties();
           if (section.getTitle().hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, section.getTitle().getTotalLeading() + "pt");
           // start tag
           addTabs(indent);
           writeStart(HtmlTags.H[depth]);
           write(section.getTitle().getFont(), styleAttributes);
           String alignment = HtmlEncoder.getAlignment(section.getTitle().getAlignment());
           if (!"".equals(alignment)) {
               write(HtmlTags.ALIGN, alignment);
           }
           writeMarkupAttributes(markup);
           os.write(GT);
           currentfont.push(section.getTitle().getFont());
           // contents
           for (Iterator i = section.getTitle().iterator(); i.hasNext(); ) {
               write((Element)i.next(), indent + 1);
           }
           // end tag
           addTabs(indent);
           writeEnd(HtmlTags.H[depth]);
           currentfont.pop();
       }
       for (Iterator i = section.iterator(); i.hasNext(); ) {
           write((Element) i.next(), indent);
       }
   }
 
Example #7
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a Section object based on a list of properties.
 * @param attributes
 * @return a Section
 */
public static Section getSection(Section parent, Properties attributes) {
	Section section = parent.addSection("");
	setSectionParameters(section, attributes);
	return section;
}
 
Example #8
Source File: ChapterSectionTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a document with outlines.
 * 
 */
@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 writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("ChapterSection.pdf"));
	// step 3: we open the document
	writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
	document.open();
	// step 4: we add content to the document
	// we define some fonts
	Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
	Font sectionFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
	Font subsectionFont = FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));
	// we create some paragraphs
	Paragraph blahblah = new Paragraph(
			"blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
	Paragraph blahblahblah = new Paragraph(
			"blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
	// this loop will create 7 chapters
	for (int i = 1; i < 8; i++) {
		Paragraph cTitle = new Paragraph("This is chapter " + i, chapterFont);
		Chapter chapter = new Chapter(cTitle, i);
		// in chapter 4 we change the alignment to ALIGN_JUSTIFIED
		if (i == 4) {
			blahblahblah.setAlignment(Element.ALIGN_JUSTIFIED);
			blahblah.setAlignment(Element.ALIGN_JUSTIFIED);
			chapter.add(blahblah);
		}
		// in chapter 5, the alignment is changed again
		if (i == 5) {
			blahblahblah.setAlignment(Element.ALIGN_CENTER);
			blahblah.setAlignment(Element.ALIGN_RIGHT);
			chapter.add(blahblah);
		}
		// the alignment is changed to ALIGN_JUSTIFIED again
		if (i == 6) {
			blahblahblah.setAlignment(Element.ALIGN_JUSTIFIED);
			blahblah.setAlignment(Element.ALIGN_JUSTIFIED);
		}
		// in every chapter 3 sections will be added
		for (int j = 1; j < 4; j++) {
			Paragraph sTitle = new Paragraph("This is section " + j + " in chapter " + i, sectionFont);
			Section section = chapter.addSection(sTitle, 1);
			// for chapters > 2, the outine isn't open by default
			if (i > 2)
				section.setBookmarkOpen(false);
			// in all chapters except the 1st one, some extra text is added
			// to section 3
			if (j == 3 && i > 1) {
				section.setIndentationLeft(72);
				section.add(blahblah);
				section.add(new Paragraph("test"));
			}
			// in every section 3 subsections are added
			for (int k = 1; k < 4; k++) {
				Paragraph subTitle = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont);
				Section subsection = section.addSection(subTitle, 3);
				// in the first subsection of section 3, extra text is added
				if (k == 1 && j == 3) {
					subsection.add(blahblahblah);
				}
				subsection.add(blahblah);
			}
			// in the section section of every chapter > 2 extra text is
			// added
			if (j == 2 && i > 2) {
				section.add(blahblahblah);
			}
			// a new page is added after the second section in Chapter 1
			if (j == 2 && i == 1) {
				section.add(Chunk.NEXTPAGE);
			}
		}
		document.add(chapter);
	}

	// step 5: we close the document
	document.close();
}
 
Example #9
Source File: PatchRtfDocument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public RtfBasicElement[] mapElement( Element element ) throws DocumentException {
  ArrayList<RtfBasicElement> rtfElements = new ArrayList<RtfBasicElement>();
  if ( element instanceof RtfBasicElement ) {
    RtfBasicElement rtfElement = (RtfBasicElement) element;
    rtfElement.setRtfDocument( rtfDoc );
    return new RtfBasicElement[] { rtfElement };
  }
  switch ( element.type() ) {
    case Element.CHUNK:
      Chunk chunk = (Chunk) element;
      if ( chunk.hasAttributes() ) {
        if ( chunk.getAttributes().containsKey( Chunk.IMAGE ) ) {
          rtfElements.add( new RtfImage( rtfDoc, chunk.getImage() ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.NEWPAGE ) ) {
          rtfElements.add( new RtfNewPage( rtfDoc ) );
        } else if ( chunk.getAttributes().containsKey( Chunk.TAB ) ) {
          Float tabPos = (Float) ( (Object[]) chunk.getAttributes().get( Chunk.TAB ) )[1];
          RtfTab tab = new RtfTab( tabPos.floatValue(), RtfTab.TAB_LEFT_ALIGN );
          tab.setRtfDocument( rtfDoc );
          rtfElements.add( tab );
          rtfElements.add( new RtfChunk( rtfDoc, new Chunk( "\t" ) ) );
        } else {
          rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
        }
      } else {
        rtfElements.add( new RtfChunk( rtfDoc, (Chunk) element ) );
      }
      break;
    case Element.PHRASE:
      rtfElements.add( new RtfPhrase( rtfDoc, (Phrase) element ) );
      break;
    case Element.PARAGRAPH:
      rtfElements.add( new RtfParagraph( rtfDoc, (Paragraph) element ) );
      break;
    case Element.ANCHOR:
      rtfElements.add( new RtfAnchor( rtfDoc, (Anchor) element ) );
      break;
    case Element.ANNOTATION:
      rtfElements.add( new RtfAnnotation( rtfDoc, (Annotation) element ) );
      break;
    case Element.IMGRAW:
    case Element.IMGTEMPLATE:
    case Element.JPEG:
      rtfElements.add( new RtfImage( rtfDoc, (Image) element ) );
      break;
    case Element.AUTHOR:
    case Element.SUBJECT:
    case Element.KEYWORDS:
    case Element.TITLE:
    case Element.PRODUCER:
    case Element.CREATIONDATE:
      rtfElements.add( new RtfInfoElement( rtfDoc, (Meta) element ) );
      break;
    case Element.LIST:
      rtfElements.add( new RtfList( rtfDoc, (List) element ) ); // TODO: Testing
      break;
    case Element.LISTITEM:
      rtfElements.add( new RtfListItem( rtfDoc, (ListItem) element ) ); // TODO: Testing
      break;
    case Element.SECTION:
      rtfElements.add( new RtfSection( rtfDoc, (Section) element ) );
      break;
    case Element.CHAPTER:
      rtfElements.add( new RtfChapter( rtfDoc, (Chapter) element ) );
      break;
    case Element.TABLE:
      if ( element instanceof Table ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (Table) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
    case Element.PTABLE:
      if ( element instanceof PdfPTable ) {
        rtfElements.add( new PatchRtfTable( rtfDoc, (PdfPTable) element ) );
      } else {
        rtfElements.add( new PatchRtfTable( rtfDoc, ( (SimpleTable) element ).createTable() ) );
      }
      break;
  }

  return rtfElements.toArray( new RtfBasicElement[rtfElements.size()] );
}
 
Example #10
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Creates a Section object based on a list of properties.
 * 
 * @param attributes
 * @return a Section
 */
public static Section getSection(Section parent, Properties attributes) {
	Section section = parent.addSection("");
	setSectionParameters(section, attributes);
	return section;
}