如何使用画布在PDF中绘制对象。(How to draw an object in a PDF using canvas.)
优质
小牛编辑
129浏览
2023-12-01
问题描述 (Problem Description)
如何使用画布在PDF中绘制对象。
解决方案 (Solution)
以下是使用Canvas在PDF中绘制对象的程序。
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
public class DrawingCircle {
public static void main(String args[]) throws Exception {
//Creating a PdfWriter
String dest = "C:/itextExamples/drawingCircle.pdf";
PdfWriter writer = new PdfWriter(dest);
//Creating a PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
//Creating a Document object
Document doc = new Document(pdfDoc);
//Creating a new page
PdfPage pdfPage = pdfDoc.addNewPage();
//Creating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
//Setting color to the circle
Color color = Color.GREEN;
canvas.setColor(color, true);
//creating a circle
canvas.circle(300, 400, 200);
//Filling the circle
canvas.fill();
//Closing the document
doc.close();
System.out.println("Object drawn on pdf successfully");
}
}