中易网

怎么用java代码生成pdf文档

答案:7  悬赏:0  
解决时间 2021-01-11 21:37
  • 提问者网友:蓝琪梦莎
  • 2021-01-11 04:31
怎么用java代码生成pdf文档
最佳答案
  • 二级知识专家网友:duile
  • 2021-01-11 05:10
用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,打开可以看到我们写的文字:
全部回答
  • 1楼网友:爱难随人意
  • 2021-01-11 11:33
java操作pdf一般都用第三方工具了,你试试itext
  • 2楼网友:逃夭
  • 2021-01-11 10:31
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();
}
}

我项目中用到的代码。仅作为参考。
  • 3楼网友:神鬼未生
  • 2021-01-11 09:13
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;

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();
}

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();
}

}
  • 4楼网友:想偏头吻你
  • 2021-01-11 08:46

推荐使用类库iText
iText简介:iText是一个非常著名的能够快速产生PDF文件的Java类库。支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。
  • 5楼网友:污到你湿
  • 2021-01-11 07:58
我暂时保留我的看法!
  • 6楼网友:独钓一江月
  • 2021-01-11 06: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();  
    }  
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息