Java/PDF RTF/Chunk Rendering Mode

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

Rendering Mode: TEXT_RENDER_MODE_FILL

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RenderingTEXT_RENDER_MODE_FILL {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("RenderingTEXT_RENDER_MODE_FILL.pdf"));
      document.open();
      
      Paragraph p = new Paragraph("Text Rendering:");
      document.add(p);
      Chunk chunk = new Chunk("rendering test");
      chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL, 100f, new Color(0xFF, 0x00, 0x00));
      document.add(chunk);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Rendering Mode: TEXT_RENDER_MODE_FILL_STROKE

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RenderingTEXT_RENDER_MODE_FILL_STROKE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("RenderingTEXT_RENDER_MODE_FILL_STROKE.pdf"));
      document.open();
      
      Paragraph p = new Paragraph("Text Rendering:");
      document.add(p);
      Chunk chunk = new Chunk("rendering test");
      chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 100f, new Color(0xFF, 0x00, 0x00));
      document.add(chunk);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Rendering Mode: TEXT_RENDER_MODE_FILL_STROKE, width 0.5f

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RenderingTEXT_RENDER_MODE_FILL_STROKE5 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("RenderingTEXT_RENDER_MODE_FILL_STROKE5.pdf"));
      document.open();
      
      Paragraph p = new Paragraph("Text Rendering:");
      document.add(p);
      Chunk chunk = new Chunk("rendering test");
      chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, new Color(0x00, 0x00, 0x00));
      document.add(chunk);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Rendering Mode: TEXT_RENDER_MODE_INVISIBLE

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RenderingTEXT_RENDER_MODE_INVISIBLE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("RenderingTEXT_RENDER_MODE_INVISIBLE.pdf"));
      document.open();
      
      Paragraph p = new Paragraph("Text Rendering:");
      document.add(p);
      Chunk chunk = new Chunk("rendering test");
      chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 100f, new Color(0x00, 0xFF, 0x00));
      document.add(chunk);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Rendering Mode: TEXT_RENDER_MODE_STROKE

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RenderingTEXT_RENDER_MODE_STROKE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("RenderingTEXT_RENDER_MODE_STROKE.pdf"));
      document.open();
      
      Paragraph p = new Paragraph("Text Rendering:");
      document.add(p);
      Chunk chunk = new Chunk("rendering test");
      chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_STROKE, 0.3f, new Color(0x00, 0x00, 0xFF));
      document.add(chunk);
    } catch (Exception ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Rendering text from right to left

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class RightToLeftPDF {
  public static void main(String[] args) {
    try {
      Document document = new Document(PageSize.A4, 50, 50, 50, 50);
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RightToLeftPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      ColumnText ct = new ColumnText(cb);
      ct.setSimpleColumn(100, 100, 500, 800, 24, Element.ALIGN_LEFT);
      ct.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
      ct.setLeading(0, 1);
      ct.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
      ct.setAlignment(Element.ALIGN_CENTER);
      ct.addText(new Chunk("ABCD"));
      ct.go();
      ct.setAlignment(Element.ALIGN_JUSTIFIED);
      ct.addText(new Chunk("ABCD"));
      ct.go();
      ct.setAlignment(Element.ALIGN_CENTER);
      ct.addText(new Chunk("ABCD"));
      ct.go();
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}