import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class PDFGenerator {
public static void main(String[] args) {
String filePath = "path/to/output.pdf"; // PDF文件保存路径
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(filePath));
document.open();
String text = "This is some sample text."; // 要写入PDF的文本内容
Paragraph paragraph = new Paragraph(text, new Font(Font.FontFamily.TIMES_ROMAN, 12));
document.add(paragraph);
document.close();
System.out.println("PDF generated successfully.");
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
}
}
}
JAVA 生成PDF文件
最新推荐文章于 2024-07-18 14:24:46 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)