com.itextpdf.text.Font.FontFamily Java Examples

The following examples show how to use com.itextpdf.text.Font.FontFamily. 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: 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 #2
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 #3
Source File: ImportPageWithoutFreeSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method creates a PDF with a single styled paragraph.
 */
static byte[] createSimpleTextPdf() throws DocumentException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, baos);
    document.open();

    Paragraph paragraph = new Paragraph();
    paragraph.add(new Phrase("Beware: ", new Font(FontFamily.HELVETICA, 12, Font.BOLDITALIC)));
    paragraph.add(new Phrase("The implementation of ", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
    paragraph.add(new Phrase("MarginFinder", new Font(FontFamily.COURIER, 12, Font.ITALIC)));
    paragraph.add(new Phrase(" is far from optimal. It is not even correct as it includes all curve control points which is too much. Furthermore it ignores stuff like line width or wedge types. It actually merely is a proof-of-concept.", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
    document.add(paragraph);

    document.close();

    return baos.toByteArray();
}
 
Example #4
Source File: ImportPageWithoutFreeSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/31980979/itext-importing-styled-text-and-informations-from-an-existing-pdf">
 * iText: Importing styled Text and informations from an existing PDF
 * </a>
 * <p>
 * This method demonstrates how to import merely the region of a PDF page with
 * actual content. The main necessity is to call {@link #cropPdf(PdfReader)}
 * for the reader in question which restricts the media boxes of the pages to
 * the bounding box of the existing content.
 * </p>
 */
@Test
public void testImportPages() throws DocumentException, IOException
{
    byte[] docText = createSimpleTextPdf();
    Files.write(new File(RESULT_FOLDER, "textOnly.pdf").toPath(), docText);
    byte[] docGraphics = createSimpleCircleGraphicsPdf();
    Files.write(new File(RESULT_FOLDER, "graphicsOnly.pdf").toPath(), docGraphics);

    PdfReader readerText = new PdfReader(docText);
    cropPdf(readerText);
    PdfReader readerGraphics = new PdfReader(docGraphics);
    cropPdf(readerGraphics);
    try (   FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "importPages.pdf")))
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(new Paragraph("Let's import 'textOnly.pdf'", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
        document.add(Image.getInstance(writer.getImportedPage(readerText, 1)));
        document.add(new Paragraph("and now 'graphicsOnly.pdf'", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
        document.add(Image.getInstance(writer.getImportedPage(readerGraphics, 1)));
        document.add(new Paragraph("That's all, folks!", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));

        document.close();
    }
    finally
    {
        readerText.close();
        readerGraphics.close();
    }
}
 
Example #5
Source File: WatermarkPdfTests.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
/**
 * pdf 用文字加水印,存在问题,如何支持中文
 * @author eko.zhan at 2018年9月2日 下午1:44:40
 * @throws FileNotFoundException
 * @throws IOException
 * @throws DocumentException
 */
@Test
public void testVisioAsPdfWithText() throws FileNotFoundException, IOException, DocumentException{
	File inputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx.vsdx");
	File outputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice.pdf");
	if (!outputFile.exists()) {
		convert(inputFile, outputFile);
	}
	File destFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice_watermark.pdf");
	//转换成 pdf 后利用 itext 加水印 
	PdfReader reader = new PdfReader(new FileInputStream(outputFile));
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
	int pageNo = reader.getNumberOfPages();
	Font f = new Font(FontFamily.HELVETICA, 28);
	Phrase p = new Phrase("Xiaoi Robot", f);
	for (int i=1;i<=pageNo;i++) {
		PdfContentByte over = stamper.getOverContent(i);
		over.saveState();
		PdfGState gs1 = new PdfGState();
		gs1.setFillOpacity(0.5f);
		over.setGState(gs1);
		ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 450, 0);
		over.restoreState();
	}
	stamper.close();
	reader.close();
}
 
Example #6
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);

	}
	

}
 
Example #7
Source File: FindFreeSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
public static PdfPTable getFooterTable(int x, int y)
{
    java.util.Date date = new java.util.Date();

    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");

    String month = sdf.format(date);
    System.out.println("Month : " + month);

    PdfPTable table = new PdfPTable(1);

    table.setTotalWidth(120);
    table.setLockedWidth(true);

    table.getDefaultCell().setFixedHeight(20);
    table.getDefaultCell().setBorder(Rectangle.TOP);
    table.getDefaultCell().setBorder(Rectangle.LEFT);
    table.getDefaultCell().setBorder(Rectangle.RIGHT);
    table.getDefaultCell().setBorderColorTop(BaseColor.BLUE);
    table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
    table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
    table.getDefaultCell().setBorderWidthTop(1f);
    table.getDefaultCell().setBorderWidthLeft(1f);
    table.getDefaultCell().setBorderWidthRight(1f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    Font font1 = new Font(FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE);

    table.addCell(new Phrase("CONTROLLED COPY", font1));

    table.getDefaultCell().setFixedHeight(20);
    table.getDefaultCell().setBorder(Rectangle.LEFT);
    table.getDefaultCell().setBorder(Rectangle.RIGHT);
    table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
    table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
    table.getDefaultCell().setBorderWidthLeft(1f);
    table.getDefaultCell().setBorderWidthRight(1f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    Font font = new Font(FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.RED);

    table.addCell(new Phrase(month, font));

    table.getDefaultCell().setFixedHeight(20);
    table.getDefaultCell().setBorder(Rectangle.LEFT);
    table.getDefaultCell().setBorder(Rectangle.RIGHT);
    table.getDefaultCell().setBorder(Rectangle.BOTTOM);
    table.getDefaultCell().setBorderColorLeft(BaseColor.BLUE);
    table.getDefaultCell().setBorderColorRight(BaseColor.BLUE);
    table.getDefaultCell().setBorderColorBottom(BaseColor.BLUE);
    table.getDefaultCell().setBorderWidthLeft(1f);
    table.getDefaultCell().setBorderWidthRight(1f);
    table.getDefaultCell().setBorderWidthBottom(1f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.addCell(new Phrase("BLR DESIGN DEPT.", font1));

    return table;
}
 
Example #8
Source File: WatermarkPdfTests.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
/**
 * pdf 用图片加水印
 * @author eko.zhan at 2018年9月2日 下午1:44:58
 * @throws FileNotFoundException
 * @throws IOException
 * @throws DocumentException
 */
@Test
public void testVisioAsPdfWithImg() throws FileNotFoundException, IOException, DocumentException{
	File inputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx.vsdx");
	File outputFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice.pdf");
	if (!outputFile.exists()) {
		convert(inputFile, outputFile);
	}
	File destFile = new File("E:/ConvertTester/TestFiles/I_am_a_vsdx_libreoffice_watermark.pdf");
	final String IMG = "D:\\Xiaoi\\logo\\logo.png";
	//转换成 pdf 后利用 itext 加水印 
	PdfReader reader = new PdfReader(new FileInputStream(outputFile));
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
	int pageNo = reader.getNumberOfPages();
	// text watermark
	Font f = new Font(FontFamily.HELVETICA, 30);
	Phrase p = new Phrase("Xiaoi Robot Image", f);
	// image watermark
	Image img = Image.getInstance(IMG);
	float w = img.getScaledWidth();
	float h = img.getScaledHeight();
	// transparency
	PdfGState gs1 = new PdfGState();
	gs1.setFillOpacity(0.5f);
	// properties
	PdfContentByte over;
	Rectangle pagesize;
	float x, y;
	// loop over every page
	for (int i = 1; i <= pageNo; i++) {
		pagesize = reader.getPageSizeWithRotation(i);
		x = (pagesize.getLeft() + pagesize.getRight()) / 2;
		y = (pagesize.getTop() + pagesize.getBottom()) / 2;
		over = stamper.getOverContent(i);
		over.saveState();
		over.setGState(gs1);
		if (i % 2 == 1)
			ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 0);
		else
			over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
		over.restoreState();
	}
	stamper.close();
	reader.close();
}