Java Tutorial/PDF/Image

Материал из Java эксперт
Версия от 05:21, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add image to an existing Pdf document

import java.io.FileOutputStream;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldRead.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStamper2.pdf"));
    Image img = Image.getInstance("watermark.jpg");
    img.setAbsolutePosition(200, 400);
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    PdfContentByte under, over;
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
      stamper.setRotateContents(false);
      under = stamper.getUnderContent(i);
      under.addImage(img);
    }
    stamper.close();
  }
}





Black and White

import java.awt.Color;
import java.awt.Toolkit;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Rectangle r = new Rectangle(PageSize.A4);
    r.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
    Document document = new Document(r);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage("dog.gif");
    com.lowagie.text.Image img1 = com.lowagie.text.Image.getInstance(awtImage, null, true);
    document.add(img1);
    
    document.close();
  }
}





Convert java.awt.Image to com.lowagie.text.Image

import java.awt.Color;
import java.awt.Toolkit;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Rectangle r = new Rectangle(PageSize.A4);
    r.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
    Document document = new Document(r);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage("dog.gif");
    com.lowagie.text.Image img1 = com.lowagie.text.Image.getInstance(awtImage, null);
    document.add(img1);
    
    document.close();
  }
}





Create chunk from Image

import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
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();
    Chunk fox = new Chunk(Image.getInstance("fox.gif"), 0, -15);
    Chunk dog = new Chunk(Image.getInstance("dog.gif"), 0, -15);
    Paragraph p = new Paragraph("Quick brown ");
    p.add(fox);
    p.add(dog);
    document.add(p);
    document.close();
  }
}





Get Image height and width

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();
    Image jpg1 = Image.getInstance("dog.jpg");
    jpg1.scalePercent(80);
    jpg1.setRotation((float) Math.PI / 6);
    document.add(new Paragraph("rotate 30 degrees"));
    document.add(jpg1);
    document.add(new Paragraph("Original width: " + jpg1.width() + "; original height: "
        + jpg1.height()));    
    document.close();
  }
}





Image Absolute Position

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    com.lowagie.text.Image img = null;
    Document document = new Document(new Rectangle(200, 280));
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    img = com.lowagie.text.Image.getInstance("dog.png");
    img.setAbsolutePosition(15, 15);
    document.add(img);
    document.close();
  }
}





Image Alignment

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
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 Phrase("foxdog.jpg"));
    Image img1 = Image.getInstance("foxdog.jpg");
    img1.setAlignment(Image.LEFT);
    document.add(img1);
    document.add(new Phrase("foxdog.gif"));
    Image img2 = Image.getInstance("foxdog.gif");
    img2.setAlignment(Image.MIDDLE);
    document.add(img2);
    document.add(new Phrase("foxdog.png"));
    Image img3 = Image.getInstance("foxdog.png");
    img3.setAlignment(Image.RIGHT);
    document.add(img3);
    document.close();
  }
}





Image Mask

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();
    byte circledata[] = { (byte) 0x3c, (byte) 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
        (byte) 0xe7, (byte) 0x7e, (byte) 0x3c };
    Image mask = Image.getInstance(8, 8, 1, 1, circledata);
    mask.makeMask();
    mask.setInvertMask(true);
    Image img = Image.getInstance("dog.jpg");
    img.setImageMask(mask);
    document.add(img);
    document.add(Image.getInstance(8, 8, 1, 1, circledata));
    document.add(mask);
    document.close();
  }
}





Image transition

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 img = Image.getInstance("dog.jpg");
    img.setAbsolutePosition(50, 600);
    document.add(img);
    img.setAbsolutePosition(50, 300);
    document.add(img);
    document.close();
  }
}





Image Wrapping

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
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();
    Phrase p = new Phrase("this is a test. ");
    Image img1 = Image.getInstance("foxdog.jpg");
    img1.setAlignment(Image.RIGHT | Image.TEXTWRAP);
    document.add(img1);
    for (int i = 0; i < 20; i++)
      document.add(p);
    Image img2 = Image.getInstance("foxdog.gif");
    img2.setAlignment(Image.MIDDLE | Image.UNDERLYING);
    document.add(img2);
    for (int i = 0; i < 30; i++)
      document.add(p);
    document.close();
  }
}





Pattern Matrix for an image

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.pdf.PatternColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPatternPainter;
import com.lowagie.text.pdf.PdfShading;
import com.lowagie.text.pdf.PdfShadingPattern;
import com.lowagie.text.pdf.PdfSpotColor;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.ShadingColor;
import com.lowagie.text.pdf.SpotColor;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfSpotColor psc_cmyk = new PdfSpotColor("iTextSpotColorCMYK", 0.25f, new CMYKColor(0.3f, .9f,
        .3f, .1f));
    SpotColor sc_cmyk = new SpotColor(psc_cmyk);
    Image img = Image.getInstance("dog.jpg");
    PdfPatternPainter img_pattern = cb.createPattern(img.scaledWidth(), img.scaledHeight(), img
        .scaledWidth(), img.scaledHeight());
    img_pattern.addImage(img, img.scaledWidth(), 0f, 0f, img.scaledHeight(), 0f, 0f);
    img_pattern.setPatternMatrix(1f, 0f, 0f, 1f, 60f, 60f);
    PatternColor img_color = new PatternColor(img_pattern);
    document.add(new Paragraph("This is a paragraph painted using an image pattern", new Font(
       Font.HELVETICA, 24, Font.BOLD, img_color)));
    document.close();
  }
}





Red and Yellow

import java.awt.Color;
import java.awt.Toolkit;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Rectangle r = new Rectangle(PageSize.A4);
    r.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
    Document document = new Document(r);
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage("dog.gif");
    com.lowagie.text.Image img1 = com.lowagie.text.Image.getInstance(awtImage, new Color(0xFF,
        0xFF, 0x00));
    document.add(img1);
    
    document.close();
  }
}





Rotate 30 degrees

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();
    Image jpg1 = Image.getInstance("dog.jpg");
    jpg1.scalePercent(80);
    jpg1.setRotation((float) Math.PI / 6);
    document.add(new Paragraph("rotate 30 degrees"));
    document.add(jpg1);
    document.close();
  }
}





Rotate 45 degrees

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();
    Image jpg1 = Image.getInstance("dog.jpg");
    jpg1.scalePercent(80);
    jpg1.setRotationDegrees(45);
    document.add(jpg1);
   
    document.close();
  }
}





Scale Absolute

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();
    Image jpg = Image.getInstance("dog.jpg");
    jpg.scaleAbsolute(154, 94);
    document.add(new Paragraph("scaleAbsolute(154, 94)"));
    document.add(jpg);
    document.close();
  }
}





Scale Percent

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();
    Image jpg = Image.getInstance("dog.jpg");
    jpg.scalePercent(50);
    document.add(new Paragraph("scalePercent(50)"));
    document.add(jpg);
    document.close();
  }
}





Scale Percent:/td>

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();
    Image jpg = Image.getInstance("dog.jpg");
    jpg.scalePercent(100, 50);
    document.add(new Paragraph("scalePercent(100, 50)"));
    document.add(jpg);
    document.close();
  }
}