Java Tutorial/PDF

Материал из Java эксперт

Перейти к: навигация, поиск

Содержание

.afm font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("putr8a.afm", "", BaseFont.EMBEDDED);
    Font font = new Font(bf, 12);
    document.add(new Paragraph("0123456789\nabcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXZ",
        font));
    bf = BaseFont.createFont("cmr10.afm", "", BaseFont.EMBEDDED);
    font = new Font(bf, 12);
    document.add(new Paragraph("0123456789\nabcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXZ",
        font));
    document.close();
  }
}





All kinds of fonts

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Font[] fonts = new Font[14];
    fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL);
    fonts[1] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.ITALIC);
    fonts[2] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD);
    fonts[3] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC);
    fonts[4] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL);
    fonts[5] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC);
    fonts[6] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
    fonts[7] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC);
    fonts[8] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL);
    fonts[9] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);
    fonts[10] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD);
    fonts[11] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC);
    fonts[12] = new Font(Font.SYMBOL, Font.DEFAULTSIZE);
    fonts[13] = new Font(Font.ZAPFDINGBATS, Font.DEFAULTSIZE, Font.UNDEFINED, new Color(0xFF, 0x00,
        0x00));
    // add the content
    for (int i = 0; i < 14; i++) {
      document.add(new Paragraph("quick brown fox jumps over the lazy dog", fonts[i]));
    }
    document.close();
  }
}





BaseFont.createFont(BaseFont.HELVETICA,BaseFont.WINANSI, BaseFont.NOT_EMBEDDED)

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfTextArray;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(
          document,
          new FileOutputStream("2.pdf"));
      document.open();
      String text = "jexp";
      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
          BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
      cb.beginText();
      cb.moveText(36, 806);
      cb.setFontAndSize(bf, 24);
      cb.moveTextWithLeading(0, -36);
      cb.showText(text);
      cb.endText();
    document.close();
  }
}





Create font

import java.io.FileOutputStream;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldRead.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStamper.pdf"));
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte over;
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
      over = stamper.getOverContent(i);
      over.beginText();
      over.setFontAndSize(bf, 18);
      over.setTextMatrix(30, 30);
      over.showText("page " + i);
      over.endText();
      over.setRGBColorStroke(0xFF, 0x00, 0x00);
      over.setLineWidth(5f);
      over.ellipse(250, 450, 350, 550);
      over.stroke();
    }
    stamper.close();
  }
}





Create font from pfm file

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("cmr10.pfm", "", BaseFont.EMBEDDED);
    Font font = new Font(bf, 12);
    document.add(new Paragraph("0123456789\nabcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXZ",
        font));
    document.close();
  }
}





Embeded font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
    System.err.println(bf.getClass().getName());
    Font font = new Font(bf, 12);
    document.add(new Paragraph("0123456789\nabcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXZ",
        font));
    document.close();
  }
}





Embeded TrueType Font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("ttf.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("c:/windows/fonts/ARBLI___.ttf", BaseFont.CP1252,
        BaseFont.EMBEDDED);
    Font font = new Font(bf, 12);
    System.err.println(bf.getClass().getName());
    document.add(new Paragraph("This is font arial black italic (embedded)", font));
    bf = BaseFont.createFont("c:/windows/fonts/ARBLI___.ttf", BaseFont.CP1252,
        BaseFont.NOT_EMBEDDED);
    font = new Font(bf, 12);
    document.add(new Paragraph("This is font arial black italic (not embedded)", font));
    System.out.println("PostScript name:" + bf.getPostscriptFontName());
    String[] encoding = bf.getCodePagesSupported();
    for (int i = 0; i < encoding.length; i++) {
      System.out.println("encoding[" + i + "] = " + encoding[i]);
    }
    document.newPage();
    String[][] name = bf.getFullFontName();
    for (int i = 0; i < name.length; i++) {
      System.out.println(name[i][3] + " (" + name[i][0] + "; " + name[i][1] + "; " + name[i][2]
          + ")");
    }
    document.close();
  }
}





Font Metrics

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Font font = new Font(Font.HELVETICA, 12);
    BaseFont bf = font.getCalculatedBaseFont(false);
    String numbers = "0123456789";
    String letters = "abcdefghijklmnopqrstuvwxyz";
    document.add(new Paragraph(numbers, font));
    System.out.println("width: " + bf.getWidth(numbers) + " (" + bf.getWidthPoint(numbers, 12)
        + "pt)");
    System.out.println("ascent: " + bf.getAscent(numbers) + "; descent: " + bf.getDescent(numbers)
        + "; height: " + (bf.getAscentPoint(numbers, 12) - bf.getDescentPoint(numbers, 12) + "pt"));
    document.add(new Paragraph(letters, font));
    System.out.println("width: " + bf.getWidth(letters) + " (" + bf.getWidthPoint(letters, 12)
        + "pt)");
    System.out.println("ascent: " + bf.getAscent(letters) + "; descent: " + bf.getDescent(letters)
        + "; height: " + (bf.getAscentPoint(letters, 12) - bf.getDescentPoint(letters, 12)) + "pt");
    document.close();
  }
}





Font supporting Unicode

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args)throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
      document.open();
      BaseFont bf;
      Font font;
      bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
          BaseFont.NOT_EMBEDDED);
      font = new Font(bf, 12);
      document.add(new Paragraph("test", font));
      document.add(new Paragraph("Font: " + bf.getPostscriptFontName(),font));
      document.add(new Paragraph("\u5341\u950a\u57cb\u4f0f", font));
    document.close();
  }
}





Font.UNDERLINE

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Font font = new Font();
    font.setStyle(Font.UNDERLINE);
    Chunk chunk = new Chunk("jexp", font);
    chunk.setAction(new PdfAction("http://www.jexp.ru"));
    document.add(chunk);
    document.close();
  }
}





Get font name

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("esl_gothic_unicode.ttf", BaseFont.IDENTITY_H,
        BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, 12);
    System.err.println(bf.getClass().getName());
    document.add(new Paragraph("this is a test.", font));
    document.add(new Paragraph("\ue70a\ue70a\ue70a\ue70a\ue70a\ue70a ", font));
    document.close();
  }
}





Japanese font

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args)throws Exception {
    Document document = new Document();
      PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
      document.open();
      BaseFont bf;
      Font font;
      bf = BaseFont.createFont("KozMinPro-Regular", "UniJIS-UCS2-H",BaseFont.EMBEDDED);
      font = new Font(bf, 12);
      document.add(new Paragraph("asdf",font));
      document.add(new Paragraph("Font: " + bf.getPostscriptFontName(),font));
      document.add(new Paragraph("\u8ab0\u3082\u77e5\u3089\u306a\u3044",font));
    document.close();
  }
}





Korea font

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf;
    Font font;
    bf = BaseFont.createFont("HYGoThic-Medium", "UniKS-UCS2-H", BaseFont.NOT_EMBEDDED);
    font = new Font(bf, 12);
    document.add(new Paragraph("asdf", font));
    document.add(new Paragraph("Font: " + bf.getPostscriptFontName(), font));
    document.add(new Paragraph("\ube48\uc9d1", font));
    document.close();
  }
}





new Font(Font.COURIER, 10, Font.BOLD)

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Font font = new Font(Font.COURIER, 10, Font.BOLD);
    font.setColor(new Color(0xFF, 0xFF, 0xFF));
    Chunk fox = new Chunk("this is a", font);
    fox.setBackground(new Color(0xa5, 0x2a, 0x2a));
    Phrase p = new Phrase(fox);
    p.add(" test");
    Chunk dog = new Chunk(" another test", new Font(Font.TIMES_ROMAN, 14, Font.ITALIC));
    dog.setBackground(new Color(0xFF, 0x00, 0x00), 10, -30, 20, -10);
    p.add(dog);
    document.add(p);
    document.close();
  }
}





Not Embedded font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document1 = new Document();
    PdfWriter.getInstance(document1, new FileOutputStream("font_not_embedded.pdf"));
    document1.open();
    BaseFont bf_not_embedded = BaseFont.createFont("c:\\windows\\fonts\\comic.ttf",
        BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font font_not_embedded = new Font(bf_not_embedded, 12);
 
    document1.add(new Paragraph("quick brown fox jumps over the lazy dog", font_not_embedded));
    document1.close();
  }
}





.otf font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("esl_gothic_shavian.otf", "Cp1252", BaseFont.EMBEDDED);
    System.err.println(bf.getClass().getName());
    Font font = new Font(bf, 12);
    document.add(new Paragraph("abced"));
    document.add(new Paragraph("this is a test", font));
    document.close();
  }
}





Right To Left Example

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    BaseFont bf = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, true);
    Font font = new Font(bf, 14);
    MultiColumnText mct = new MultiColumnText();
    mct.addSimpleColumn(36, PageSize.A4.width() - 36);
    mct.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    mct.addElement(new Paragraph("\u05e0\u05d9\u05e0\u05d4", font));
    document.add(mct);
    document.close();
  }
}





TrueType font

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("ttc.pdf"));
    document.open();
    BaseFont bf;
    Font font;
    bf = BaseFont.createFont("c:/windows/fonts/msgothic.ttc,0", BaseFont.IDENTITY_H,
        BaseFont.EMBEDDED);
    font = new Font(bf, 12);
    System.err.println(bf.getClass().getName());
    document.add(new Paragraph("abcde", font));
    document.add(new Paragraph("\u7f85\u751f\u9580", font));
    String[] names = BaseFont.enumerateTTCNames("c:/windows/fonts/msgothic.ttc");
    for (int i = 0; i < names.length; i++) {
      document.add(new Paragraph("font " + i + ": " + names[i], font));
    }
    document.close();
  }
}





Use DefaultFontMapper

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Map;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document(new Rectangle(100, 100));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "sun_tutorial_with_text.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 100);
    DefaultFontMapper mapper = new DefaultFontMapper();
    mapper.insertDirectory("c:/windows/fonts");
    String name;
    Map map = mapper.getMapper();
    for (Iterator i = map.keySet().iterator(); i.hasNext();) {
      name = (String) i.next();
      System.out.println(name + ": "
          + ((DefaultFontMapper.BaseFontParameters) map.get(name)).fontName);
    }
    Graphics2D g2 = tp.createGraphics(100, 100, mapper);
    g2.setColor(Color.black);
    java.awt.Font thisFont = new java.awt.Font("Garamond", java.awt.Font.PLAIN, 18);
    g2.setFont(thisFont);
    String pear = "Pear";
    FontMetrics metrics = g2.getFontMetrics();
    int width = metrics.stringWidth(pear);
    g2.drawString(pear, (100 - width) / 2, 20);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
    document.close();
  }
}





Use truetype font to draw unicode string

import java.awt.Graphics2D;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    String text = "\u0936\u093e\u0902\u0924\u093f";
    BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
        BaseFont.EMBEDDED);
    document.add(new Paragraph(text, new com.lowagie.text.Font(bf, 12)));
 
 
    document.close();
  }
}