「java生成pdf表格」java pdf表格

博主:adminadmin 2022-11-22 05:50:13 48

今天给各位分享java生成pdf表格的知识,其中也会对java pdf表格进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么输出pdf格式的文件

java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf

的文档,而且可以将XML、Html文件转化为PDF文件。

iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用

iText类库了。

代码如下:

public class createPdf {

//自己做的一个简单例子,中间有图片之类的

//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document

Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);

public void getPDFdemo() throws DocumentException, IOException{

//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:

//首先

//字体的定义:这里用的是自带的jar里面的字体

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);

// 当然你也可以用你电脑里面带的字体库

//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

//定义字体 注意在最新的包里面 颜色是封装的

Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));

//生成pdf的第一个步骤:

//保存本地指定路径

saveLocal();

document.open();

ByteArrayOutputStream ba = new ByteArrayOutputStream();

// PdfWriter writer = PdfWriter.getInstance(document, ba);

document.open();

//获取此编译的文件路径

String path = this.getClass().getClassLoader().getResource("").getPath();

//获取根路径

String filePath = path.substring(1, path.length()-15);

//获取图片路径 找到你需要往pdf上生成的图片

//这里根据自己的获取的路径写 只要找到图片位置就可以

String picPath = filePath +"\\WebContent" +"\\images\\";

//往PDF中添加段落

Paragraph pHeader = new Paragraph();

pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));

//pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );

document.add(pHeader);//在文档中加入你写的内容

//获取图片

Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");

//定义图片在文档中显示的绝对位置

img2.scaleAbsolute(137.0F, 140.0F);

img2.setAbsolutePosition(330.0F, 37.0F);

//将图片添加到文档中

document.add(img2);

//关闭文档

document.close();

/*//设置文档保存的文件名

response.setHeader("Content-

disposition", "attachment;filename=\""+ new String(("CCF会员资格确认

函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");

//设置类型

response.setContentType("application/pdf");

response.setContentLength(ba.size());

ServletOutputStream out = response.getOutputStream();

ba.writeTo(out);

out.flush();*/

}

public static void main(String[]args) throws DocumentException, IOException{

createPdf pdf= new createPdf();

pdf.getPDFdemo();

}

//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf

public void saveLocal() throws IOException, DocumentException{

//直接生成PDF 制定生成到D盘test.pdf

File file = new File("D:\\text2.pdf");

file.createNewFile();

PdfWriter.getInstance(document, new FileOutputStream(file));

}

}

怎么用java动态生成pdf文档

Flying-Saucer + iText + Velocity

1. 第一步

将jar包放到你的工程里,需要的jar如下:

bcprov-jdk15-140.jar

core-renderer.jar

iText-2.0.8.jar

iTextAsian.jar

velocity-1.4.jar

Jar包下载地址:

2. 第二步

设计模版,进行排版调整样式,css样式也可以导入@import 等,通过Velocity模版引擎动态替换 页面内容,以下是模版内容:

?xml version="1.0" encoding="UTF-8" ?

html

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /

titlePDF模版/title

style type="text/css"

!--

body {

font: 100% Verdana, Arial, Helvetica, sans-serif;

margin: 0;

padding: 0;

text-align: center;

color: #000000;

}

.oneColLiqCtrHdr #container {

width: 100%;

margin: 0 auto;

text-align: left;

}

div.header-left {display: none}

div.header-right {display: none}

div.footer-left {display: none}

div.footer-right {display: none}

如何运用Java组件itext生成pdf

第一步:下载  iText5.5.6的压缩文件,解压得到核心jar包itextpdf-5.5.6.jar

下载  extrajars-2.3.zip ,解压后,得到支持中文的itext.asian.jar

第二步:项目Build Path添加刚刚的两个jar包

第三步:开始写代码

import java.io.FileOutputStream;

import com.itextpdf.text.Document;

import com.itextpdf.text.Font;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfWriter;

public class PDFDemo {

// main函数抛出异常,当然也可以try catch进行处理

public static void main(String[] args) throws Exception {

// ---------------第一阶段准备-------------------------

// 创建一个Document对象

Document document = new Document();

// 创建 PDF写入器,通过PDF写入器将文档对象写入磁盘 (第一个参数:文档对象,第二个参数,输出流)

PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("c:\\abcd.pdf"));

// 打开Document文档

document.open();

// 向Document文档中添加内容

// ---------------第二阶段写入-------------------------

// 新建段落第一段

Paragraph p = new Paragraph();

p.add("Hello World Happy");

// 设置中文字体

BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true);

Font font = new Font(baseFont);

// 新建段落第二段,支持中文

Paragraph p2 = new Paragraph();

p2.setFont(font);

p2.add("非常风云");

document.add(p);

document.add(p2);

// ---------------第三阶段收尾-------------------------

// 添加完毕,关闭文档

document.close();

}

}

效果展示

java生成pdf如何让文字和图片显示在同一行

可以用表格布局

BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);

Font fontChinese5 = new Font(bf,8);

PdfPTable table1 = new PdfPTable(2); //表格两列

table1.setHorizontalAlignment(Element.ALIGN_CENTER); //垂直居中

table1.setWidthPercentage(100);//表格的宽度为100%

float[] wid1 ={0.75f,0.25f}; //两列宽度的比例

table1.setWidths(wid1);

table1.getDefaultCell().setBorderWidth(0); //不显示边框

PdfPCell cell11 = new PdfPCell(new Paragraph("SilkRoad24 GmbH",fontChinese5)); table1.addCell(cell11);

String imagepath = "D:\\wl\\logo.png";

Image image = Image.getInstance(imagepath);

table1.addCell(image);

document.add(table1);//增加到文档中

java中怎么利用poi和itext生成pdf文档

生成PDF文档代码如下:

package poi.itext;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.Color;

import com.lowagie.text.*;

import com.lowagie.text.pdf.*;

import com.lowagie.text.pdf.BaseFont;

/**

 * 创建Pdf文档

 * @author Administrator

 *

 */

public class HelloPdf

{

    public static void main(String[] args)throws Exception

    {

        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

        // 第一步,创建document对象

        Rectangle rectPageSize = new Rectangle(PageSize.A4);

        

        //下面代码设置页面横置

        //rectPageSize = rectPageSize.rotate();

        

        //创建document对象并指定边距

        Document doc = new Document(rectPageSize,50,50,50,50);

        Document document = new Document();

        try

        {

            // 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起

            //从而完成向Document写,即写入PDF文档

            PdfWriter.getInstance(document,new FileOutputStream("src/poi/itext/HelloWorld.pdf"));

            //第3步,打开文档

            document.open();

            //第3步,向文档添加文字. 文档由段组成

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

            Paragraph par = new Paragraph("世界你好",FontChinese);

            document.add(par);

            PdfPTable table = new PdfPTable(3);

            for(int i=0;i12;i++)

            {

                if (i == 0)

                {

                    PdfPCell cell = new PdfPCell();

                    cell.setColspan(3);

                    cell.setBackgroundColor(new Color(180,180,180));

                    cell.addElement(new Paragraph("表格头" , FontChinese));

                    table.addCell(cell);

                }

                else

                {

                    PdfPCell cell = new PdfPCell();

                    cell.addElement(new Paragraph("表格内容" , FontChinese));

                    table.addCell(cell);

                }

            }

            document.add(table);

        }

        catch (DocumentException de)

        {

            System.err.println(de.getMessage());

        }

        catch (IOException ioe)

        {

            System.err.println(ioe.getMessage());

        }

        //关闭document

        document.close();

        

        System.out.println("生成HelloPdf成功!");

     }

    

    

}

希望对你有帮助。

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;

/**

* 描述:TODO 【JAVA生成PDF】

*

*

* @title GeneratePDF

* @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();

}

}

「java生成pdf表格」java pdf表格

关于java生成pdf表格和java pdf表格的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。