java pdf版本转换_无需依赖第三方软件,在Java中进行PDF格式转换全新攻略

将文档从一种格式转换为另一种格式是Spire.PDF的主要功能之一。这种转换只不过是加载和保存操作的组合。因此,使用Spire.PDF可以将文档从任何受支持的加载格式转换为任何受支持的保存格式。

本文整理了包括在Java中以下文件格式的转换指南,希望对您有所帮助:

PDF转换为图像以及图像转换为PDF

将PDF转换为PDF / A

将PDF转换Word

将PDF转换为HTML以及HTML转换为PDF

将PDF转换为SVG

将PDF转换为XPS并将XPS转换为PDF

将PDF转换为Excel

>>你可以下载Spire.PDF for Java测试体验。

(篇幅较长,建议收藏阅读)

PDF转换为图像以及图像转换为PDF

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import com.spire.pdf.PdfDocument;

import javax.imageio.ImageIO;

public class toImage {

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

//load the sample PDF

PdfDocument doc = new PdfDocument();

doc.loadFromFile("Sample.pdf");

//save every PDF to .png image

BufferedImage image;

for (int i = 0; i < doc.getPages().getCount(); i++) { image = doc.saveAsImage(i); File file = new File( String.format("ToImage-img-%d.png", i)); ImageIO.write(image, "PNG", file); } doc.close(); } }

import com.spire.pdf.PdfDocument;

import com.spire.pdf.PdfPageBase;

import com.spire.pdf.graphics.PdfImage;

public class ImageToPDF {

public static void main(String[] args){

//Create a PdfDocument instance

PdfDocument pdf = new PdfDocument();

//Add a page

PdfPageBase page = pdf.getPages().add();

//Load the image

PdfImage image = PdfImage.fromFile("Hydrangeas.jpg");

//Draw the image to the specific rectangular area of the page

double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();

double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();

double fitRate = Math.max(widthFitRate, heightFitRate);

double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;

double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;

page.getCanvas().drawImage(image, new Rectangle2D.Double(0, 0, fitWidth, fitHeight));

//Save the resultant document

pdf.saveToFile("ConvertImageToPDF.pdf");

}

}

将PDF转换为PDF / A

import com.spire.pdf.PdfConformanceLevel;

import com.spire.pdf.PdfDocument;

import com.spire.pdf.PdfNewDocument;

import com.spire.pdf.PdfPageBase;

import com.spire.pdf.graphics.PdfMargins;

import java.awt.geom.Dimension2D;

public class PDFToPDFA {

public static void main(String[] args)

{

//Load pdf file

PdfDocument document = new PdfDocument();

document.loadFromFile("Sales Report.pdf");

//Convert to Pdf_A_1_B

PdfNewDocument newDoc = new PdfNewDocument();

newDoc.setConformance(PdfConformanceLevel.Pdf_A_1_B);

for (PdfPageBase page : (Iterable) document.getPages()) {

Dimension2D size = page.getSize();

PdfPageBase p = newDoc.getPages().add(size, new PdfMargins(0));

page.createTemplate().draw(p, 0, 0);

}

//Save the file

newDoc.save("Output.pdf");

newDoc.close();

}

}

将PDF转换Word

import com.spire.pdf.*;

public class PdfToDoc {

public static void main(String[] args) {

//create a PdfDocument object

PdfDocument doc = new PdfDocument();

//load a sample PDF file

doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Introduction of Spire.PDF for Java.pdf");

//save as .doc file

doc.saveToFile("output/ToDoc.doc",FileFormat.DOC);

//save as. docx file

doc.saveToFile("output/ToDocx.docx",FileFormat.DOCX);

doc.close();

}

}

将PDF转换为HTML以及HTML转换为PDF

import com.spire.pdf.*;

public class PDFConversion {

public static void main(String[] args) {

//Load the PDF file

PdfDocument pdf = new PdfDocument();

pdf.loadFromFile("New Zealand.pdf");

//Save to HTML format

pdf.saveToFile("ToHTML.html", FileFormat.HTML);

}

}

import com.spire.pdf.graphics.PdfMargins;

import com.spire.pdf.htmlconverter.qt.HtmlConverter;

import com.spire.pdf.htmlconverter.qt.Size;

public class HtmltoPDF {

public static void main(String[] args) {

//define the HTML link and result PDF

String url = "https://www.e-iceblue.com/";

String fileName = "Result.pdf";

//Set the plugin path

String pluginPath = "D:/Qt/plugins_32";

HtmlConverter.setPluginPath(pluginPath);

//convert HTML to PDF and set the size of the result PDF page

HtmlConverter.convert(url, fileName, true, 1000000, new Size(600f, 900f), new PdfMargins(0));

}

}

将PDF转换为HTML以及HTML转换为PDF

import com.spire.pdf.*;

public class PDFConversion {

public static void main(String[] args) {

//Load the PDF file

PdfDocument pdf = new PdfDocument();

pdf.loadFromFile("Input.pdf");

//Save to SVG image

pdf.saveToFile("ToSVG.svg", FileFormat.SVG);

pdf.close();

}

}

将PDF转换为XPS并将XPS转换为PDF

import com.spire.pdf.*;

public class PDFtoXPS {

public static void main(String[] args) {

//Load the PDF file

PdfDocument pdf = new PdfDocument();

pdf.loadFromFile("Sample.pdf");

//Save to XPS

pdf.saveToFile("ToXPS.xps", FileFormat.XPS);

}

}

import com.spire.pdf.*;

public class PDFtoXPS {

public static void main(String[] args) {

//Load XPS

PdfDocument pdf = new PdfDocument();

pdf.loadFromXPS("ToXPS.xps");

//Save to PDF

pdf.saveToFile("toPDF.pdf", FileFormat.PDF);

}

}

将PDF转换为Excel

import com.spire.pdf.FileFormat;

import com.spire.pdf.PdfDocument;

public class ToXLS {

public static void main(String[] args) {

//Create a PdfDocument object

PdfDocument pdf = new PdfDocument();

//Load a sample PDF file

pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//Save to Excel

pdf.saveToFile("output/ToXLS.xlsx", FileFormat.XLSX);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值