
正文
java创建pdf代码 java 生成pdf文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 生成PDF表格
实现代码如下java创建pdf代码:
package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* 描述java创建pdf代码:TODO 【JAVA生成PDF】
*
*
* @title GeneratePDF
* @version V1.0
*/
public class GeneratePDF {
public static void main(String[] args) {
//调用第一个方法,向C盘生成一个名字为ITextTest.pdf java创建pdf代码的文件
try {
writeSimplePdf();
}
catch (Exception e) { e.printStackTrace(); }
//调用第二个方法,向C盘名字为ITextTest.pdf的文件,添加章节。
try {
writeCharpter();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void writeSimplePdf() throws Exception {
// 1.新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
// 3.打开文档
document.open();
// 4.向文档中添加内容
// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
// 5.关闭文档
document.close();
}
/**
* 添加含有章节的pdf文件
*
* @throws Exception
*/
public static void writeCharpter() throws Exception {
// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
// 打开文件
document.open();
// 标题
document.addTitle("Hello mingri example");
// 作者
document.addAuthor("wolf");
// 主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
// 向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
// 关闭文档
document.close();
}
}
相关问答
Q1: java生成pdf时如何判断最后一页能否把剩余的内容全部放下
1、首先打开java的运行器,需要使用Java代码创建一个PDF文档对象。
2、其次把要输入的内容全部输入到要生成的pdf中。
3、最后使用前端抓取打印,即可查看。
Q2: Java如何使用Java创建一个空的PDF文档
package com.yiibai;import java.io.IOException;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;// 需要下 apache pdfbox包和apache.commons.loggin乌,下载地址: 和 在本示例中下载使用的是:pdfbox-2.0.7.jar // 将下载的pdfbox-2.0.7.jar添加到Eclipse项目依懒库中。// 右键点击:"java_apache_pdf_box"-"Bulid Path"-"Add External Artchives...",然后选笃下载的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件 public class CreatingEmptyPdf {
public static void main(String args[]) throws IOException {
// Creating PDF document object
PDDocument document = new PDDocument();
// Add an empty page to it
document.addPage(new PDPage());
// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/BlankPdf.pdf");
System.out.println("PDF created");
// Closing the document
document.close();
}}
Q3: java创建pdf文件写入不进去
通常需要用到用于读、写、编辑PDF文件的库java创建pdf代码,java创建pdf代码你可以参考下面采用spire.pdf.jar来创建PDF的步骤及方法:
首先需要引入jar包。具体的引入方法可以自行百度搜索。
创建PdfDocument类的对象java创建pdf代码,并通过PdfDocument.getPages().add()方法添加页码。
定义标题文字。
创建PdfSolidBrush 画刷、PdfTrueTypeFont 字体、PdfStringFormat字符串、Rectangle2D等对象java创建pdf代码,用于指定字符串绘制效果、字体、格式、绘制区域等。
通过PdfPageBase.getCanvas().drawString(body, font2, brush2, rect, format2)方法将内容绘制到PDF页面。
下面附上详细的代码demo示例:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
public class CreatePdfDocumentInJava {
public static void main(String[] args) throws FileNotFoundException, IOException {
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//添加一页
PdfPageBase page = doc.getPages().add();
//标题文字
String title = "Java基础语法";
//创建单色画刷对象
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//创建TrueType字体对象
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 14), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", Font.PLAIN, 10), true);
//创建PdfStringFormat对象
PdfStringFormat format1 = new PdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//设置文字居中
//使用drawString方法绘制标题文字
page.getCanvas().drawString(title, font1, brush1, new Point2D.Float((float) page.getActualBounds(true).getWidth() / 2, 0), format1);
//从txt文件读取内容到字符串
String body = readFileToString("C:\\Users\\Administrator\\Desktop\\bodyText.txt");
//创建PdfStringFormat对象
PdfStringFormat format2 = new PdfStringFormat();
format2.setParagraphIndent(20);//设置段首缩进
//创建Rectangle2D对象
Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, (float) page.getActualBounds(true).getWidth(), (float) page.getActualBounds(true).getHeight());
//使用drawString方法在矩形区域绘制主体文字
page.getCanvas().drawString(body, font2, brush2, rect, format2);
//保存到PDF文档
doc.saveToFile("ouput.pdf");
}
//自定义方法读取txt文件内容到字符串
private static String readFileToString(String filepath) throws FileNotFoundException, IOException {
StringBuilder sb = new StringBuilder();
String s = "";
BufferedReader br = new BufferedReader(new FileReader(filepath));
while ((s = br.readLine()) != null) {
sb.append(s + "\n");
}
br.close();
String str = sb.toString();
return str;
}
}
关于java创建pdf代码和java 生成pdf文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





