Java itext字句

Itext是一個Java庫,用於創建和操作PDF檔案。以下是使用iText庫在Java中創建PDF的一些常見用法:

1. 創建PDF檔案

```java

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

public class CreatePDF {

public static void main(String[] args) {

try {

PdfDocument pdf = new PdfDocument(new PdfWriter("output.pdf"));

System.out.println("PDF Created!");

} catch (Exception e) {

e.printStackTrace();

}

}

}

```

2. 添加文本到PDF

```java

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.text.Document;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.*;

public class AddTextToPDF {

public static void main(String[] args) {

try {

PdfDocument pdf = new PdfDocument(new PdfWriter("output.pdf"));

Document document = new Document(pdf);

document.add(new Paragraph("Hello, World!"));

document.close();

System.out.println("Text Added!");

} catch (Exception e) {

e.printStackTrace();

}

}

}

```

3. 添加圖像到PDF

```java

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.*;

import com.itextpdf.styledxmlparser.css.util.CssUtils;

import javafx.embed.swingfxloader;

import javafxfxloader.*;

import javafxloader.fxloader;

import javafxutils.*;

import javafxutilsfx.*;

import javafx.*;

import javafxmedia.*;

import javafxmediafx.*;

import javafxgraphics.*;

import javafxgraphicsfx.*;

import javafxxml.*;

import javafxxmlgraphics.*;

import javafxxmlgraphicsfx.*;

import javafxnet.*;

import javafxnetfx.*;

import javafxxmlgraphicsfx.*;

import javafxmath.*;

import javafxmathfx.*;

import javafxutil.*;

import javafxutilfx.*;

import java.*; // import all needed classes for image loading and display here! (for example, for javax Image, BufferedImage, and Toolkit)

import javax servlet.* // if necessary for loading images via a URL from a webserver, use this class (from javax servlet package) for ImageLoaderServlet instead of JavaFX methods for displaying images in PDFs! // !!! Don't forget to call fxloader stop() method after use! // !!! Use only one of the methods for displaying images in PDFs! // !!! Don't use both methods at the same time! // !!! Use only one of the methods for loading images from URLs! // !!! Use only one of the methods for displaying images in PDFs! // !!! Don't use deprecated methods! // !!! Avoid using final statements inside methods that call each other, because this may lead to circular dependencies! // !!! Use Image and ImageUtil from iText to display images in PDFs! // !!!! Import only the necessary classes! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!! !!!!!!!!!!!!! NOTE: In some cases, JavaFX uses certain packages internally for some of its classes and functions that do not need to be used for basic PDF generation with iText libraries (like above methods), and which might be a cause of unnecessary load/compile times! Also note that there might be other library or Java specific libraries or code involved here (iText libraries do not always have direct support for displaying images in PDFs), so please check your specific situation before using any of these methods! Also note that iText libraries are not always the best choice for generating PDFs, especially if you need to generate complex PDFs with multiple pages and/or complex layouts, and/or if you need to generate PDFs with images, text, tables, and/or charts, and/or if you need to generate PDFs with different fonts and/or colors and/or backgrounds and/or borders and/or watermarks and/or signatures and/or other advanced features! In such cases, it might be better to use a dedicated PDF library like Apache PDFBox or iText Advanced for Java, which might have better support for generating

以上就是【Java itext字句】的相關內容,敬請閱讀。