Java Tutorial/PDF/JPG Image

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

Add JPG image to Pdf document

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
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();
    Image jpg = Image.getInstance("dog.jpg");
    jpg.setBorder(Image.BOX);
    jpg.setBorderColor(new Color(0xFF, 0x00, 0x00));
    jpg.setBorderWidth(5);
    document.add(jpg);
    document.close();
  }
}





Gif and jpg images

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
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"));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("3.pdf"));
    writer.setStrictImageSequence(true);
    document.open();
    Image jpg = Image.getInstance("dog1.jpg");
    Image gif = Image.getInstance("dog2.gif");
    document.add(jpg);
    document.add(gif);
    document.close();
  }
}





Load JPG image

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
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();
    document.add(new Paragraph("dog.jpg"));
    Image img1 = Image.getInstance("dog.jpg");
    System.out.println(img1.getClass().getName());
    document.add(img1);
    
    
    document.close();
  }
}