Java/PDF RTF/Image Scale

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

Image: Scale Absolute

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.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class ImageScalingAbsolutePDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ImageScalingAbsolutePDF.pdf"));
      document.open();
      Image png = Image.getInstance("logo.png");
      png.scaleAbsolute(160, 120);
      
      document.add(png);
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Image: Scale by Percent

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class ImageScalingByPercentPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ImageScalingByPercentPDF.pdf"));
      document.open();
      Image png = Image.getInstance("logo.png");
      png.scalePercent(50);
      
      document.add(png);
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Image: Scale Percent Two Axis

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class ImageScalingPercentTwoAxisPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ImageScalingPercentTwoAxisPDF.pdf"));
      document.open();
      Image png = Image.getInstance("logo.png");
      png.scalePercent(100, 50);
      
      document.add(png);
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}





Image: Scale to Fit

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class ImageScaleToFitPDF {
  public static void main(String[] args) throws Exception{
      Image front = Image.getInstance("logo.png");
      Rectangle pageSize = new Rectangle(780, 525);
      Document document = new Document(pageSize);
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ImageScaleToFitPDF.pdf"));
      document.open();
      front.scaleToFit(200, 300);
      front.setAbsolutePosition(500, 100);
      document.add(front);
      document.close();
  }
}