Java Tutorial/PDF/Line
Содержание
Add shape to an existing Pdf document
import java.io.FileOutputStream;
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"));
int total = reader.getNumberOfPages() + 1;
for (int i = 1; i < total; i++) {
stamper.setRotateContents(false);
PdfContentByte over = stamper.getOverContent(i);
over.setRGBColorStroke(0xFF, 0x00, 0x00);
over.setLineWidth(5f);
over.ellipse(250, 450, 350, 550);
over.stroke();
}
stamper.close();
}
}
Line Dash
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.setLineWidth(3);
cb.moveTo(40, 480);
cb.lineTo(320, 480);
cb.stroke();
cb.setLineDash(6, 0);
cb.moveTo(40, 450);
cb.lineTo(320, 450);
cb.stroke();
float[] dash1 = { 10, 5, 5, 5, 20 };
cb.setLineDash(dash1, 5);
cb.moveTo(40, 440);
cb.lineTo(320, 440);
cb.stroke();
float[] dash2 = { 9, 6, 0, 6 };
cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
cb.setLineDash(dash2, 0);
cb.moveTo(40, 430);
cb.lineTo(320, 430);
cb.stroke();
cb.restoreState();
document.close();
}
}
Miter Limit
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.setLineWidth(8);
cb.setLineJoin(PdfContentByte.LINE_JOIN_MITER);
cb.setMiterLimit(2);
cb.moveTo(75, 560);
cb.lineTo(95, 590);
cb.lineTo(115, 560);
cb.stroke();
cb.restoreState();
cb.saveState();
cb.setLineWidth(8);
cb.setLineJoin(PdfContentByte.LINE_JOIN_MITER);
cb.setMiterLimit(2.1f);
cb.moveTo(280, 500);
cb.lineTo(295, 530);
cb.lineTo(310, 500);
cb.stroke();
cb.restoreState();
document.close();
}
}
Paint line
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4);
PdfReader reader = new PdfReader("1.pdf");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("my.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
cb.addTemplate(page, -0.5f, 0f, 0f, -0.5f, PageSize.A4.width() / 2, PageSize.A4.height());
cb.setLineDash(20, 10, 10);
cb.moveTo(0, PageSize.A4.height() / 2f);
cb.lineTo(PageSize.A4.width(), PageSize.A4.height() / 2f);
cb.stroke();
cb.moveTo(PageSize.A4.width() / 2f, 0);
cb.lineTo(PageSize.A4.width() / 2f, PageSize.A4.height());
cb.stroke();
document.close();
}
}
PdfContentByte.LINE_CAP_BUTT
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.setLineWidth(8);
cb.setLineCap(PdfContentByte.LINE_CAP_BUTT);
cb.moveTo(72, 640);
cb.lineTo(144, 640);
cb.stroke();
cb.restoreState();
document.close();
}
}
PdfContentByte.LINE_CAP_PROJECTING_SQUARE
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.setLineWidth(8);
cb.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
cb.moveTo(72, 640);
cb.lineTo(144, 640);
cb.stroke();
cb.restoreState();
document.close();
}
}
PdfContentByte.LINE_CAP_ROUND
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.setLineWidth(8);
cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
cb.moveTo(72, 640);
cb.lineTo(144, 640);
cb.stroke();
cb.restoreState();
document.close();
}
}
PdfContentByte.LINE_JOIN_MITER
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.setLineWidth(8);
cb.setLineCap(PdfContentByte.LINE_JOIN_MITER);
cb.moveTo(72, 640);
cb.lineTo(144, 640);
cb.stroke();
cb.restoreState();
document.close();
}
}
set LineCap
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
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();
PdfTemplate template = cb.createTemplate(150, 150);
template.setLineWidth(12f);
template.arc(40f, 10f, 20f, -50f, 90f, 45f);
template.stroke();
template.setLineCap(PdfContentByte.LINE_JOIN_ROUND);
template.arc(80f, 30f, 160f, 10f, 90f, 180f);
template.arc(115f, 65f, 25f, 5f, 0f, 360f);
template.stroke();
cb.addTemplate(template, 1f, 0f, 0f, -1f, 0f, PageSize.A4.height());
document.close();
}
}
Set 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 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();
}
document.close();
}
}