搜索

怎么用java代码生成pdf文档

发布网友 发布时间:2022-04-23 22:46

我来回答

8个回答

热心网友 时间:2022-04-21 20:41

import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
 
import com.itextpdf.text.*;  
import com.itextpdf.text.pdf.PdfWriter;  
 
public class PdfTest  
{  
    public static void main(String[] args) throws Exception  
    {  
        Document pdfDoc = new Document();  
        // 将要生成的 pdf 文件的路径输出流  
        FileOutputStream pdfFile =   
            new FileOutputStream(new File("F:/study/test/firstPdf.pdf"));  
 
        // pdf 文件中的一个文字段落  
        Paragraph paragraph = new Paragraph("My first PDF file with an image ...");  
        Image image = Image.getInstance("F:/study/test/洛克 李.jpg");  
          
        // 用 Document 对象、File 对象获得 PdfWriter 输出流对象  
        PdfWriter.getInstance(pdfDoc, pdfFile);  
        pdfDoc.open();  // 打开 Document 文档  
          
        // 添加一个文字段落、一张图片  
        pdfDoc.add(paragraph);  
        pdfDoc.add(image);  
      
        pdfDoc.close();  
    }  
}

热心网友 时间:2022-04-21 21:59

用java代码生成pdf文档
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
// 创建一个Document对象
Document document = new Document();

try
{
// 生成名为 HelloWorld.pdf 的文档
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
// 添加PDF文档的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText");
// 打开文档,将要写入内容
document.open();
// 插入一个段落
document.add(new Paragraph("Hello World!"));

}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
// 关闭打开的文档
document.close();
}
}
编译运行以后,我们可以在运行的目录发现生成的HelloWorld.pdf,打开可以看到我们写的文字:

热心网友 时间:2022-04-21 23:33

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;

/**
* 描述:TODO 【JAVA生成PDF】
* <p>
*
* @title GeneratePDF
* @author SYJ
* @email songyanjun_stars@126.com
* @date 2013-4-6
* @version V1.0
*/
public class GeneratePDF {

public static void main(String[] args) {

//调用第一个方法,向C盘生成一个名字为ITextTest.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();
}

}

热心网友 时间:2022-04-22 01:25

直接生成或者间接生成。
1 使用 诸如 iText,Apache PDFBox 之类的PDF库从头生成PDF文档。
这个在iText,PDFBox的网站有文档可以参考。

2使用Flying Saucer xhtmlrenderer,CSS2XSLFO,JODConverter,Apache FOP等从其他格式生成PDF文档。

几个工具的站点

https://github.com/flyingsaucerproject/flyingsaucer
http://sourceforge.net/projects/css2xslfo/

http://www.artofsolving.com/opensource/jodconverter.html

热心网友 时间:2022-04-22 03:33

java操作pdf一般都用第三方工具了,你试试itext

热心网友 时间:2022-04-22 05:57

public void CreatePdf() {
Rectangle rectangle = new Rectangle(parameters.getPaperSize()
.getHeight(), parameters.getPaperSize().getWidth());
Document document = new Document(rectangle, 0, 0, 0, 0);
try {
PdfWriter.getInstance(document, new FileOutputStream(new File(
destFileName)));
document.open();
try {
Image image = Image.getInstance(destFileName + ".jpg", true);
image.setAlignment(Image.MIDDLE);
document.add(image);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}

我项目中用到的代码。仅作为参考。

热心网友 时间:2022-04-22 08:39

推荐使用类库iText

iText简介:iText是一个非常著名的能够快速产生PDF文件的Java类库。支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。

热心网友 时间:2022-04-22 11:37

http://www.jb51.net/article/36626.htm
希望对你有帮助
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
Top