com.itextpdf.text.Chunk Java Examples

The following examples show how to use com.itextpdf.text.Chunk. 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: PDFExport.java    From MtgDesktopCompanion with GNU General Public License v3.0 8 votes vote down vote up
@Override
public void exportDeck(MagicDeck deck, File f) throws IOException {
	PdfPTable table = new PdfPTable(3);
	table.setHorizontalAlignment(Element.ALIGN_CENTER);

	try {
		document = new Document(PageSize.A4, 5, 5, 10, 5);
		document.addAuthor(getString("AUTHOR"));
		document.addCreationDate();
		document.addCreator(MTGConstants.MTG_APP_NAME);
		document.addTitle(deck.getName());

		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
		document.open();
		document.add(new Chunk(""));
		for (MagicCard card : deck.getMainAsList()) {
			table.addCell(getCells(card));
			notify(card);
		}
		document.add(table);
		document.close();
		writer.close();
	} catch (Exception e) {
		logger.error("Error in pdf creation " + f, e);
	}
}
 
Example #2
Source File: AnnotationIcons.java    From testarea-itext5 with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/46204693/cant-get-itext-rectangle-to-work-correctly-with-annotations">
 * Can't get itext Rectangle to work correctly with annotations
 * </a>
 * <p>
 * This test looks at a <b>Text</b> annotation added via a {@link Chunk}
 * as done by the OP. As this way of adding annotations resets the
 * annotation <b>Rect</b> to the bounding box of the rendered {@link Chunk},
 * it is not really what the OP wants.
 * </p>
 */
@Test
public void testAnnotationIconForTYD() throws FileNotFoundException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "annotationIcons.pdf")));
    document.open();

    // Not "new Rectangle(164, 190, 164, 110)" which would be empty
    Rectangle rect = new Rectangle(164, 190, 328, 300);

    // Annotation added like the OP does
    Chunk chunk_text = new Chunk("Let's test a Text annotation...");
    chunk_text.setAnnotation(PdfAnnotation.createText(writer, rect, "Warning", "This is a Text annotation with Comment icon.", false, "Comment"));        

    document.add(chunk_text);

    // Annotation added to the document without Chunk
    writer.addAnnotation(PdfAnnotation.createText(writer, rect, "Warning 2", "This is another Text annotation with Comment icon.", false, "Comment"));

    document.close();
}
 
Example #3
Source File: PDFMigrationReportWriter.java    From bonita-studio with GNU General Public License v2.0 7 votes vote down vote up
private void createLegend(Paragraph mainPara) throws BadElementException,
		MalformedURLException, IOException {
	mainPara.add(new Chunk("      ",legendFont));
	Image im = getImageForStatus(IStatus.OK);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.noActionRequired,legendFont));
	mainPara.add(new Chunk("   ",legendFont));
	im = getImageForStatus(IStatus.WARNING);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.reviewRequired,legendFont));
	mainPara.add(new Chunk("   ",legendFont));
	im = getImageForStatus(IStatus.ERROR);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.actionRequired,legendFont));
}
 
Example #4
Source File: InterlineSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/34681893/itextsharp-extra-space-between-lines">
 * iTextSharp: Extra space between lines
 * </a>
 * <p>
 * Indeed, the OP's {@link Phrase#setLeading(float, float)} calls are ignored.
 * The reason is that the op is working in text mode. Thus, he has to use
 * {@link ColumnText#setLeading(float, float)} instead, cf.
 * {@link #testLikeUser3208131Fixed()}.
 * </p>
 */
@Test
public void testLikeUser3208131() throws DocumentException, FileNotFoundException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "interline-user3208131.pdf")));
    document.open();

    Font font = new Font(FontFamily.UNDEFINED, 4, Font.UNDEFINED, null);
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);

    float gutter = 15;
    float colwidth = (document.getPageSize().getRight() - document.getPageSize().getLeft() - gutter) / 2;

    float[] left = { document.getPageSize().getLeft() + 133, document.getPageSize().getTop() - 35,
            document.getPageSize().getLeft() + 133, document.getPageSize().getBottom() };
    float[] right = { document.getPageSize().getLeft() + colwidth, document.getPageSize().getTop() - 35,
            document.getPageSize().getLeft() + colwidth, document.getPageSize().getBottom() };

    for (int i = 0; i < 3; i++)
    {
        Phrase Ps = new Phrase("Test " + i + "\n", font);
        Ps.setLeading(0.0f, 0.6f);
        ct.addText(Ps);
        ct.addText(Chunk.NEWLINE);
    }
    ct.setColumns(left, right);
    ct.go();

    document.close();
}
 
Example #5
Source File: InterlineSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/34681893/itextsharp-extra-space-between-lines">
 * iTextSharp: Extra space between lines
 * </a>
 * <p>
 * Indeed, the OP's {@link Phrase#setLeading(float, float)} calls are ignored,
 * cf. {@link #testLikeUser3208131()}. The reason is that the op is working in
 * text mode. Thus, he has to use {@link ColumnText#setLeading(float, float)}
 * instead.
 * </p>
 */
@Test
public void testLikeUser3208131Fixed() throws DocumentException, FileNotFoundException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "interline-user3208131-fixed.pdf")));
    document.open();

    Font font = new Font(FontFamily.UNDEFINED, 4, Font.UNDEFINED, null);
    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);

    float gutter = 15;
    float colwidth = (document.getPageSize().getRight() - document.getPageSize().getLeft() - gutter) / 2;

    float[] left = { document.getPageSize().getLeft() + 133, document.getPageSize().getTop() - 35,
            document.getPageSize().getLeft() + 133, document.getPageSize().getBottom() };
    float[] right = { document.getPageSize().getLeft() + colwidth, document.getPageSize().getTop() - 35,
            document.getPageSize().getLeft() + colwidth, document.getPageSize().getBottom() };

    ct.setLeading(0.0f, 0.3f);
    for (int i = 0; i < 3; i++)
    {
        Phrase Ps = new Phrase("Test " + i + "\n", font);
        ct.addText(Ps);
        ct.addText(Chunk.NEWLINE);
    }
    ct.setColumns(left, right);
    ct.go();

    document.close();
}
 
Example #6
Source File: PdfExportTest.java    From xiaoyaoji with GNU General Public License v3.0 6 votes vote down vote up
private void getInfo(Document document) throws DocumentException {

        NormalContent content = new NormalContent(12, false);
        content.setAlignment(Element.ALIGN_RIGHT);
        Map<Object, Object> info = new HashMap<Object, Object>();
        info.put("作者", "Hello World");
        info.put("更新时间", "2016-09-21 10:17:05");
        content.addContent(info);
        content.add(Chunk.NEXTPAGE);
        document.add(content);
    }
 
Example #7
Source File: PdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
public void addNewline(Document doc, int i) throws DocumentException {
	if (i > 0) {
		for (int j = 0; j < i; j++) {
			doc.add(Chunk.NEWLINE);
		}
	}
}
 
Example #8
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
protected Paragraph createReportTitle(ReportTitle reportTitle) {
	Paragraph paragraph = new Paragraph();
	paragraph.setAlignment(Element.ALIGN_CENTER);
	if (reportTitle != null && reportTitle.isShowTitle()) {
		TextChunk titleChunk = new TextChunk();
		titleChunk.setText(reportTitle.getTitle());
		titleChunk.setFontSize(reportTitle.getStyle().getFontSize());
		titleChunk.setFontColor(reportTitle.getStyle().getFontColor());
		paragraph.add(createChunk(titleChunk));
		paragraph.add(Chunk.NEWLINE);
		paragraph.add(Chunk.NEWLINE);
	}
	return paragraph;
}
 
Example #9
Source File: JFreeChartTest.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName)
{
    PdfWriter writer = null;
    Document document = new Document();

    try
    {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte pdfContentByte = writer.getDirectContent();
        PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(50, 50);
        Graphics2D graphics2d = new PdfGraphics2D(pdfTemplateChartHolder, 50, 50);
        Rectangle2D chartRegion = new Rectangle2D.Double(0, 0, 50, 50);
        chart.draw(graphics2d, chartRegion);
        graphics2d.dispose();

        Image chartImage = Image.getInstance(pdfTemplateChartHolder);
        document.add(chartImage);

        PdfPTable table = new PdfPTable(5);
        // the cell object
        // we add a cell with colspan 3

        PdfPCell cellX = new PdfPCell(new Phrase("A"));
        cellX.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
        cellX.setRowspan(6);
        table.addCell(cellX);

        PdfPCell cellA = new PdfPCell(new Phrase("A"));
        cellA.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
        cellA.setColspan(4);
        table.addCell(cellA);

        PdfPCell cellB = new PdfPCell(new Phrase("B"));
        table.addCell(cellB);
        PdfPCell cellC = new PdfPCell(new Phrase("C"));
        table.addCell(cellC);
        PdfPCell cellD = new PdfPCell(new Phrase("D"));
        table.addCell(cellD);
        PdfPCell cellE = new PdfPCell(new Phrase("E"));
        table.addCell(cellE);
        PdfPCell cellF = new PdfPCell(new Phrase("F"));
        table.addCell(cellF);
        PdfPCell cellG = new PdfPCell(new Phrase("G"));
        table.addCell(cellG);
        PdfPCell cellH = new PdfPCell(new Phrase("H"));
        table.addCell(cellH);
        PdfPCell cellI = new PdfPCell(new Phrase("I"));
        table.addCell(cellI);

        PdfPCell cellJ = new PdfPCell(new Phrase("J"));
        cellJ.setColspan(2);
        cellJ.setRowspan(3);
        //instead of
        //  cellJ.setImage(chartImage);
        //the OP now uses
        Chunk chunk = new Chunk(chartImage, 20, -50);
        cellJ.addElement(chunk);
        //presumably with different contents of the other cells at hand
        table.addCell(cellJ);

        PdfPCell cellK = new PdfPCell(new Phrase("K"));
        cellK.setColspan(2);
        table.addCell(cellK);
        PdfPCell cellL = new PdfPCell(new Phrase("L"));
        cellL.setColspan(2);
        table.addCell(cellL);
        PdfPCell cellM = new PdfPCell(new Phrase("M"));
        cellM.setColspan(2);
        table.addCell(cellM);

        document.add(table);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    document.close();
}
 
Example #10
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
protected Chunk createChunk(TextChunk textChunk) {
	Chunk chunk = new Chunk(textChunk.getText());
	chunk.setFont(createFont(textChunk));
	return chunk;
}
 
Example #11
Source File: PDFView.java    From Spring-MVC-Blueprints with MIT License 5 votes vote down vote up
protected void buildPdfDocument(        
		Map<String, Object> model,        
		Document document,        
		PdfWriter writer,        
		HttpServletRequest req,        
		HttpServletResponse resp)        
				throws Exception {
	
	
	// Get data "articles" from model
	@SuppressWarnings("unchecked")
	List<HrmsLogin> users = (List<HrmsLogin>) model.get("allUsers");
	
	// Fonts
	Font fontTitle = new Font(FontFamily.TIMES_ROMAN, 14, Font.BOLD, BaseColor.BLACK);
	Font fontTag = new Font(FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.WHITE);

	for(HrmsLogin user: users){

		// 1.Title
		document.add(new Chunk("Employee ID: "));
		Chunk title = new Chunk(user.getHrmsEmployeeDetails().getEmpId()+"", fontTitle);
		document.add(title);
		document.add(new Chunk(" "));

		// -- newline
		document.add(Chunk.NEWLINE);

		// 2.URL
		document.add(new Chunk("Username: "));
		Chunk title2 = new Chunk(user.getUsername(), fontTitle);
		document.add(title2);
		document.add(new Chunk(" "));
		
		// -- newline
		document.add(Chunk.NEWLINE);

		// 3.Categories
		document.add(new Chunk("Password: "));
		Chunk title3 = new Chunk(user.getPassword(), fontTitle);
		document.add(title3);
		document.add(new Chunk(" "));
		
		// -- newline
		document.add(Chunk.NEWLINE);
		
		// 4.Tags
		document.add(new Chunk("Employee ID: "));
		Chunk title4 = new Chunk(user.getRole(), fontTitle);
		document.add(title4);
		document.add(new Chunk(" "));
		
		// -- newline
		document.add(Chunk.NEWLINE);
		document.add(Chunk.NEWLINE);

	}
	

}