Type 3 font

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

Logo.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie.
  10.  */
  11. package com.itextpdf.samples.sandbox.fonts;
  12.  
  13. import com.itextpdf.kernel.color.DeviceRgb;
  14. import com.itextpdf.kernel.font.PdfFontFactory;
  15. import com.itextpdf.kernel.font.PdfType3Font;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class Logo extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/fonts/logo.pdf";
  32.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new Logo().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  43.         Document doc = new Document(pdfDoc);
  44.  
  45.         float linewidth = 125;
  46.  
  47.         PdfType3Font t3 = PdfFontFactory.createType3Font(pdfDoc, true);
  48.         PdfCanvas i = t3.addGlyph('I', 700, 0, 0, 1200, 600);
  49.         i.setLineWidth(10);
  50.         i.setStrokeColor(new DeviceRgb(0xf9, 0x9d, 0x25));
  51.         i.setLineWidth(linewidth);
  52.         i.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
  53.         i.moveTo(600, 36);
  54.         i.lineTo(600, 564);
  55.         i.stroke();
  56.  
  57.         PdfCanvas t = t3.addGlyph('T', 1170, 0, 0, 1200, 600);
  58.         t.setLineWidth(10);
  59.         t.setStrokeColor(new DeviceRgb(0x08, 0x49, 0x75));
  60.         t.setLineWidth(linewidth);
  61.         t.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
  62.         t.moveTo(144, 564);
  63.         t.lineTo(1056, 564);
  64.         t.moveTo(600, 36);
  65.         t.lineTo(600, 564);
  66.         t.stroke();
  67.  
  68.         PdfCanvas e = t3.addGlyph('E', 1150, 0, 0, 1200, 600);
  69.         e.setLineWidth(10);
  70.         e.setStrokeColor(new DeviceRgb(0xf8, 0x9b, 0x22));
  71.         e.setLineWidth(linewidth);
  72.         e.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
  73.         e.moveTo(144, 36);
  74.         e.lineTo(1056, 36);
  75.         e.moveTo(144, 300);
  76.         e.lineTo(1056, 300);
  77.         e.moveTo(144, 564);
  78.         e.lineTo(1056, 564);
  79.         e.stroke();
  80.  
  81.         PdfCanvas x = t3.addGlyph('X', 1160, 0, 0, 1200, 600);
  82.         x.setStrokeColor(new DeviceRgb(0x10, 0x46, 0x75));
  83.         x.setLineWidth(10);
  84.         x.setLineWidth(linewidth);
  85.         x.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
  86.         x.moveTo(144, 36);
  87.         x.lineTo(1056, 564);
  88.         x.moveTo(144, 564);
  89.         x.lineTo(1056, 36);
  90.         x.stroke();
  91.  
  92.         Paragraph p = new Paragraph("ITEXT").setFont(t3).setFontSize(20);
  93.         doc.add(p);
  94.  
  95.         p = new Paragraph("I\nT\nE\nX\nT").setFixedLeading(20).setFont(t3).setFontSize(20);
  96.         doc.add(p);
  97.  
  98.         doc.close();
  99.     }
  100. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now