导航:首页 > 文档加密 > java写入pdf

java写入pdf

发布时间:2022-09-21 17:47:34

java中怎么将word转pdf

能安装第三方软件的话,可以考虑以使用Spire.Doc for Java:

你可以在Java程序中添加 Spire.Doc.jar 文件作为依赖项。可以从这个链接下载 JAR 文件;如果使用Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。

repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository></repositories><dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.3</version>
</dependency></dependencies>

将Word转换成PDF:

Spire.Doc for Java拥有强大的文件转换功能,其提供了 Document. saveToFile(String fileName, FileFormat fileFormat) 方法可将 Word 文档转为多种格式的目标文件,下面是转为 PDF 的方法步骤:

Java代码如下:

import com.spire.doc.*;public class WordToPDF{
public static void main(String[] args) {
//实例化Document类的对象
Document doc = new Document();

//加载Word
doc.loadFromFile("测试.docx");

//保存为PDF格式
doc.saveToFile("WordToPDF.pdf",FileFormat.PDF);
}

}

希望对您有帮助。

❷ java中如何实现向已有的PDF文件插入附件

可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:

import com.spire.pdf.annotations.*;

import com.spire.pdf.attachments.PdfAttachment;

import com.spire.pdf.graphics.*;

import java.awt.*;

import java.awt.geom.Dimension2D;

import java.awt.geom.Rectangle2D;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class AttachFiles {
public static void main(String[] args) throws IOException {


//创建PdfDocument对象


PdfDocument doc = new PdfDocument();


//加载PDF文档


doc.loadFromFile("C:\Users\Administrator\Desktop\sample.pdf");


//添加附件到PDF


PdfAttachment attachment = new PdfAttachment("C:\Users\Administrator\Desktop\使用说明书.docx");


doc.getAttachments().add(attachment);


//绘制标签

String label = "财务报表.xlsx";


PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);


double x = 35;


double y = doc.getPages().get(0).getActualSize().getHeight() - 200;


doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);

//添加注释附件到PDF


String filePath = "C:\Users\Administrator\Desktop\财务报表.xlsx";


byte[] data = toByteArray(filePath);


Dimension2D size = font.measureString(label);


Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);


PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);


annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));


annotation.setFlags(PdfAnnotationFlags.Default);


annotation.setIcon(PdfAttachmentIcon.Graph);


annotation.setText("点击打开财务报表.xlsx");


doc.getPages().get(0).getAnnotationsWidget().add(annotation);

//保存文档


doc.saveToFile("Attachments.pdf");
}

//读取文件到byte数组


public static byte[] toByteArray(String filePath) throws IOException {

File file = new File(filePath);


long fileSize = file.length();


if (fileSize > Integer.MAX_VALUE) {


System.out.println("file too big...");


return null;


}


FileInputStream fi = new FileInputStream(file);


byte[] buffer = new byte[(int) fileSize];


int offset = 0;


int numRead = 0;


while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {


offset += numRead;


}

if (offset != buffer.length) {


throw new IOException("Could not completely read file "
+ file.getName());


}


fi.close();


return buffer;


}


}

效果:

❸ 如何用java把好多office文件写入1个pdf文件

你用JAVA想干嘛?如果你是想把这数个文件放在一起,成一个PDF档,那么楼上讲的那个软件就可以做到了,下载安装后,点击制作PDF,将想制成PDF档的文件也可按你想要的顺序一个一个拖过去即可。

❹ java将html文件转成pdf

核心代码如下
package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;

public class App {
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='hmkcode.com'>hmkcode.com</a>";

public static void main( String[] args ) throws FileNotFoundException, IOException
{
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));

System.out.println( "PDF Created!" );
}
}

❺ java将pdf文件写入bytes[]

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

//现在我有一个Byte[]
byte[] bs = new byte[]{1,2,3,4,5};

//确定写出文件的位置
File file = new File("Test.txt");

//建立输出字节流
FileOutputStream fos = new FileOutputStream(file);

//用FileOutputStream 的write方法写入字节数组
fos.write(bs);
System.out.println("写入成功");

//为了节省IO流的开销,需要关闭
fos.close();

}
}

总结:因为你写入的是字节,所以会显示乱码。字节流就是这样的,用于读取文件和复制任何东西。

❻ java导出的pdf文件是空白是因为什么

导出的PDF大小是否是0kb?如果是0,那就是没有把内容写入到pdf,看下如下导出方法:

importjava.awt.*;

importjava.awt.geom.Point2D;

importjava.awt.geom.Rectangle2D;

importjava.io.*;

importcom.spire.pdf.PdfPageBase;

importcom.spire.pdf.graphics.*;

{

publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{

//创建PdfDocument对象

PdfDocumentdoc=newPdfDocument();

//添加一页

PdfPageBasepage=doc.getPages().add();

//标题文字

Stringtitle="Java基础语法";

//创建单色画刷对象

PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));

PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.BLACK));

//创建TrueType字体对象

PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);

PdfTrueTypeFontfont2=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);

//创建PdfStringFormat对象

PdfStringFormatformat1=newPdfStringFormat();

format1.setAlignment(PdfTextAlignment.Center);//设置文字居中

//使用drawString方法绘制标题文字

page.getCanvas().drawString(title,font1,brush1,newPoint2D.Float(page.getActualBounds(true).width/2,0),format1);

//从txt文件读取内容到字符串

Stringbody=readFileToString("C:\Users\Administrator\Desktop\bodyText.txt");

//创建PdfStringFormat对象

PdfStringFormatformat2=newPdfStringFormat();

format2.setParagraphIndent(20);//设置段首缩进

//创建Rectangle2D对象

Rectangle2D.Floatrect=newRectangle2D.Float(0,30,page.getActualBounds(true).width,page.getActualBounds(true).height);

//使用drawString方法在矩形区域绘制主体文字

page.getCanvas().drawString(body,font2,brush2,rect,format2);

//保存到PDF文档

doc.saveToFile("ouput.pdf");

}

//自定义方法读取txt文件内容到字符串

(Stringfilepath)throwsFileNotFoundException,IOException{

StringBuildersb=newStringBuilder();

Strings="";

BufferedReaderbr=newBufferedReader(newFileReader(filepath));

while((s=br.readLine())!=null){

sb.append(s+" ");

}

br.close();

Stringstr=sb.toString();

returnstr;

}

}

PDF到处效果如下图:

❼ Java如何使用Java向PDF页面中添加文本

试试这个教程,需要依赖免费版的Spire.Pdf.jar包

importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.awt.geom.Rectangle2D;
importjava.io.*;
importcom.spire.pdf.PdfPageBase;
importcom.spire.pdf.graphics.*;

{

publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{

//创建PdfDocument对象
PdfDocumentdoc=newPdfDocument();

//添加一页
PdfPageBasepage=doc.getPages().add();

//标题文字
Stringtitle="标题";

//创建单色画刷对象
PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));
PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.BLACK));

//创建TrueType字体对象
PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);
PdfTrueTypeFontfont2=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);

//创建PdfStringFormat对象
PdfStringFormatformat1=newPdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//设置文字居中

//使用drawString方法绘制标题文字
page.getCanvas().drawString(title,font1,brush1,newPoint2D.Float(page.getActualBounds(true).width/2,0),format1);

//从txt文件读取内容到字符串
Stringbody=readFileToString("C:\Users\Administrator\Desktop\bodyText.txt");

//创建PdfStringFormat对象
PdfStringFormatformat2=newPdfStringFormat();
format2.setParagraphIndent(20);//设置段首缩进

//创建Rectangle2D对象
Rectangle2D.Floatrect=newRectangle2D.Float(0,30,page.getActualBounds(true).width,page.getActualBounds(true).height);

//使用drawString方法在矩形区域绘制主体文字
page.getCanvas().drawString(body,font2,brush2,rect,format2);

//保存到PDF文档
doc.saveToFile("ouput.pdf");
}

//自定义方法读取txt文件内容到字符串
(Stringfilepath)throwsFileNotFoundException,IOException{

StringBuildersb=newStringBuilder();
Strings="";
BufferedReaderbr=newBufferedReader(newFileReader(filepath));

while((s=br.readLine())!=null){
sb.append(s+" ");
}
br.close();
Stringstr=sb.toString();
returnstr;
}

}

❽ 怎么用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】
* <p>
*
* @title GeneratePDF
* @author SYJ
* @email [email protected]
* @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();
}

}

❾ java创建pdf文件写入不进去

通常需要用到用于读、写、编辑PDF文件的库,你可以参考下面采用spire.pdf.jar来创建PDF的步骤及方法:

  1. 首先需要引入jar包。具体的引入方法可以自行网络搜索。

  2. 创建PdfDocument类的对象,并通过PdfDocument.getPages().add()方法添加页码。

  3. 定义标题文字。

  4. 创建PdfSolidBrush画刷、PdfTrueTypeFont字体、PdfStringFormat字符串、Rectangle2D等对象,用于指定字符串绘制效果、字体、格式、绘制区域等。

  5. 通过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 + " ");
}
br.close();
String str = sb.toString();
return str;
}

}

❿ java如何创建pdf文件,并将数据库数据写入pdf

写入PDF? 可以,不过需要下载操作 PDF 的JAR包!操作起来不是太麻烦,不过就是生成的时间比较长。。。

flex 是 flash的一种延伸,flash 对于网站加载来说本来就比较慢,比较的消耗资源,生成PDF 可想而知!

其次 flex 发布到jobss tomcat 等服务器中时,你的 flex 是编译成flash,还是直接将xml放入容器中让其自动生成flash呢? 如果是的话,那反应就更慢了。。。

再后者,flex 虽然说是 flash 只要客户端支持flash就能看见 你的网站,但flex 需要在jobss tomcat 等容器中配置的,有免费的有收费的,它们的配置方法都不一样的。。。

好了就说到这里,虽然有点废话,并且与主题无关,但希望对LZ有帮助!

阅读全文

与java写入pdf相关的资料

热点内容
rf3148编程器 浏览:505
浙江标准网络服务器机柜云主机 浏览:587
设置网络的服务器地址 浏览:600
java图形界面设计 浏览:751
纯前端项目怎么部署到服务器 浏览:538
瓜子脸程序员 浏览:505
如何保证服务器优质 浏览:94
小微信aPP怎么一下找不到了 浏览:299
算法纂要学术价值 浏览:975
程序员你好是什么意思 浏览:801
倩女幽魂老服务器如何玩 浏览:561
电子钟单片机课程设计实验报告 浏览:999
看加密频道 浏览:381
程序员算不算流水线工人 浏览:632
三星电视我的app怎么卸载 浏览:44
简述vi编译器的基本操作 浏览:507
让程序员选小号 浏览:91
加强数字货币国际信息编译能力 浏览:584
购买的app会员怎么退安卓手机 浏览:891
程序员的种类及名称 浏览:295