Java/PDF RTF/Unicode

Материал из Java эксперт
Перейти к: навигация, поиск

Adding Special Symbol to Pdf document

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;
public class SpecialSymbolPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SpecialSymbolPDF.pdf"));
      document.open();
      for (int i = 913; i < 970; i++) {
        document.add(Phrase.getInstance(" " + i + ": " + (char) i));
      }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Arabic Text in PDF

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class ArabicTextPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ArabicTextPDF.pdf"));
      document.open();
      java.awt.Font font = new java.awt.Font("arial", 0, 18);
      PdfContentByte cb = writer.getDirectContent();
      java.awt.Graphics2D g2 = cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
      g2.setFont(font);
      g2.drawString("\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0645\u0646", 100, 100);
      g2.dispose();
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Unicode Example

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 UnicodeExamplePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("UnicodeExamplePDF.pdf"));
      document.open();
      BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      document.add(new Paragraph("Unicode: \u0418", new Font(bfComic, 12)));
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Using Unicode

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class UsingUnicodePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("UsingUnicodePDF.pdf"));
      document.open();
      document.add(new Phrase("\u0161"));
      document.add(new Phrase("\u0178"));
      document.add(new Phrase("\u017D"));
      document.add(new Phrase("\u017E"));
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}