Java Code Examples for com.lowagie.text.pdf.BaseFont#createFont()

The following examples show how to use com.lowagie.text.pdf.BaseFont#createFont() . 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: BulkReceivingPdf.java    From kfs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Overrides the method in PdfPageEventHelper from itext to create and set the headerTable and set its logo image if 
 * there is a logoImage to be used, creates and sets the nestedHeaderTable and its content.
 * 
 * @param writer    The PdfWriter for this document.
 * @param document  The document.
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onOpenDocument(PdfWriter writer, Document document) {
    try {
        
        loadHeaderTable();
        
        // initialization of the template
        tpl = writer.getDirectContent().createTemplate(100, 100);
        
        // initialization of the font
        helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
        
    }catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
 
Example 2
Source File: OpenTypeFontTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Using oth
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2
	PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("opentypefont.pdf"));
	// step 3
	document.open();
	// step 4
	BaseFont bf = BaseFont.createFont(PdfTestBase.RESOURCES_DIR
			+ "liz.otf", BaseFont.CP1252, true);
	String text = "Some text with the otf font LIZ.";
	document.add(new Paragraph(text, new Font(bf, 14)));
	// step 5
	document.close();
}
 
Example 3
Source File: FullFontNamesTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Retrieving the full font name
 */
@Test
public void main() throws Exception {

	
	File font = new File(PdfTestBase.RESOURCES_DIR + "liberation-fonts-ttf/LiberationMono-Regular.ttf");
	BufferedWriter out = new BufferedWriter(new FileWriter(PdfTestBase.OUTPUT_DIR
			+ "fullfontname_liberationmono.txt"));
	BaseFont bf = BaseFont.createFont(font.getAbsolutePath(), "winansi", BaseFont.NOT_EMBEDDED);
	out.write("postscriptname: " + bf.getPostscriptFontName());
	out.write("\r\n\r\n");
	String names[][] = bf.getFullFontName();
	out.write("\n\nListing the full font name:\n\n");
	for (int k = 0; k < names.length; ++k) {
		if (names[k][0].equals("3") && names[k][1].equals("1")) {
			 // Microsoftencoding
			out.write(names[k][3] + "\r\n");
		}
	}
	out.flush();
	out.close();

}
 
Example 4
Source File: ListEncodingsTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Listing the encodings of font comic.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {

	File font = new File(PdfTestBase.RESOURCES_DIR + "liberation-fonts-ttf/LiberationMono-Regular.ttf");
	BufferedWriter out = new BufferedWriter(new FileWriter(PdfTestBase.OUTPUT_DIR + "encodings.txt"));
	BaseFont bfComic = BaseFont.createFont(font.getAbsolutePath(), BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	out.write("postscriptname: " + bfComic.getPostscriptFontName());
	out.write("\r\n\r\n");
	String[] codePages = bfComic.getCodePagesSupported();
	out.write("All available encodings:\n\n");
	for (int i = 0; i < codePages.length; i++) {
		out.write(codePages[i]);
		out.write("\r\n");
	}
	out.flush();
	out.close();

}
 
Example 5
Source File: TextOnlySignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
private BaseFont getBaseFont(DSSFont dssFont) {
	if (dssFont instanceof ITextNativeFont) {
		ITextNativeFont nativeFont = (ITextNativeFont) dssFont;
		return nativeFont.getFont();
	} else if (dssFont instanceof DSSFileFont) {
		DSSFileFont fileFont = (DSSFileFont) dssFont;
		try (InputStream iStream = fileFont.getInputStream()) {
			byte[] fontBytes = DSSUtils.toByteArray(iStream);
			BaseFont baseFont = BaseFont.createFont(fileFont.getName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontBytes, null);
			baseFont.setSubset(false);
			return baseFont;
		} catch (IOException e) {
			throw new DSSException("The iText font cannot be initialized", e);
		}
	} else {
		DefaultFontMapper fontMapper = new DefaultFontMapper();
		return fontMapper.awtToPdf(dssFont.getJavaFont());
	}
}
 
Example 6
Source File: VerticalTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Writing vertical text.
 */
@Test
public void main() throws Exception {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	texts[3] = convertCid(texts[0]);
	texts[4] = convertCid(texts[1]);
	texts[5] = convertCid(texts[2]);
	PdfWriter writer = PdfWriter.getInstance(document,PdfTestBase.getOutputStream("vertical.pdf"));
	int idx = 0;
	document.open();
	PdfContentByte cb = writer.getDirectContent();
	for (int j = 0; j < 2; ++j) {
		BaseFont bf = BaseFont.createFont("KozMinPro-Regular", encs[j],	false);
		cb.setRGBColorStroke(255, 0, 0);
		cb.setLineWidth(0);
		float x = 400;
		float y = 700;
		float height = 400;
		float leading = 30;
		int maxLines = 6;
		for (int k = 0; k < maxLines; ++k) {
			cb.moveTo(x - k * leading, y);
			cb.lineTo(x - k * leading, y - height);
		}
		cb.rectangle(x, y, -leading * (maxLines - 1), -height);
		cb.stroke();
		VerticalText vt = new VerticalText(cb);
		vt.setVerticalLayout(x, y, height, maxLines, leading);
		vt.addText(new Chunk(texts[idx++], new Font(bf, 20)));
		vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.blue)));
		vt.go();
		vt.setAlignment(Element.ALIGN_RIGHT);
		vt.addText(new Chunk(texts[idx++],	new Font(bf, 20, 0, Color.orange)));
		vt.go();
		document.newPage();
	}
	document.close();

}
 
Example 7
Source File: ShadingPatternTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Shading example.
 */
@Test
public void main() throws Exception {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("shading_pattern.pdf"));
	document.open();

	PdfShading shading = PdfShading.simpleAxial(writer, 100, 100, 400, 100,
			Color.red, Color.cyan);
	PdfShadingPattern shadingPattern = new PdfShadingPattern(shading);
	PdfContentByte cb = writer.getDirectContent();
	BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD,
			BaseFont.WINANSI, false);
	cb.setShadingFill(shadingPattern);
	cb.beginText();
	cb.setTextMatrix(100, 100);
	cb.setFontAndSize(bf, 40);
	cb.showText("Look at this text!");
	cb.endText();
	PdfShading shadingR = PdfShading.simpleRadial(writer, 200, 500, 50,
			300, 500, 100, new Color(255, 247, 148), new Color(247, 138,
					107), false, false);
	cb.paintShading(shadingR);
	cb.sanityCheck();
	document.close();

}
 
Example 8
Source File: AdvancedPageNumberEvents.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * we override the onOpenDocument method.
 *
 * @param writer
 *           PdfWriter
 * @param document
 *           Document
 */
@Override
public void onOpenDocument(final PdfWriter writer, final Document document) {
	try {
		bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		cb = writer.getDirectContent();
		template = cb.createTemplate(50, 50);
	} catch (final DocumentException de) {
		throw new IllegalStateException(de);
	} catch (final IOException ioe) {
		throw new IllegalStateException(ioe);
	}
}
 
Example 9
Source File: AffineTransformationTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changes the transformation matrix with AffineTransform.
 */
@Test
public void main() throws Exception {

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

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

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

	// step 4:
	PdfContentByte cb = writer.getDirectContent();
	cb.transform(AffineTransform.getScaleInstance(1.2, 0.75));

	// we create a PdfTemplate
	PdfTemplate template = cb.createTemplate(25, 25);

	// we add some crosses to visualize the coordinates
	template.moveTo(13, 0);
	template.lineTo(13, 25);
	template.moveTo(0, 13);
	template.lineTo(50, 13);
	template.stroke();
	template.sanityCheck();

	// we add the template on different positions
	cb.addTemplate(template, 216 - 13, 720 - 13);
	cb.addTemplate(template, 360 - 13, 360 - 13);
	cb.addTemplate(template, 360 - 13, 504 - 13);
	cb.addTemplate(template, 72 - 13, 144 - 13);
	cb.addTemplate(template, 144 - 13, 288 - 13);

	cb.moveTo(216, 720);
	cb.lineTo(360, 360);
	cb.lineTo(360, 504);
	cb.lineTo(72, 144);
	cb.lineTo(144, 288);
	cb.stroke();

	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
			BaseFont.NOT_EMBEDDED);
	cb.beginText();
	cb.setFontAndSize(bf, 12);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(3\" * 1.2, 10\" * .75)", 216 + 25, 720 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(5\" * 1.2, 5\" * .75)", 360 + 25, 360 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(5\" * 1.2, 7\" * .75)", 360 + 25, 504 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(1\" * 1.2, 2\" * .75)", 72 + 25, 144 + 5, 0);
	cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
			"(2\" * 1.2, 4\" * .75)", 144 + 25, 288 + 5, 0);
	cb.endText();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 10
Source File: ComplexTextTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding text at absolute positions.
 * 
 * @param args
 *            no arguments needed
 */
@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("complextext.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();

	// first we draw some lines to be able to visualize the text alignment
	// functions
	cb.setLineWidth(0f);
	cb.moveTo(250, 500);
	cb.lineTo(250, 800);
	cb.moveTo(50, 700);
	cb.lineTo(400, 700);
	cb.moveTo(50, 650);
	cb.lineTo(400, 650);
	cb.moveTo(50, 600);
	cb.lineTo(400, 600);
	cb.stroke();

	File font = new File (PdfTestBase.RESOURCES_DIR + "/liberation-fonts-ttf/LiberationSans-Regular.ttf");
	// we construct a font
	BaseFont bf = BaseFont.createFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, true);
	Font ft = new Font(bf, 12);
	// This is the text:
	String text = "\u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a \u0627\u0644\u062d\u0627\u0633\u0648\u0628 + \u0627\u0646\u062a\u0631\u0646\u064a\u062a :";
	Phrase center = new Phrase(text + " Center", ft);
	ColumnText
			.showTextAligned(cb, PdfContentByte.ALIGN_CENTER, center, 250, 700, 0, PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_RIGHT, new Phrase(text + " Right", ft), 250, 650, 20,
			PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, new Phrase("Some text Left aligned", ft), 250, 600,
			20);
	float size = ColumnText.getWidth(center, PdfWriter.RUN_DIRECTION_RTL, 0);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(250 - size / 2, 690, size, 30);
	cb.stroke();

	// step 5: we close the document
	document.close();
}
 
Example 11
Source File: SpotColorsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Demonstrates the use of spotcolors.
    */
@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("spotcolor.pdf"));
           BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
           
           // step 3: we open the document
           document.open();
           
           // step 4: we grab the ContentByte and do some stuff with it
           PdfContentByte cb = writer.getDirectContent();
           
           // step 5: we instantiate PdfSpotColor
           
           // Note: I made up these names unless someone give me a PANTONE swatch as gift ([email protected])
           PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", new CMYKColor(0.9f, .2f, .3f, .1f));
           PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", new Color(114, 94, 38));
           PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", new GrayColor(0.9f));
           
           // Stroke a rectangle with CMYK alternate
           cb.setColorStroke(spc_cmyk, .5f);
           cb.setLineWidth(10f);
           // draw a rectangle
           cb.rectangle(100, 700, 100, 100);
           // add the diagonal
           cb.moveTo(100, 700);
           cb.lineTo(200, 800);
           // stroke the lines
           cb.stroke();
           
           // Fill a rectangle with CMYK alternate
           cb.setColorFill(spc_cmyk, 0.25f);
           cb.rectangle(250, 700, 100, 100);
           cb.fill();
           
           // Stroke a circle with RGB alternate
           cb.setColorStroke(spc_rgb, 0.9f);
           cb.setLineWidth(5f);
           cb.circle(150f, 500f, 100f);
           cb.stroke();
           
           // Fill the circle with RGB alternate
           cb.setColorFill(spc_rgb, 0.9f);
           cb.circle(150f, 500f, 50f);
           cb.fill();
           
           // example with colorfill
           cb.setColorFill(spc_g, 0.5f);
           cb.moveTo(100f, 200f);
           cb.lineTo(200f, 250f);
           cb.lineTo(400f, 150f);
           cb.fill();
           // cb.sanityCheck is called during newPage().
           document.newPage();
           String text = "Some text to show";
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.25f))));
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
           
           // example with template
           PdfTemplate t = cb.createTemplate(500f, 500f);
           // Stroke a rectangle with CMYK alternate
           t.setColorStroke(new SpotColor(spc_cmyk, .5f));
           t.setLineWidth(10f);
           // draw a rectangle
           t.rectangle(100, 10, 100, 100);
           // add the diagonal
           t.moveTo(100, 10);
           t.lineTo(200, 100);
           // stroke the lines
           t.stroke();
           
           // Fill a rectangle with CMYK alternate
           t.setColorFill(spc_g, 0.5f);
           t.rectangle(100, 125, 100, 100);
           t.fill();
           t.beginText();
           t.setFontAndSize(bf, 20f);
           t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
           t.showText("Template text upside down");
           t.endText();
           t.rectangle(0, 0, 499, 499);
           t.stroke();
           t.sanityCheck();
           cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
           
           cb.sanityCheck();

       
       // step 5: we close the document
       document.close();
   }
 
Example 12
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 13
Source File: MetaFont.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public BaseFont getFont() {
    if (font != null)
        return font;
    Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? Font.ITALIC : 0) | ((bold != 0) ? Font.BOLD : 0));
    font = ff2.getBaseFont();
    if (font != null)
        return font;
    String fontName;
    if (faceName.indexOf("courier") != -1 || faceName.indexOf("terminal") != -1
        || faceName.indexOf("fixedsys") != -1) {
        fontName = fontNames[MARKER_COURIER + italic + bold];
    }
    else if (faceName.indexOf("ms sans serif") != -1 || faceName.indexOf("arial") != -1
        || faceName.indexOf("system") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + bold];
    }
    else if (faceName.indexOf("arial black") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
    }
    else if (faceName.indexOf("times") != -1 || faceName.indexOf("ms serif") != -1
        || faceName.indexOf("roman") != -1) {
        fontName = fontNames[MARKER_TIMES + italic + bold];
    }
    else if (faceName.indexOf("symbol") != -1) {
        fontName = fontNames[MARKER_SYMBOL];
    }
    else {
        int pitch = pitchAndFamily & 3;
        int family = (pitchAndFamily >> 4) & 7;
        switch (family) {
            case FF_MODERN:
                fontName = fontNames[MARKER_COURIER + italic + bold];
                break;
            case FF_ROMAN:
                fontName = fontNames[MARKER_TIMES + italic + bold];
                break;
            case FF_SWISS:
            case FF_SCRIPT:
            case FF_DECORATIVE:
                fontName = fontNames[MARKER_HELVETICA + italic + bold];
                break;
            default:
            {
                switch (pitch) {
                    case FIXED_PITCH:
                        fontName = fontNames[MARKER_COURIER + italic + bold];
                        break;
                    default:
                        fontName = fontNames[MARKER_HELVETICA + italic + bold];
                        break;
                }
            }
        }
    }
    try {
        font = BaseFont.createFont(fontName, "Cp1252", false);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    
    return font;
}
 
Example 14
Source File: XandYcoordinatesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Creates a PDF document with shapes, lines and text at specific X and Y coordinates.
    */
@Test
public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document();
       
       try {      
           // step 2: creation of the writer
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "XandY.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4:
           PdfContentByte cb = writer.getDirectContent();
           
           // we create a PdfTemplate
           PdfTemplate template = cb.createTemplate(25, 25);
           
           // we add some crosses to visualize the coordinates
           template.moveTo(13, 0);
           template.lineTo(13, 25);
           template.moveTo(0, 13);
           template.lineTo(50, 13);
           template.stroke();
           template.sanityCheck();
           
           // we add the template on different positions
           cb.addTemplate(template, 216 - 13, 720 - 13);
           cb.addTemplate(template, 360 - 13, 360 - 13);
           cb.addTemplate(template, 360 - 13, 504 - 13);
           cb.addTemplate(template, 72 - 13, 144 - 13);
           cb.addTemplate(template, 144 - 13, 288 - 13);

           cb.moveTo(216, 720);
           cb.lineTo(360, 360);
           cb.lineTo(360, 504);
           cb.lineTo(72, 144);
           cb.lineTo(144, 288);
           cb.stroke();
           
           BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.beginText();
           cb.setFontAndSize(bf, 12);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
           cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
           cb.endText(); 
           
           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: BaseFontSupport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Creates a PDF font record from a true type font.
 *
 * @param encoding       the encoding.
 * @param stringEncoding the string encoding.
 * @param embedded       a flag indicating whether to embed the font glyphs in the generated documents.
 * @return the PDF font record.
 * @throws com.lowagie.text.DocumentException if the BaseFont could not be created.
 */
private BaseFontRecord createFontFromTTF( final FontRecord fontRecord,
                                          final boolean bold,
                                          final boolean italic,
                                          final String encoding,
                                          final String stringEncoding,
                                          final boolean embedded )
  throws DocumentException {
  // check if this font is in the cache ...
  //Log.warn ("TrueTypeFontKey : " + fontKey + " Font: " + font.isItalic() + " Encoding: "
  //          + encoding);
  final String rawFilename;
  if ( fontRecord instanceof TrueTypeFontRecord ) {
    final TrueTypeFontRecord ttfRecord = (TrueTypeFontRecord) fontRecord;
    if ( ttfRecord.getCollectionIndex() >= 0 ) {
      rawFilename = ttfRecord.getFontSource() + ',' + ttfRecord.getCollectionIndex();
    } else {
      rawFilename = ttfRecord.getFontSource();
    }
  } else if ( fontRecord instanceof FontSource ) {
    final FontSource source = (FontSource) fontRecord;
    rawFilename = source.getFontSource();
  } else {
    return null;
  }

  final String filename;
  // check, whether the the physical font does not provide some of the
  // required styles. We have to synthesize them, if neccessary
  if ( ( fontRecord.isBold() == false && bold ) &&
    ( fontRecord.isItalic() == false && italic ) ) {
    filename = rawFilename + ",BoldItalic";
  } else if ( fontRecord.isBold() == false && bold ) {
    filename = rawFilename + ",Bold";
  } else if ( fontRecord.isItalic() == false && italic ) {
    filename = rawFilename + ",Italic";
  } else {
    filename = rawFilename;
  }

  final BaseFontRecord fontRec = getFromCache( filename, encoding, embedded );
  if ( fontRec != null ) {
    return fontRec;
  }

  BaseFont f;
  try {
    try {
      f = BaseFont.createFont( filename, encoding, embedded, false, null, null );
    } catch ( DocumentException e ) {
      f = BaseFont.createFont( filename, stringEncoding, embedded, false, null, null );
    }
  } catch ( IOException ioe ) {
    throw new DocumentException( "Failed to read the font: " + ioe );
  }

  // no, we have to create a new instance
  final BaseFontRecord record = new BaseFontRecord
    ( filename, true, embedded, f, fontRecord.isBold(), fontRecord.isItalic() );
  putToCache( record );
  return record;
}
 
Example 16
Source File: FormTextFieldTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a TextField
 */
@Test
public void main() throws Exception {

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

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

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

	// step 4:
	BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	String text = "Some start text";
	float fontSize = 12;
	Color textColor = new GrayColor(0f);
	PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
	field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFlags(PdfAnnotation.FLAGS_PRINT);
	field.setFieldName("ATextField");
	field.setValueAsString(text);
	field.setDefaultValueAsString(text);
	field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID));
	field.setPage();
	PdfAppearance tp = cb.createAppearance(171, 19);
	PdfAppearance da = (PdfAppearance) tp.getDuplicate();
	da.setFontAndSize(helv, fontSize);
	da.setColorFill(textColor);
	field.setDefaultAppearanceString(da);
	tp.beginVariableText();
	tp.saveState();
	tp.rectangle(2, 2, 167, 15);
	tp.clip();
	tp.newPath();
	tp.beginText();
	tp.setFontAndSize(helv, fontSize);
	tp.setColorFill(textColor);
	tp.setTextMatrix(4, 5);
	tp.showText(text);
	tp.endText();
	tp.restoreState();
	tp.endVariableText();
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example 17
Source File: Font.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Gets the <CODE>BaseFont</CODE> this class represents. For the built-in
 * fonts a <CODE>BaseFont</CODE> is calculated.
 * 
 * @param specialEncoding
 *            <CODE>true</CODE> to use the special encoding for Symbol and
 *            ZapfDingbats, <CODE>false</CODE> to always use <CODE>Cp1252
 *            </CODE>
 * @return the <CODE>BaseFont</CODE> this class represents
 */
public BaseFont getCalculatedBaseFont(boolean specialEncoding) {
	if (baseFont != null)
		return baseFont;
	int style = this.style;
	if (style == UNDEFINED) {
		style = NORMAL;
	}
	String fontName = BaseFont.HELVETICA;
	String encoding = BaseFont.WINANSI;
	BaseFont cfont = null;
	switch (family) {
	case COURIER:
		switch (style & BOLDITALIC) {
		case BOLD:
			fontName = BaseFont.COURIER_BOLD;
			break;
		case ITALIC:
			fontName = BaseFont.COURIER_OBLIQUE;
			break;
		case BOLDITALIC:
			fontName = BaseFont.COURIER_BOLDOBLIQUE;
			break;
		default:
			//case NORMAL:
			fontName = BaseFont.COURIER;
			break;
		}
		break;
	case TIMES_ROMAN:
		switch (style & BOLDITALIC) {
		case BOLD:
			fontName = BaseFont.TIMES_BOLD;
			break;
		case ITALIC:
			fontName = BaseFont.TIMES_ITALIC;
			break;
		case BOLDITALIC:
			fontName = BaseFont.TIMES_BOLDITALIC;
			break;
		default:
		case NORMAL:
			fontName = BaseFont.TIMES_ROMAN;
			break;
		}
		break;
	case SYMBOL:
		fontName = BaseFont.SYMBOL;
		if (specialEncoding)
			encoding = BaseFont.SYMBOL;
		break;
	case ZAPFDINGBATS:
		fontName = BaseFont.ZAPFDINGBATS;
		if (specialEncoding)
			encoding = BaseFont.ZAPFDINGBATS;
		break;
	default:
	case Font.HELVETICA:
		switch (style & BOLDITALIC) {
		case BOLD:
			fontName = BaseFont.HELVETICA_BOLD;
			break;
		case ITALIC:
			fontName = BaseFont.HELVETICA_OBLIQUE;
			break;
		case BOLDITALIC:
			fontName = BaseFont.HELVETICA_BOLDOBLIQUE;
			break;
		default:
		case NORMAL:
			fontName = BaseFont.HELVETICA;
			break;
		}
		break;
	}
	try {
		cfont = BaseFont.createFont(fontName, encoding, false);
	} catch (Exception ee) {
		throw new ExceptionConverter(ee);
	}
	return cfont;
}
 
Example 18
Source File: MetaFont.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public BaseFont getFont() {
    if (font != null)
        return font;
    Font ff2 = FontFactory.getFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? Font.ITALIC : 0) | ((bold != 0) ? Font.BOLD : 0));
    font = ff2.getBaseFont();
    if (font != null)
        return font;
    String fontName;
    if (faceName.indexOf("courier") != -1 || faceName.indexOf("terminal") != -1
        || faceName.indexOf("fixedsys") != -1) {
        fontName = fontNames[MARKER_COURIER + italic + bold];
    }
    else if (faceName.indexOf("ms sans serif") != -1 || faceName.indexOf("arial") != -1
        || faceName.indexOf("system") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + bold];
    }
    else if (faceName.indexOf("arial black") != -1) {
        fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
    }
    else if (faceName.indexOf("times") != -1 || faceName.indexOf("ms serif") != -1
        || faceName.indexOf("roman") != -1) {
        fontName = fontNames[MARKER_TIMES + italic + bold];
    }
    else if (faceName.indexOf("symbol") != -1) {
        fontName = fontNames[MARKER_SYMBOL];
    }
    else {
        int pitch = pitchAndFamily & 3;
        int family = (pitchAndFamily >> 4) & 7;
        switch (family) {
            case FF_MODERN:
                fontName = fontNames[MARKER_COURIER + italic + bold];
                break;
            case FF_ROMAN:
                fontName = fontNames[MARKER_TIMES + italic + bold];
                break;
            case FF_SWISS:
            case FF_SCRIPT:
            case FF_DECORATIVE:
                fontName = fontNames[MARKER_HELVETICA + italic + bold];
                break;
            default:
            {
                switch (pitch) {
                    case FIXED_PITCH:
                        fontName = fontNames[MARKER_COURIER + italic + bold];
                        break;
                    default:
                        fontName = fontNames[MARKER_HELVETICA + italic + bold];
                        break;
                }
            }
        }
    }
    try {
        font = BaseFont.createFont(fontName, "Cp1252", false);
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    
    return font;
}
 
Example 19
Source File: ColumnTest.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: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("column.pdf"));

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

	// step 4:

	// we create some content
	BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
	Font font = new Font(bf, 11, Font.NORMAL);

	Phrase unicodes = new Phrase(15, "UNI\n", font);
	Phrase characters = new Phrase(15, "\n", font);
	Phrase names = new Phrase(15, "NAME\n", font);

	for (int i = 0; i < 27; i++) {
		unicodes.add(uni[i] + "\n");
		characters.add(code[i] + "\n");
		names.add(name[i] + "\n");
	}

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

	ColumnText ct = new ColumnText(cb);
	ct.setSimpleColumn(unicodes, 60, 300, 100, 300 + 28 * 15, 15, Element.ALIGN_CENTER);
	ct.go();
	cb.rectangle(103, 295, 52, 8 + 28 * 15);
	cb.stroke();
	ct.setSimpleColumn(characters, 105, 300, 150, 300 + 28 * 15, 15, Element.ALIGN_RIGHT);
	ct.go();
	ct.setSimpleColumn(names, 160, 300, 500, 300 + 28 * 15, 15, Element.ALIGN_LEFT);
	ct.go();

	// step 5: we close the document
	document.close();
}
 
Example 20
Source File: CashReceiptCoverSheetServiceImpl.java    From kfs with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Read-only accessor for <code>{@link BaseFont}</code>. Used for creating the check detail information.  The font being
 * used is  Helvetica.
 *
 * @return A BaseFont object used to identify what type of font is used on the cover sheet.
 */
protected BaseFont getTextFont() throws DocumentException, IOException {
    return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
}