Java Tutorial/PDF/Text

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

Add text to an existing Pdf document

import java.io.FileOutputStream;
import com.lowagie.text.Image;
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("HelloWorldStamper2.pdf"));
    Image img = Image.getInstance("watermark.jpg");
    img.setAbsolutePosition(200, 400);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte under, over;
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
      stamper.setRotateContents(false);
      under = stamper.getUnderContent(i);
      under.addImage(img);
      over = stamper.getOverContent(i);
      over.beginText();
      over.setFontAndSize(bf, 18);
      over.setTextMatrix(30, 30);
      over.showText("page " + i);
      over.endText();
    }
    stamper.close();
  }
}





Begin text and end text

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
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();
    PdfContentByte cb = writer.getDirectContent();
    String text = "this is a test";
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    cb.setFontAndSize(bf, 12);
    cb.beginText();
    

    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, 650, 0);
    cb.endText();
    document.close();
  }
}





Chunk text with underline

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.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
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();
    Chunk foxLineUnder = new Chunk("this is a test");
    foxLineUnder.setUnderline(0.2f, -2f);
    Paragraph p = new Paragraph ();
    p.add(foxLineUnder);
    document.add(p);
    document.add(Chunk.NEWLINE);
    document.close();
  }
}





Create and use PdfTextArray

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.newlineText();
      
      PdfTextArray array = new PdfTextArray("J");
      array.add(120);
      array.add("a");
      array.add(120);
      array.add("v");
      array.add(95);
      array.add("a2s");
      cb.showText(array);
      
      cb.endText();
    document.close();
  }
}





Draw Unicode string

import java.awt.Graphics2D;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
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 = "\u5e73\u548C";
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(100, 50);
    Graphics2D g2 = tp.createGraphicsShapes(100, 50);
    java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 12);
    g2.setFont(font);
    g2.drawString(text, 0, 40);
    g2.dispose();
    cb.addTemplate(tp, 36, 780);
    document.close();
  }
}





PdfContentByte.TEXT_RENDER_MODE_STROKE

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();
      PdfTemplate tp2 = cb.createTemplate(200, 36);
      tp2.beginText();
      tp2.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
      tp2.setFontAndSize(bf, 24);
      tp2.moveText(1, -1);
      tp2.showText(text);
      tp2.endText();
      cb.addTemplate(tp2, 36, 10);
      
      cb.endText();
    document.close();
  }
}





Set Text Rendering Mode

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();
      PdfTemplate tp1 = cb.createTemplate(160, 36);
      tp1.beginText();
      tp1.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
      tp1.setFontAndSize(bf, 24);
      tp1.moveText(6, -6);
      tp1.showText(text);
      tp1.endText();
      cb.addTemplate(tp1, 36, 240);
      
      cb.endText();
    document.close();
  }
}





Set Word Spacing

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.newlineText();
      
      PdfTextArray array = new PdfTextArray("J");
      array.add(120);
      array.add("a");
      array.add(120);
      array.add("v");
      array.add(95);
      array.add("a2s");
      cb.showText(array);
      cb.setWordSpacing(50);
      cb.endText();
    document.close();
  }
}





Text alignment

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
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();
    PdfContentByte cb = writer.getDirectContent();
    String text = "this is a test";
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    cb.setFontAndSize(bf, 12);
    cb.beginText();

    for (int i = 0; i < 360; i += 30) {
      cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text, 200, 300, i);
    }
    cb.endText();
    document.close();
  }
}





Text is added at an absolute position and the stream with the content of a page is not compressed

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorldAbsolute {
  public static void main(String[] args) throws Exception {
    Document.rupress = false;
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "HelloWorldAbsolute.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
        BaseFont.NOT_EMBEDDED);
    cb.saveState(); 
    cb.beginText();
    cb.moveText(36, 806); 
    cb.moveText(0, -18); 
    cb.setFontAndSize(bf, 12); 
    cb.showText("Hello World"); 
    cb.endText(); 
    cb.restoreState(); 
    document.close();
  }
}





Text Matrix

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
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();
    PdfContentByte cb = writer.getDirectContent();
    String text = "this is a test";
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    cb.setFontAndSize(bf, 12);
    cb.beginText();
    
    cb.setTextMatrix(50, 700);
    cb.showText(text);
    cb.endText();
    document.close();
  }
}





Text with line above

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.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
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();
    Chunk line = new Chunk("this is a test");
    line.setUnderline(0.2f, 14f);
    Paragraph p = new Paragraph ();
    p.add(line);
    document.add(p);
    document.add(Chunk.NEWLINE);
    document.close();
  }
}