Java/PDF RTF/PdfContentByte
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Closing Stroke
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class StrokeClosePDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("StrokeClosePDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(10f);
cb.setRGBColorStrokeF(0f, 255f, 0f);
cb.moveTo(100, 700);
cb.lineTo(200, 800);
cb.lineTo(200f, 250f);
cb.lineTo(400f, 150f);
cb.closePathFillStroke();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Drawing text using PdfContentByte
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class TextPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " Center", 250, 700, 0);
cb.endText();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
PdfContentByte: RGBColorStrokeF
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class StrokeColorPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("StrokeColorPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(10f);
cb.setRGBColorStrokeF(0f, 255f, 0f);
cb.moveTo(100, 700);
cb.lineTo(200, 800);
cb.stroke();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Setting Color for PdfContentByte
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class PdfContentByteColor {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PdfContentByteColor.pdf"));
document.open();
BaseFont bf = FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont(false);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setColorFill(new Color(0x00, 0xFF, 0x00));
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "Grass is green", 50, 70, 0);
cb.endText();
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
Stroke Line Width
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class StrokeLineWidthPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("StrokeLineWidthPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(10f);
cb.rectangle(100, 700, 100, 100);
cb.stroke();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}
Text alignment: Right Left
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class TextRightLeftPDF {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextRightLeftPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Right", 250, 350, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, " Left", 250, 300, 0);
cb.endText();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
}