Java Tutorial/PDF/PdfContentByte — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:21, 1 июня 2010
Содержание
Clip a region
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("clipping_path.pdf"));
document.open();
Image img = Image.getInstance("dog.jpg");
float w = img.scaledWidth();
float h = img.scaledHeight();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.circle(260, 700, 70);
cb.clip();
cb.newPath();
cb.addImage(img, w, 0, 0, h, 36, 620);
cb.restoreState();
document.close();
}
}
Draw shape with PdfContentByte
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
document.add(new Paragraph("this is a test."));
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(30, 70);
cb.lineTo(49, 70);
cb.lineTo(49, 80);
cb.lineTo(30, 80);
cb.closePath();
cb.rectangle(30, 700, 460, 100);
document.close();
}
}
Literal PdfContentByte
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("logo.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
String eye = "this is a test";
cb.setLiteral(eye);
document.close();
}
}
Position and resize an image
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
Image eye = Image.getInstance("iTextLogo.gif");
eye.setAbsolutePosition(36, 780);
cb.addImage(eye, true);
cb.addImage(eye, 271, -50, -30, 550, 100, 100, true);
document.close();
}
}
restore State
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.moveTo(72, 650);
cb.lineTo(72, 600);
cb.moveTo(144, 650);
cb.lineTo(144, 600);
cb.stroke();
cb.restoreState();
document.close();
}
}
Save paint state
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document.rupress = false;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
for (int i = 25; i > 0; i--) {
cb.setLineWidth((float) i / 10);
cb.moveTo(40, 806 - (5 * i));
cb.lineTo(320, 806 - (5 * i));
cb.stroke();
}
cb.restoreState();
document.close();
}
}