Java Code Examples for com.lowagie.text.Document#open()

The following examples show how to use com.lowagie.text.Document#open() . 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: NegativeLeadingTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Demonstrates what happens if you choose a negative leading.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("NegativeLeading.pdf"));

	// step 3: we open the document
	document.open();
	// step 4:
	document.add(new Phrase(16, "\n\n\n"));
	document.add(new Phrase(
			-16,
			"Hello, this is a very long phrase to show you the somewhat odd effect of a negative leading. You can write from bottom to top. This is not fully supported. It's something between a feature and a bug."));

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

	// Create a RtfFont with the desired font name.
	RtfFont msComicSans = new RtfFont("Comic Sans MS");

	// Use the RtfFont like any other Font.
	document.add(new Paragraph("This paragraph uses the" + " Comic Sans MS font.", msComicSans));

	// Font size, font style and font colour can also be specified.
	RtfFont bigBoldGreenArial = new RtfFont("Arial", 36, Font.BOLD, Color.GREEN);

	document.add(new Paragraph("This is a really big bold green Arial text", bigBoldGreenArial));
	document.close();
}
 
Example 3
Source File: DrawingAnchorTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates setting the horizontal and vertical anchors for a drawing
 * object
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingAnchor.rtf"));

	document.open();

	document.add(new Paragraph("This text is above the horizontal rule"));

	// Construct a new RtfShapePosition that covers the whole page
	// horizontally
	RtfShapePosition position = new RtfShapePosition(150, 0, 10400, 150);

	// The horizontal position is relative to the margins of the page
	position.setXRelativePos(RtfShapePosition.POSITION_X_RELATIVE_MARGIN);

	// The vertical position is relative to the paragraph
	position.setYRelativePos(RtfShapePosition.POSITION_Y_RELATIVE_PARAGRAPH);

	// Create a new line drawing object
	RtfShape shape = new RtfShape(RtfShape.SHAPE_LINE, position);

	// Add the shape to the paragraph, so that it is anchored to the
	// correct paragraph
	Paragraph par = new Paragraph();
	par.add(shape);
	document.add(par);

	document.add(new Paragraph("This text is below the horizontal rule"));

	document.close();

}
 
Example 4
Source File: TrialBalanceReportAction.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generate pdf for sending response using itext
 *
 * @param reportFileFullName
 * @return
 * @throws IOException
 * @throws DocumentException
 * @throws BadPdfFormatException
 */
protected ByteArrayOutputStream generatePdfOutStream(String reportFileFullName) throws IOException, DocumentException, BadPdfFormatException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // we create a reader for a certain document
    PdfReader reader = new PdfReader(reportFileFullName);
    reader.consolidateNamedDestinations();

    // step 1: creation of a document-object
    Document document = new Document(reader.getPageSizeWithRotation(1));
    // step 2: we create a writer that listens to the document
    PdfCopy writer = new PdfCopy(document, baos);
    // step 3: we open the document
    document.open();

    // we retrieve the total number of pages
    int n = reader.getNumberOfPages();

    // step 4: we add content
    PdfImportedPage page;
    for (int i = 0; i < n;) {
        ++i;
        page = writer.getImportedPage(reader, i);
        writer.addPage(page);
    }
    writer.freeReader(reader);

    // step 5: we close the document
    document.close();
    return baos;
}
 
Example 5
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 6
Source File: PdfReaderTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Ignore("validity of test needs to be resolved")
   @Test
   public void testGetLink() throws Exception {
PdfReader currentReader = new PdfReader(PdfTestBase.RESOURCES_DIR +"getLinkTest1.pdf");
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, new
	ByteArrayOutputStream());
document.open();
document.newPage();
List<?> links = currentReader.getLinks(1);
PdfAnnotation.PdfImportedLink link =
	(PdfAnnotation.PdfImportedLink) links.get(0);
writer.addAnnotation(link.createAnnotation(writer));
document.close();
   }
 
Example 7
Source File: MetaDataTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testProducer() throws Exception {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	Document document = new Document();

	PdfWriter.getInstance(document, baos);
	document.open();
	document.add(new Paragraph("Hello World"));
	document.close();

	PdfReader r = new PdfReader(baos.toByteArray());

	// with this special version, metadata is not generated by default
	Assert.assertNull(r.getInfo().get("Producer"));
}
 
Example 8
Source File: FontCacheUtils.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
static void createUnicodePDF( String format, Locale locale, String fileName )
		throws Exception
{
	FontMappingManager manager = FontMappingManagerFactory.getInstance( )
			.getFontMappingManager( format, locale );

	// step 1: creation of a document-object
	Document document = new Document( );
	PdfWriter writer = PdfWriter.getInstance( document,
			new FileOutputStream( fileName ) );
	document.open( );
	for ( int seg = 0; seg < 0xFF; seg++ )
	{
		PdfContentByte cb = writer.getDirectContent( );
		cb.beginText( );
		for ( int hi = 0; hi < 16; hi++ )
		{
			for ( int lo = 0; lo < 16; lo++ )
			{
				int x = 100 + hi * 32;
				int y = 100 + lo * 32;
				char ch = (char) ( seg * 0xFF + hi * 16 + lo );

				String fontFamily = manager.getDefaultPhysicalFont( ch );
				BaseFont bf = manager.createFont( fontFamily, Font.NORMAL );
				cb.setFontAndSize( bf, 16 );
				cb.setTextMatrix( x, y );
				cb.showText( new String( new char[]{ch} ) );
			}
		}
		cb.endText( );
	}
	document.close( );
}
 
Example 9
Source File: DrawingFreeformTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates creating freeform drawing objects
 * 
 * 
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	RtfWriter2.getInstance(document, PdfTestBase.getOutputStream("DrawingFreeform.rtf"));

	document.open();

	// Create a new rectangle RtfShape using the SHAPE_FREEFORM constant.
	RtfShapePosition position = new RtfShapePosition(1000, 1000, 4000, 4000);
	RtfShape shape = new RtfShape(RtfShape.SHAPE_FREEFORM, position);

	// Set the bottom and right extents of the drawing object.
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_GEO_RIGHT, 3000));
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_GEO_BOTTOM, 3000));

	// Define the vertices that make up the drawing object.
	// This list draws a basic table shape.
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_VERTICIES, new Point[] { new Point(100, 100),
			new Point(2900, 100), new Point(2900, 200), new Point(2600, 200), new Point(2600, 1500),
			new Point(2520, 1500), new Point(2520, 200), new Point(480, 200), new Point(480, 1500),
			new Point(400, 1500), new Point(400, 200), new Point(100, 200) }));

	// A nice red Table :-)
	shape.setProperty(new RtfShapeProperty(RtfShapeProperty.PROPERTY_FILL_COLOR, Color.red));

	document.add(shape);

	document.close();

}
 
Example 10
Source File: EncriptionTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private byte[] createEncryptedPDF () throws DocumentException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	
	Rectangle rec = new Rectangle(PageSize.A4.getWidth(),
			PageSize.A4.getHeight());
	Document document = new Document(rec);		
	PdfWriter writer = PdfWriter.getInstance(document, baos);
	writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
	document.open();
	document.add(new Paragraph("Hello World"));
	document.close();
			
	return baos.toByteArray();
}
 
Example 11
Source File: TablePdfTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testCreateTable() throws FileNotFoundException,
		DocumentException {
	// create document
	Document document = PdfTestBase.createPdf("testCreateTable.pdf");
	try {
		// new page with a table
		document.open();
		document.newPage();

		PdfPTable table = createPdfTable(2);

		for (int i = 0; i < 10; i++) {
			PdfPCell cell = new PdfPCell();
			cell.setRowspan(2);
			table.addCell(cell);

		}
		table.calculateHeights(true);
		document.add(table);
		document.newPage();

	} finally {
		// close document
		if (document != null)
			document.close();
	}

}
 
Example 12
Source File: ImagesAlignmentTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates a PDF with Images that are aligned.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("imagesAlignment.pdf"));

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

	// step 4: content
	Image gif = Image.getInstance(PdfTestBase.RESOURCES_DIR + "vonnegut.gif");
	gif.setAlignment(Image.RIGHT | Image.TEXTWRAP);
	Image jpeg = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpeg.setAlignment(Image.MIDDLE);
	Image png = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png");
	png.setAlignment(Image.LEFT | Image.UNDERLYING);

	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(gif);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(Chunk.NEWLINE);
	document.add(jpeg);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(png);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(gif);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(Chunk.NEWLINE);
	document.add(jpeg);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}
	document.add(png);
	for (int i = 0; i < 100; i++) {
		document.add(new Phrase("Who is this? "));
	}

	// step 5: we close the document
	document.close();
}
 
Example 13
Source File: ColumnSimpleTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrating the use of ColumnText
 */
@Test
public void main() throws Exception {

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

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("columnsimple.pdf"));

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

	// step 4:

	// we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.addText(new Phrase(15, "UNI\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, uni[i] + "\n", font));
	}
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.addText(new Phrase(15, "char\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, code[i] + "\n", font));
	}
	ct.go();
	ct.setSimpleColumn(160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.addText(new Phrase(15, "NAME" + "\n", font));
	for (int i = 0; i < 27; i++) {
		ct.addText(new Phrase(15, name[i] + "\n", font));
	}
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 14
Source File: StateTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changing the Graphics State with saveState() and restoreState().
 * 
 */
@Test
public void main() throws Exception {

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

	try {

		// step 2:
		// we create a writer that listens to the document
		// and directs a PDF-stream to a file
		PdfWriter writer = PdfWriter.getInstance(document,
				PdfTestBase.getOutputStream( "state.pdf"));

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

		// step 4: we grab the ContentByte and do some stuff with it
		PdfContentByte cb = writer.getDirectContent();

		cb.circle(260.0f, 500.0f, 250.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.red);
		cb.circle(260.0f, 500.0f, 200.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.blue);
		cb.circle(260.0f, 500.0f, 150.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 100.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 50.0f);
		cb.fill();
		
		cb.sanityCheck();
	} catch (DocumentException de) {
		System.err.println(de.getMessage());
	} catch (IOException ioe) {
		System.err.println(ioe.getMessage());
	}

	// step 5: we close the document
	document.close();
}
 
Example 15
Source File: JavaScriptActionTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a document with Named Actions.
 * 
 */
@Test
public void main() throws Exception {

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

	// step 2:
	HtmlWriter.getInstance(document, PdfTestBase.getOutputStream("JavaScriptAction.html"));
	// step 3: we add Javascript as Metadata and we open the document

	StringBuffer javaScriptSection = new StringBuffer();
	javaScriptSection.append("\t\tfunction load() {\n");
	javaScriptSection.append("\t\t  alert('Page has been loaded.');\n");
	javaScriptSection.append("\t\t}\n");

	javaScriptSection.append("\t\tfunction unload(){\n");
	javaScriptSection.append("\t\t  alert('Page has been unloaded.');\n");
	javaScriptSection.append("\t\t}\n");

	javaScriptSection.append("\t\tfunction sayHi(){\n");
	javaScriptSection.append("\t\t  alert('Hi !!!');\n");
	javaScriptSection.append("\t\t}");

	document.add(new Header(HtmlTags.JAVASCRIPT, javaScriptSection.toString()));
	document.setJavaScript_onLoad("load()");
	document.setJavaScript_onUnLoad("unload()");

	document.open();
	// step 4: we add some content
	Phrase phrase1 = new Phrase(
			"There are 3 JavaScript functions in the HTML page, load(), unload() and sayHi().\n\n"
					+ "The first one will be called when the HTML page has been loaded by your browser.\n"
					+ "The second one will be called when the HTML page is being unloaded,\n"
					+ "for example when you go to another page.\n");
	document.add(phrase1);

	// add a HTML link <A HREF="...">
	Anchor anchor = new Anchor("Click here to execute the third JavaScript function.");
	anchor.setReference("JavaScript:sayHi()");
	document.add(anchor);

	// step 5: we close the document
	document.close();

}
 
Example 16
Source File: ExportHighCharts.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void transformSVGIntoPDF(InputStream inputStream, OutputStream outputStream) throws IOException, DocumentException {

		Rectangle pageSize = PageSize.A4;
		Document document = new Document(pageSize);
		int orientation = PageFormat.PORTRAIT;
		try {
			PdfWriter writer = PdfWriter.getInstance(document, outputStream);
			document.open();

			double a4WidthInch = 8.26771654; // Equals 210mm
			double a4HeightInch = 11.6929134; // Equals 297mm

			Paper paper = new Paper();
			// 72 DPI
			paper.setSize((a4WidthInch * 72), (a4HeightInch * 72));
			// 1 inch margins
			paper.setImageableArea(72, 72, (a4WidthInch * 72 - 144), (a4HeightInch * 72 - 144));
			PageFormat pageFormat = new PageFormat();
			pageFormat.setPaper(paper);
			pageFormat.setOrientation(orientation);

			float width = ((float) pageFormat.getWidth());
			float height = ((float) pageFormat.getHeight());

			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate template = cb.createTemplate(width, height);
			Graphics2D g2 = template.createGraphics(width, height);

			PrintTranscoder prm = new PrintTranscoder();
			TranscoderInput ti = new TranscoderInput(inputStream);
			prm.transcode(ti, null);

			prm.print(g2, pageFormat, 0);
			g2.dispose();

			ImgTemplate img = new ImgTemplate(template);
			img.setWidthPercentage(100);
			img.setAlignment(Image.ALIGN_CENTER);

			document.add(img);

		} catch (DocumentException e) {
			logger.error("Error exporting Highcharts to PDF: " + e);
		}
		document.close();
	}
 
Example 17
Source File: VerticalTextInCellsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example with vertical text in Cells.
 */
@Test
public void main() throws Exception {

	// step1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("VerticalText.pdf"));
	// step3
	document.open();
	// step4

	// make a PdfTemplate with the vertical text
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
	String text = "Vertical";
	float size = 16;
	float width = bf.getWidthPoint(text, size);
	template.beginText();
	template.setRGBColorFillF(1, 1, 1);
	template.setFontAndSize(bf, size);
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(size + 2);
	// make an Image object from the template
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	PdfPCell cell = new PdfPCell(img);
	cell.setPadding(4);
	cell.setBackgroundColor(new Color(0, 0, 255));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	table.addCell(cell);
	table.addCell("I see a template everywhere");
	table.addCell(cell);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	document.add(table);

	// step5
	document.close();
}
 
Example 18
Source File: GlossaryTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generic page event.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Glossary.pdf"));
	GlossaryTest generic = new GlossaryTest();
	writer.setPageEvent(generic);

	// step 3: we open the document
	document.open();
	// step 4:
	String[] f = new String[14];
	f[0] = "Courier";
	f[1] = "Courier Bold";
	f[2] = "Courier Italic";
	f[3] = "Courier Bold Italic";
	f[4] = "Helvetica";
	f[5] = "Helvetica bold";
	f[6] = "Helvetica italic";
	f[7] = "Helvetica bold italic";
	f[8] = "Times New Roman";
	f[9] = "Times New Roman bold";
	f[10] = "Times New Roman italic";
	f[11] = "Times New Roman bold italic";
	f[12] = "Symbol";
	f[13] = "Zapfdingbats";
	Font[] fonts = new Font[14];
	fonts[0] = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL);
	fonts[1] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD);
	fonts[2] = FontFactory.getFont(FontFactory.COURIER, 12, Font.ITALIC);
	fonts[3] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC);
	fonts[4] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
	fonts[5] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
	fonts[6] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC);
	fonts[7] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC);
	fonts[8] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
	fonts[9] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);
	fonts[10] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC);
	fonts[11] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC);
	fonts[12] = FontFactory.getFont(FontFactory.SYMBOL, 12, Font.NORMAL);
	fonts[13] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL);
	for (int i = 0; i < 14; i++) {
		Chunk chunk = new Chunk("This is font ", fonts[i]);
		Paragraph p = new Paragraph(chunk);
		p.add(new Phrase(new Chunk(f[i], fonts[i]).setGenericTag(f[i])));
		document.add(p);
		if (i % 4 == 3) {
			document.newPage();
		}
	}

	// we add the glossary
	document.newPage();
	for (Iterator<String> i = generic.glossary.keySet().iterator(); i.hasNext();) {
		String key = (String) i.next();
		int page = ((Integer) generic.glossary.get(key)).intValue();
		Paragraph g = new Paragraph(key);
		g.add(" : page ");
		g.add(String.valueOf(page));
		document.add(g);
	}

	// step 5: we close the document
	document.close();
}
 
Example 19
Source File: LogoTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Draws the iText logo.
 */
 @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("logo.pdf"));

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

	// step 4:
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	PdfContentByte cb = writer.getDirectContent();
	PdfTemplate template = cb.createTemplate(500, 200);
	template.setLineWidth(2f);
	template.rectangle(2.5f, 2.5f, 495f, 195f);
	template.stroke();
	template.setLineWidth(12f);
	template.arc(40f - (float) Math.sqrt(12800),
			120f + (float) Math.sqrt(12800),
			200f - (float) Math.sqrt(12800),
			-40f + (float) Math.sqrt(12800), 281.25f, 33.75f);
	template.arc(40f, 120f, 200f, -40f, 90f, 45f);
	template.stroke();
	template.setLineCap(1);
	template.setLineWidth(12f);
	template.arc(80f, 40f, 160f, 120f, 90f, 180f);
	template.arc(115f, 75f, 125f, 85f, 0f, 360f);
	template.stroke();
	template.beginText();
	template.setFontAndSize(bf, 180);
	template.setRGBColorFill(0xFF, 0x00, 0x00);
	template.showTextAligned(PdfContentByte.ALIGN_LEFT, "T", 125f, 35f, 0f);
	template.resetRGBColorFill();
	template.showTextAligned(PdfContentByte.ALIGN_LEFT, "ext", 220f, 35f,
			0f);
	template.endText();
	template.sanityCheck();

	cb.addTemplate(template, 0, 1, -1, 0, 500, 200);
	cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
	cb.addTemplate(template, 0.25f, 0, 0, 0.25f, 100, 100);
	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 20
Source File: TableBorderExTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
protected void setUp( ) throws Exception
{
	super.setUp( );

	cells[0] = new CellArea( 10, 10, 40, 40 );
	cells[0].defineBorder( BorderInfo.TOP_BORDER, 2 );
	cells[0].defineBorder( BorderInfo.TOP_BORDER, Color.blue );
	cells[0].defineBorder( BorderInfo.LEFT_BORDER, 6 );
	cells[0].defineBorder( BorderInfo.LEFT_BORDER, Color.orange );
	cells[1] = new CellArea( 50, 10, 40, 40 );
	cells[1].defineBorder( BorderInfo.TOP_BORDER, 4 );
	cells[1].defineBorder( BorderInfo.TOP_BORDER, Color.blue );
	cells[2] = new CellArea( 90, 10, 40, 40 );
	cells[2].defineBorder( BorderInfo.TOP_BORDER, 6 );
	cells[2].defineBorder( BorderInfo.TOP_BORDER, Color.blue );
	cells[2].defineBorder( BorderInfo.RIGHT_BORDER, 6 );
	cells[2].defineBorder( BorderInfo.RIGHT_BORDER, Color.orange );
	cells[3] = new CellArea( 10, 50, 40, 80 );
	cells[3].defineBorder( BorderInfo.LEFT_BORDER, 6 );
	cells[3].defineBorder( BorderInfo.LEFT_BORDER, Color.orange );
	cells[3].defineBorder( BorderInfo.BOTTOM_BORDER, 6 );
	cells[3].defineBorder( BorderInfo.BOTTOM_BORDER, Color.blue );
	cells[4] = new CellArea( 50, 50, 40, 40 );
	cells[4].defineBorder( BorderInfo.TOP_BORDER, 2 );
	cells[4].defineBorder( BorderInfo.TOP_BORDER, Color.green );
	cells[5] = new CellArea( 90, 50, 40, 40 );
	cells[5].defineBorder( BorderInfo.LEFT_BORDER, 0 );
	cells[5].defineBorder( BorderInfo.TOP_BORDER, 0 );
	cells[5].defineBorder( BorderInfo.RIGHT_BORDER, 6 );
	cells[5].defineBorder( BorderInfo.RIGHT_BORDER, Color.orange );
	cells[6] = null;//new CellArea( 10, 90, 40, 40 );
	cells[7] = new CellArea( 50, 90, 40, 40 );
	cells[7].defineBorder( BorderInfo.BOTTOM_BORDER, 4 );
	cells[7].defineBorder( BorderInfo.BOTTOM_BORDER, Color.blue );
	cells[8] = new CellArea( 90, 90, 40, 40 );
	cells[8].defineBorder( BorderInfo.TOP_BORDER, 0 );
	cells[8].defineBorder( BorderInfo.LEFT_BORDER, 2 );
	cells[8].defineBorder( BorderInfo.LEFT_BORDER, Color.red );
	cells[8].defineBorder( BorderInfo.BOTTOM_BORDER, 2 );
	cells[8].defineBorder( BorderInfo.BOTTOM_BORDER, Color.blue );
	cells[8].defineBorder( BorderInfo.RIGHT_BORDER, 6 );
	cells[8].defineBorder( BorderInfo.RIGHT_BORDER, Color.orange );
	Document document = new Document( );
	try
	{
		// step 2: creation of the writer
		PdfWriter writer = PdfWriter.getInstance( document,
				new FileOutputStream( "TableBorderTestEx.pdf" ) );
		// step 3: we open the document
		Rectangle pageSize = new Rectangle( pageWidth, pageHeight );
		document.setPageSize( pageSize );
		document.open( );
		// step 4: we grab the ContentByte and do some stuff with it
		cb = writer.getDirectContent( );
		testBorderDraw( );
	}
	catch ( DocumentException de )
	{
		System.err.println( de.getMessage( ) );
	}
	catch ( IOException ioe )
	{
		System.err.println( ioe.getMessage( ) );
	}

	// step 5: we close the document
	document.close( );
}