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

The following examples show how to use com.lowagie.text.Paragraph#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: ContractsGrantsInvoiceReportServiceImpl.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Generates a PDF paragraph for a given Address
 * @param name the name that this envelope is being sent to
 * @param line1Address the first line of the address
 * @param line2Address the second line of the address
 * @param cityName the name of the city to send this to
 * @param stateCode the code of the state or presumably province to send this to
 * @param postalCode the postal code/zip code to send the enveleope to
 * @param font the font to write in
 * @return a PDF Paragraph for the address
 */
protected Paragraph generateAddressParagraph(String name, String line1Address, String line2Address, String cityName, String stateCode, String postalCode, Font font) {
    Paragraph addressParagraph = new Paragraph();
    addressParagraph.add(new Paragraph(name, font));
    if (!StringUtils.isBlank(line1Address)) {
        addressParagraph.add(new Paragraph(line1Address, font));
    }
    if (!StringUtils.isBlank(line2Address)) {
        addressParagraph.add(new Paragraph(line2Address, font));
    }
    String string = "";
    if (!StringUtils.isBlank(cityName)) {
        string += cityName;
    }
    if (!StringUtils.isBlank(stateCode)) {
        string += ", " + stateCode;
    }
    if (!StringUtils.isBlank(postalCode)) {
        string += "-" + postalCode;
    }
    if (!StringUtils.isBlank(string)) {
        addressParagraph.add(new Paragraph(string, font));
    }
    return addressParagraph;
}
 
Example 2
Source File: CustomerLoadServiceImpl.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void writeCustomerSectionTitle(Document pdfDoc, String customerNameLine) {
    Font font = FontFactory.getFont(FontFactory.COURIER, 8, Font.BOLD + Font.UNDERLINE);

    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(Element.ALIGN_LEFT);
    paragraph.add(new Chunk(customerNameLine, font));

    //  blank line
    paragraph.add(new Chunk("", font));

    try {
        pdfDoc.add(paragraph);
    }
    catch (DocumentException e) {
        LOG.error("iText DocumentException thrown when trying to write content.", e);
        throw new RuntimeException("iText DocumentException thrown when trying to write content.", e);
    }
}
 
Example 3
Source File: DifferentFontsTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Using FontSelector.
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document();
	// step 2
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("differentfonts.pdf"));
	// step 3
	document.open();
	// step 4
	Paragraph p = new Paragraph();
	p.add(new Chunk("This text is in Times Roman. This is ZapfDingbats: ", new Font(Font.TIMES_ROMAN, 12)));
	p.add(new Chunk("abcdefghijklmnopqrstuvwxyz", new Font(Font.ZAPFDINGBATS, 12)));
	p.add(new Chunk(". This is font Symbol: ", new Font(Font.TIMES_ROMAN, 12)));
	p.add(new Chunk("abcdefghijklmnopqrstuvwxyz", new Font(Font.SYMBOL, 12)));
	document.add(new Paragraph(p));
	// step 5
	document.close();

}
 
Example 4
Source File: PdfDocumentFactory.java    From javamelody with Apache License 2.0 6 votes vote down vote up
Element createParagraphElement(String paragraphTitle, String iconName)
		throws DocumentException, IOException {
	final Paragraph paragraph = new Paragraph("", paragraphTitleFont);
	paragraph.setSpacingBefore(5);
	paragraph.setSpacingAfter(5);
	if (iconName != null) {
		paragraph.add(new Chunk(getParagraphImage(iconName), 0, -5));
	}
	final Phrase element = new Phrase(' ' + paragraphTitle, paragraphTitleFont);
	element.setLeading(12);
	paragraph.add(element);
	// chapter pour avoir la liste des signets
	final ChapterAutoNumber chapter = new ChapterAutoNumber(paragraph);
	// sans numéro de chapitre
	chapter.setNumberDepth(0);
	chapter.setBookmarkOpen(false);
	chapter.setTriggerNewPage(false);
	return chapter;
}
 
Example 5
Source File: SurveyPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeAnswer(Document document,String questionText , boolean answerValue , 
						 String falseMessage,String trueMessage) throws Exception{
	
	
	//String falseString ="False";  
	//String trueString = "True";
	
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.add(new Chunk(questionText.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(answerValue ? trueMessage : falseMessage ,normalFont));  
	document.add(questionParagraph);
}
 
Example 6
Source File: PdfCounterRequestContextReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeDurations(List<CounterRequestContext> contexts)
		throws DocumentException, IOException {
	getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);

	final Paragraph paragraph = new Paragraph("", cellFont);
	boolean first = true;
	for (final CounterRequestContext context : contexts) {
		if (!first) {
			paragraph.add(new Chunk('\n', cellFont));
		}
		final int duration = context.getDuration(timeOfSnapshot);
		final Counter parentCounter = context.getParentCounter();
		final PdfCounterReport counterReport = counterReportsByCounterName
				.get(parentCounter.getName());
		if (parentCounter.getIconName() != null) {
			paragraph.add(new Chunk(getImage(parentCounter.getIconName()), 0, -1));
		}
		final Font slaFont;
		if (counterReport == null) {
			slaFont = infoCellFont;
		} else {
			slaFont = counterReport.getSlaFont(duration);
		}
		paragraph.add(new Phrase(integerFormat.format(duration), slaFont));
		first = false;
	}
	addCell(paragraph);
}
 
Example 7
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeCurrencyEntry(Document document,String label , double value) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.setIndentationLeft(18);
	questionParagraph.add(new Chunk(label.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(CurrencyValidator.getInstance().format(value , LocaleContextHolder.getLocale()) , normalFont));  
	document.add(questionParagraph);
}
 
Example 8
Source File: ExtendedFontStylesTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
    * Extended font styles example.
    * 
    * 
    */
@Test
   public void main() throws Exception {
           Document document = new Document();
           RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("ExtendedFontStyles.rtf"));
           document.open();
           
           // Use the RtfFont.STYLE_* instead of the Font styles.
           RtfFont doubleStrikethrough = new RtfFont("Arial", RtfFont.UNDEFINED,
                   RtfFont.STYLE_DOUBLE_STRIKETHROUGH);
           RtfFont shadow = new RtfFont("Arial", RtfFont.UNDEFINED,
                   RtfFont.STYLE_SHADOW);
           
           // Or combine them with Font styles.
           RtfFont engravedItalic = new RtfFont("Arial", RtfFont.UNDEFINED,
                   RtfFont.STYLE_ENGRAVED | Font.ITALIC);
           
           // The hidden style is special since it hides text.
           RtfFont hidden = new RtfFont("Arial", RtfFont.UNDEFINED,
                   RtfFont.STYLE_HIDDEN);
           
           Paragraph paragraph = new Paragraph("This text is ", new RtfFont("Arial", 12));
           
           // Use the RtfFonts when creating the text.
           paragraph.add(new Chunk("deleted,", doubleStrikethrough));
           paragraph.add(new Chunk(" shady,", shadow));
           paragraph.add(new Chunk(" engraved and italic", engravedItalic));
           paragraph.add(" and");
           paragraph.add(new Chunk(" you won't see this", hidden));
           paragraph.add(" nothing.");
           
           document.add(paragraph);
           document.close();
   }
 
Example 9
Source File: MRtfWriter.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * We create a writer that listens to the document and directs a RTF-stream to out
 *
 * @param table
 *           MBasicTable
 * @param document
 *           Document
 * @param out
 *           OutputStream
 * @return DocWriter
 */
@Override
protected DocWriter createWriter(final MBasicTable table, final Document document,
		final OutputStream out) {
	final RtfWriter2 writer = RtfWriter2.getInstance(document, out);

	// title
	final String title = buildTitle(table);
	if (title != null) {
		final HeaderFooter header = new RtfHeaderFooter(new Paragraph(title));
		header.setAlignment(Element.ALIGN_LEFT);
		header.setBorder(Rectangle.NO_BORDER);
		document.setHeader(header);
		document.addTitle(title);
	}

	// advanced page numbers : x/y
	final Paragraph footerParagraph = new Paragraph();
	final Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
	footerParagraph.add(new RtfPageNumber(font));
	footerParagraph.add(new Phrase(" / ", font));
	footerParagraph.add(new RtfTotalPageNumber(font));
	footerParagraph.setAlignment(Element.ALIGN_CENTER);
	final HeaderFooter footer = new RtfHeaderFooter(footerParagraph);
	footer.setBorder(Rectangle.TOP);
	document.setFooter(footer);

	return writer;
}
 
Example 10
Source File: PdfRequestAndGraphDetailReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeRequest(CounterRequest childRequest, float executionsByRequest,
		boolean allChildHitsDisplayed) throws IOException, DocumentException {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	final Paragraph paragraph = new Paragraph(defaultCell.getLeading() + cellFont.getSize());
	if (executionsByRequest != -1) {
		paragraph.setIndentationLeft(5);
	}
	final Counter parentCounter = getCounterByRequestId(childRequest);
	if (parentCounter != null && parentCounter.getIconName() != null) {
		paragraph.add(new Chunk(getSmallImage(parentCounter.getIconName()), 0, -1));
	}
	paragraph.add(new Phrase(childRequest.getName(), cellFont));
	final PdfPCell requestCell = new PdfPCell();
	requestCell.addElement(paragraph);
	requestCell.setGrayFill(defaultCell.getGrayFill());
	requestCell.setPaddingTop(defaultCell.getPaddingTop());
	addCell(requestCell);

	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	if (executionsByRequest != -1) {
		addCell(nbExecutionsFormat.format(executionsByRequest));
	} else {
		final boolean hasChildren = !request.getChildRequestsExecutionsByRequestId().isEmpty();
		if (hasChildren) {
			addCell("");
		}
	}
	writeRequestValues(childRequest, allChildHitsDisplayed);
}
 
Example 11
Source File: PdfThreadInformationsReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private void writeThreadInformations(ThreadInformations threadInformations)
		throws DocumentException, IOException {
	final PdfPCell defaultCell = getDefaultCell();
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	addCell(threadInformations.getName());
	defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	if (threadInformations.isDaemon()) {
		addCell(getString("oui"));
	} else {
		addCell(getString("non"));
	}
	defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	addCell(integerFormat.format(threadInformations.getPriority()));
	defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
	final PdfPCell cell = new PdfPCell();
	final Paragraph paragraph = new Paragraph(
			getDefaultCell().getLeading() + cellFont.getSize());
	paragraph.add(new Chunk(
			getImage(
					"bullets/" + HtmlThreadInformationsReport.getStateIcon(threadInformations)),
			0, -1));
	paragraph.add(new Phrase(String.valueOf(threadInformations.getState()), cellFont));
	cell.addElement(paragraph);
	addCell(cell);
	if (stackTraceEnabled) {
		addCell(threadInformations.getExecutedMethod());
	}
	if (cpuTimeEnabled) {
		defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		addCell(integerFormat.format(threadInformations.getCpuTimeMillis()));
		addCell(integerFormat.format(threadInformations.getUserTimeMillis()));
	}
}
 
Example 12
Source File: PdfRequestAndGraphDetailReport.java    From javamelody with Apache License 2.0 5 votes vote down vote up
@Override
void toPdf() throws DocumentException, IOException {
	if (request != null) {
		if (request.getRumData() != null && request.getRumData().getHits() != 0) {
			writeRequestRumData();
		}

		writeHeader();

		writeRequests();

		addTableToDocument();

		if (JdbcWrapper.SINGLETON.getSqlCounter().isRequestIdFromThisCounter(request.getId())
				&& !request.getName().toLowerCase(Locale.ENGLISH).startsWith("alter ")) {
			// inutile d'essayer d'avoir le plan d'exécution des requêtes sql
			// telles que "alter session set ..." (cf issue 152)
			writeSqlRequestExplainPlan();
		}
	}

	if (isGraphDisplayed()) {
		writeGraph();
	}

	if (request != null && request.getStackTrace() != null) {
		final Paragraph paragraph = new Paragraph("\n", cellFont);
		paragraph.setIndentationLeft(20);
		paragraph.setIndentationRight(20);
		paragraph.add(new Phrase("Stack-trace\n", boldFont));
		paragraph.add(new Phrase(request.getStackTrace().replace("\t", "        "), cellFont));
		addToDocument(paragraph);
	}
}
 
Example 13
Source File: MarginsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a PDF document with different pages that have different margins.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A5, 36, 72, 108, 180);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Margins.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	document.add(new Paragraph(
			"The left margin of this document is 36pt (0.5 inch); the right margin 72pt (1 inch); the top margin 108pt (1.5 inch); the bottom margin 180pt (2.5 inch). "));
	Paragraph paragraph = new Paragraph();
	paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
	for (int i = 0; i < 20; i++) {
		paragraph.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. ");
	}
	document.add(paragraph);
	document.setMargins(180, 108, 72, 36);
	document.add(new Paragraph("Now we change the margins. You will see the effect on the next page."));
	document.add(paragraph);
	document.setMarginMirroring(true);
	document.add(new Paragraph("Starting on the next page, the margins will be mirrored."));
	document.add(paragraph);

	// step 5: we close the document
	document.close();
}
 
Example 14
Source File: OutlineActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates some PageLabel functionality.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	Document remote = new Document();
	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OutlineActions.pdf"));
	PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("remote.pdf"));
	// step 3:
	writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
	document.open();
	remote.open();
	// step 4:
	// we add some content
	document.add(new Paragraph("Outline action example"));
	// we add the outline
	PdfContentByte cb = writer.getDirectContent();
	PdfOutline root = cb.getRootOutline();
	PdfOutline links = new PdfOutline(root, new PdfAction("http://www.lowagie.com/iText/links.html"),
			"Useful links");
	links.setColor(new Color(0x00, 0x80, 0x80));
	links.setStyle(Font.BOLD);
	new PdfOutline(links, new PdfAction("http://www.lowagie.com/iText"), "Bruno's iText site");
	new PdfOutline(links, new PdfAction("http://itextpdf.sourceforge.net/"), "Paulo's iText site");
	new PdfOutline(links, new PdfAction("http://sourceforge.net/projects/itext/"), "iText @ SourceForge");
	PdfOutline other = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), "other actions", false);
	other.setStyle(Font.ITALIC);
	new PdfOutline(other, new PdfAction("remote.pdf", 1), "Go to yhe first page of a remote file");
	new PdfOutline(other, new PdfAction("remote.pdf", "test"), "Go to a local destination in a remote file");
	new PdfOutline(other, PdfAction.javaScript("app.alert('Hello');\r", writer), "Say Hello");

	remote.add(new Paragraph("Some remote document"));
	remote.newPage();
	Paragraph p = new Paragraph("This paragraph contains a ");
	p.add(new Chunk("local destination").setLocalDestination("test"));
	remote.add(p);

	// step 5: we close the document
	document.close();
	remote.close();
}
 
Example 15
Source File: DrawingWrapTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates setting different text wrapping modes and the z-ordering.
 * 
 * 
 */
@Test
public void main() throws Exception {

	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingWrap.rtf"));

	document.open();

	Paragraph par = new Paragraph();
	for (int i = 0; i < 600; i++) {
		par.add("bla ");
	}
	document.add(par);

	// Create a new rectangle RtfShape. By default it will be above the text
	RtfShapePosition position = new RtfShapePosition(1000, 1000, 2000, 2000);
	RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, position);
	document.add(shape);

	// Create a rounded rectangle RtfShape and position it below the text
	position = new RtfShapePosition(4000, 1500, 4500, 5000);
	position.setShapeBelowText(true);
	shape = new RtfShape(RtfShape.SHAPE_ROUND_RECTANGLE, position);
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_ADJUST_VALUE, 4500));
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.GRAY));
	document.add(shape);

	// Create a triangle RtfShape, around which text will wrap on both sides
	position = new RtfShapePosition(1500, 3000, 4000, 2500);
	shape = new RtfShape(RtfShape.SHAPE_TRIANGLE_RIGHT, position);
	shape.setWrapping(RtfShape.SHAPE_WRAP_BOTH);
	document.add(shape);

	// Create an elliptical RtfShape, around which text will only wrap on
	// the left side
	position = new RtfShapePosition(3000, 6000, 10500, 4500);
	shape = new RtfShape(RtfShape.SHAPE_ELLIPSE, position);
	shape.setWrapping(RtfShape.SHAPE_WRAP_LEFT);
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.BLUE));
	document.add(shape);

	// Create a circular RtfShape and set its z-order to 1
	position = new RtfShapePosition(5850, 6800, 8200, 7250);
	position.setZOrder(1);
	shape = new RtfShape(RtfShape.SHAPE_ELLIPSE, position);
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.RED));
	document.add(shape);

	// Create a star RtfShape and set its z-order to 2, above the circle
	// defined above
	position = new RtfShapePosition(6000, 7000, 8000, 7000);
	position.setZOrder(2);
	shape = new RtfShape(RtfShape.SHAPE_STAR, position);
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.YELLOW));
	document.add(shape);

	document.close();
}
 
Example 16
Source File: LocalGotoTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a document with a Local Goto and a Local Destination.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("LocalGoto.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:

	// we make some content

	// a paragraph with a local goto
	Paragraph p1 = new Paragraph("We will do something special with this paragraph. If you click on ",
			FontFactory.getFont(FontFactory.HELVETICA, 12));
	p1.add(new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255)))
			.setLocalGoto("test"));
	p1.add(" you will automatically jump to another location in this document.");

	// some paragraph
	Paragraph p2 = new Paragraph("blah, blah, blah");

	// a paragraph with a local destination
	Paragraph p3 = new Paragraph("This paragraph contains a ");
	p3.add(new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0,
			255, 0))).setLocalDestination("test"));

	// we add the content
	document.add(p1);
	document.add(p2);
	document.add(p2);
	document.add(p2);
	document.add(p2);
	document.add(p2);
	document.add(p2);
	document.add(p2);
	document.add(p3);

	// step 5: we close the document
	document.close();
}
 
Example 17
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();
}
 
Example 18
Source File: LayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Draws different things into different layers.
 */
@Test
public void main() throws Exception {


	// step 1: creation of a document-object
	Document document = new Document();

	
	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream("layers.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:

	// high level
	Paragraph p = new Paragraph();
	for (int i = 0; i < 100; i++)
		p.add(new Chunk("Blah blah blah blah blah. "));
	document.add(p);
	Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR
			+ "hitchcock.png");
	img.setAbsolutePosition(100, 500);
	document.add(img);

	// low level
	PdfContentByte cb = writer.getDirectContent();
	PdfContentByte cbu = writer.getDirectContentUnder();
	cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
	cb.circle(250.0f, 500.0f, 50.0f);
	cb.fill();
	cb.sanityCheck();

	cbu.setRGBColorFill(0xFF, 0x00, 0x00);
	cbu.circle(250.0f, 500.0f, 100.0f);
	cbu.fill();
	cbu.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 19
Source File: MultipleHeaderFooterTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Extended font example.
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("MultipleHeaderFooter.rtf"));

	// Create the Paragraph that will be used in the header.
	Paragraph date = new Paragraph("01.01.2010");
	date.setAlignment(Element.ALIGN_CENTER);

	// Create the RtfHeaderFooterGroup for the header.
	// To display the same header on both pages, but not the
	// title page set them to left and right pages explicitly.
	RtfHeaderFooterGroup header = new RtfHeaderFooterGroup();
	header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
	header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);

	// Set the header
	document.setHeader(header);

	// Create the paragraphs that will be used as footers
	Paragraph titleFooter = new Paragraph("Multiple headers / footers example");
	titleFooter.setAlignment(Element.ALIGN_CENTER);
	Paragraph leftFooter = new Paragraph("Page ");
	leftFooter.add(new RtfPageNumber());
	Paragraph rightFooter = new Paragraph("Page ");
	rightFooter.add(new RtfPageNumber());
	rightFooter.setAlignment(Element.ALIGN_RIGHT);

	// Create the RtfHeaderGroup for the footer and set the footers
	// at the desired positions
	RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
	footer.setHeaderFooter(new RtfHeaderFooter(titleFooter), RtfHeaderFooter.DISPLAY_FIRST_PAGE);
	footer.setHeaderFooter(new RtfHeaderFooter(leftFooter), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
	footer.setHeaderFooter(new RtfHeaderFooter(rightFooter), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);

	// Set the document footer
	document.setFooter(footer);

	document.open();

	document.add(new Paragraph("This document has headers and footers created"
			+ " using the RtfHeaderFooterGroup class.\n\n"));

	// Add some content, so that the different headers / footers show up.
	for (int i = 0; i < 300; i++) {
		document.add(new Paragraph("Just a bit of content so that the headers become visible."));
	}

	document.close();

}
 
Example 20
Source File: ContractsGrantsInvoiceReportServiceImpl.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Generates the pdf file for printing the envelopes.
 *
 * @param list
 * @param outputStream
 * @throws DocumentException
 * @throws IOException
 */
protected void generateCombinedPdfForEnvelopes(Collection<ContractsGrantsInvoiceDocument> list, OutputStream outputStream) throws DocumentException, IOException {
    Document document = new Document(new Rectangle(ArConstants.InvoiceEnvelopePdf.LENGTH, ArConstants.InvoiceEnvelopePdf.WIDTH));
    PdfWriter.getInstance(document, outputStream);
    boolean pageAdded = false;

    for (ContractsGrantsInvoiceDocument invoice : list) {
        // add a document
        for (InvoiceAddressDetail invoiceAddressDetail : invoice.getInvoiceAddressDetails()) {
            if (ArConstants.InvoiceTransmissionMethod.MAIL.equals(invoiceAddressDetail.getInvoiceTransmissionMethodCode())) {
                CustomerAddress address = invoiceAddressDetail.getCustomerAddress();

                Integer numberOfEnvelopesToPrint = address.getCustomerEnvelopesToPrintQuantity();
                if (ObjectUtils.isNull(numberOfEnvelopesToPrint)) {
                    numberOfEnvelopesToPrint = 1;
                }
                for (int i = 0; i < numberOfEnvelopesToPrint; i++) {
                    // if a page has not already been added then open the document.
                    if (!pageAdded) {
                        document.open();
                    }
                    pageAdded = true;
                    document.newPage();
                    // adding the sent From address
                    Organization org = invoice.getInvoiceGeneralDetail().getAward().getPrimaryAwardOrganization().getOrganization();
                    Paragraph sentBy = generateAddressParagraph(org.getOrganizationName(), org.getOrganizationLine1Address(), org.getOrganizationLine2Address(), org.getOrganizationCityName(), org.getOrganizationStateCode(), org.getOrganizationZipCode(), ArConstants.PdfReportFonts.ENVELOPE_SMALL_FONT);
                    sentBy.setIndentationLeft(ArConstants.InvoiceEnvelopePdf.INDENTATION_LEFT);
                    sentBy.setAlignment(Element.ALIGN_LEFT);

                    // adding the send To address
                    String string;
                    Paragraph sendTo = generateAddressParagraph(address.getCustomerAddressName(), address.getCustomerLine1StreetAddress(), address.getCustomerLine2StreetAddress(), address.getCustomerCityName(), address.getCustomerStateCode(), address.getCustomerZipCode(), ArConstants.PdfReportFonts.ENVELOPE_TITLE_FONT);
                    sendTo.setAlignment(Element.ALIGN_CENTER);
                    sendTo.add(new Paragraph(KFSConstants.BLANK_SPACE));

                    document.add(sentBy);
                    document.add(sendTo);
                }
            }
        }
    }
    if (pageAdded) {
        document.close();
    }
}