无需依赖Adobe Acrobat,在Java中进行PDF格式转换全新攻略
生活随笔
收集整理的這篇文章主要介紹了
无需依赖Adobe Acrobat,在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 PDFPdfDocument doc = new PdfDocument();doc.loadFromFile("Sample.pdf");//save every PDF to .png imageBufferedImage 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 instancePdfDocument pdf = new PdfDocument();//Add a pagePdfPageBase page = pdf.getPages().add();//Load the imagePdfImage 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 documentpdf.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 filePdfDocument document = new PdfDocument();document.loadFromFile("Sales Report.pdf");//Convert to Pdf_A_1_BPdfNewDocument 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 filenewDoc.save("Output.pdf");newDoc.close();} }將PDF轉換Word
import com.spire.pdf.*;public class PdfToDoc {public static void main(String[] args) {//create a PdfDocument objectPdfDocument doc = new PdfDocument();//load a sample PDF filedoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Introduction of Spire.PDF for Java.pdf");//save as .doc filedoc.saveToFile("output/ToDoc.doc",FileFormat.DOC);//save as. docx filedoc.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 filePdfDocument pdf = new PdfDocument();pdf.loadFromFile("New Zealand.pdf");//Save to HTML formatpdf.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 pathString pluginPath = "D:/Qt/plugins_32";HtmlConverter.setPluginPath(pluginPath);//convert HTML to PDF and set the size of the result PDF pageHtmlConverter.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 filePdfDocument pdf = new PdfDocument();pdf.loadFromFile("Input.pdf");//Save to SVG imagepdf.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 filePdfDocument 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 PDFpdf.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 objectPdfDocument pdf = new PdfDocument();//Load a sample PDF filepdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");//Save to Excelpdf.saveToFile("output/ToXLS.xlsx", FileFormat.XLSX);} }總結
以上是生活随笔為你收集整理的无需依赖Adobe Acrobat,在Java中进行PDF格式转换全新攻略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql group_concat拼接
- 下一篇: php读取execl 实例,php读取e