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);
}
}
總結
以上是生活随笔為你收集整理的java pdf版本转换_无需依赖第三方软件,在Java中进行PDF格式转换全新攻略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优秀的在线工具
- 下一篇: 栅栏密码解密——Java实现