Java Code Examples for com.lowagie.text.Chapter#add()

The following examples show how to use com.lowagie.text.Chapter#add() . 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: 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 2
Source File: RtfTOCandCellbordersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates an RTF document with a TOC and Table with special Cellborders.
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2 writer2 = RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("toc.rtf"));

	writer2.setAutogenerateTOCEntries(true);

	document.open();

	Paragraph para = new Paragraph();
	para.add(new RtfTableOfContents("RIGHT CLICK AND HERE AND SELECT \"UPDATE FIELD\" TO UPDATE."));
	document.add(para);

	Paragraph par = new Paragraph("This is some sample content.");
	Chapter chap1 = new Chapter("Chapter 1", 1);
	chap1.add(par);
	Chapter chap2 = new Chapter("Chapter 2", 2);
	chap2.add(par);
	document.add(chap1);
	document.add(chap2);

	for (int i = 0; i < 300; i++) {
		if (i == 158) {
			document.add(new RtfTOCEntry("This is line 158."));
		}
		document.add(new Paragraph("Line " + i));
	}

	document.add(new RtfTOCEntry("Cell border demonstration"));

	Table table = new Table(3);

	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);

	document.add(table);
	document.close();
}