原創(chuàng)|使用教程|編輯:李顯亮|2020-12-23 11:49:19.623|閱讀 453 次
概述:Spire系列文檔處理API是國產(chǎn)開發(fā)工具中功能可媲美Aspose的強大控件,幫助開發(fā)者輕松將文檔功能集成到應用程序中。年終福利,小編為您傾情獻上Spire.PDF在Java中格式轉(zhuǎn)換的示例教程,記得收藏哦!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
將文檔從一種格式轉(zhuǎn)換為另一種格式是Spire.PDF的主要功能之一。這種轉(zhuǎn)換只不過是加載和保存操作的組合。因此,使用Spire.PDF可以將文檔從任何受支持的加載格式轉(zhuǎn)換為任何受支持的保存格式。
本文整理了包括在Java中以下文件格式的轉(zhuǎn)換指南,希望對您有所幫助:
>>你可以點擊這里下載Spire.PDF for Java測試體驗。
(篇幅較長,建議收藏閱讀)
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");
}
}
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();
}
}
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();
}
}
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 = "http://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));
}
}
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();
}
}
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);
}
}
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);
}
}
如果您有任何疑問或需求,請隨時,我們很高興為您提供查詢和咨詢。 本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn