Java/PDF RTF/Template

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

Creating Image from template

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class TemplateImagesPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TemplateImagesPDF.pdf"));
      document.open();
      PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
      String text = "Vertical";
      float size = 16;
      float width = bf.getWidthPoint(text, size);
      template.beginText();
      template.setFontAndSize(bf, size);
      template.setTextMatrix(0, 2);
      template.showText(text);
      template.endText();
      template.setWidth(width);
      template.setHeight(size + 2);
      
      Image img = Image.getInstance(template);
      img.setRotationDegrees(90);
      document.add(new Phrase(new Chunk(img, 0, 0)));
      document.close();
    } catch (Exception de) {
      System.err.println(de.getMessage());
    }
  }
}





Template Images Scale Height

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class TemplateImagesScaleHeightPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TemplateImagesScaleHeightPDF.pdf"));
      document.open();
      PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
      String text = "Vertical";
      float size = 16;
      float width = bf.getWidthPoint(text, size);
      template.beginText();
      template.setFontAndSize(bf, size);
      template.setTextMatrix(0, 2);
      template.showText(text);
      template.endText();
      template.setWidth(width);
      template.setHeight(size + 2);
      
      Image img = Image.getInstance(template);
      img.setRotationDegrees(90);
      Paragraph p1 = new Paragraph("This is a template ");
      p1.add(new Phrase(new Chunk(img, 0, 0)));
      p1.setLeading(img.scaledHeight() * 1.1f);
      document.add(p1);
      
      document.close();
    } catch (Exception de) {
      System.err.println(de.getMessage());
    }
  }
}





Templates Draw

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 TemplatesDrawPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TemplatesDrawPDF.pdf"));
      document.open();
      
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate template = cb.createTemplate(500, 200);
      template.moveTo(0, 200);
      template.lineTo(500, 0);
      template.stroke();
      template.circle(250f, 100f, 80f);
      template.stroke();
      cb.addTemplate(template, 0, 0);
      document.newPage();
      cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Templates: 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.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class TemplatesTextPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TemplatesTextPDF.pdf"));
      document.open();
      
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate template = cb.createTemplate(500, 200);
      template.beginText();
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
      template.setFontAndSize(bf, 12);
      template.setTextMatrix(10, 10);
      template.showText("10,10 (relative to the template!)");
      template.endText();
      cb.addTemplate(template, 0, 0);
      document.newPage();
      cb.addTemplate(template, 0, 0);
      
      cb.addTemplate(template, 2, 0, 0, 2, -200, 400);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}