Java/PDF RTF/Transformation

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

AffineTransformation in PDF

import java.awt.geom.AffineTransform;
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 AffineTransformationPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AffineTransformationPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      cb.transform(AffineTransform.getScaleInstance(1.2, 0.75));
      cb.moveTo(216, 720);
      cb.lineTo(360, 360);
      cb.lineTo(360, 504);
      cb.lineTo(72, 144);
      cb.lineTo(144, 288);
      cb.stroke();
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Transformations Demo

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class TransformationsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransformationsPDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      PdfTemplate template = cb.createTemplate(120, 120);
      template.moveTo(30, 10);
      template.lineTo(90, 10);
      template.lineTo(10, 80);
      template.lineTo(30, 80);
      template.closePath();
      template.stroke();
      cb.addTemplate(template, 0, 0);
      cb.addTemplate(template, 0, 1, -1, 0, 200, 600);
      cb.addTemplate(template, .5f, 0, 0, .5f, 100, 400);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Transforming Image

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class TransformImagePDF {
  public static void main(String[] args) {
    Document.rupress = false;
    Document document = new Document(PageSize.A4);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransformImagePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      Image img = Image.getInstance("logo.png");
      cb.addImage(img, 250, -50, -30, 550, 100, 100);
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}