Word文檔轉換:如何使用Aspose.words在Java中將Word和圖像轉換爲PDF

Aspose.Words for Java是功能豐富的文字處理API,開發人員能夠在本身的Java應用程序中嵌入生成,修改,轉換,呈現和打印Microsoft Word支持的全部格式的功能。它不依賴於Microsoft Word,可是它提供了Microsoft Word經過其API支持的功能。編程

>>Aspose.Words for Java已經更新至v20.7,有97項改進和修復,點擊下載體驗app


輕鬆可靠地將文檔從一種格式轉換爲另外一種格式的能力是Aspose.Words的一項關鍵功能。PDF是一種最受歡迎的轉換格式,一種固定佈局的格式,能夠在各類平臺上呈現文檔時保留其原始外觀。Aspose.Words中使用「渲染」一詞來描述將文檔轉換爲分頁或具備頁面概念的文件格式的過程。ide

將Word文檔轉換爲PDF

從Word到PDF的轉換是一個至關複雜的過程,須要幾個計算階段。Aspose.Words佈局引擎模仿了Microsoft Word的頁面佈局引擎的工做方式,使PDF輸出文檔看起來與Microsoft Word中所看到的儘量接近。使用Aspose.Words,您能夠經過編程方式將文檔從DOC或DOCX格式轉換爲PDF,而無需使用Microsoft Office。本文介紹瞭如何執行此轉換。函數

請注意,文檔中的頁數會影響轉換時間。oop

將DOC或DOCX轉換爲PDF佈局

在Aspose.Words中從DOC或DOCX文檔格式轉換爲PDF格式很是容易,只需兩行代碼便可完成:ui

  • 經過使用擴展名指定文檔名稱,使用其構造函數之一將文檔加載到 Document對象中。
  • 調用Document對象上的Document.Save方法 之一,並經過輸入擴展名爲「 .PDF」的文件名將所需的輸出格式指定爲PDF。

下面的代碼示例演示如何使用Save方法將文檔從DOCX轉換爲PDF:url

// Load the document from disk.
Document doc = new Document(dataDir + "Template.doc");

// Save the document in PDF format.
dataDir = dataDir + "output.pdf";
doc.save(dataDir);

請注意,使用相同的技術,能夠將任何流程佈局格式的文檔轉換爲PDF格式。spa

轉換爲各類PDF標準 .net

Aspose.Words提供 PdfCompliace 枚舉以支持將DOC或DOCX轉換爲各類PDF格式標準(例如PDF 1.7,PDF 1.5等)。下面的代碼示例演示如何將文檔轉換爲PDF使用1.7 PdfSaveOptions 與符合PDF17:

// The path to the documents directory.
Document originalDoc = new Document(dataDir + "Document.docx");

// Provide PDFSaveOption compliance to PDF17
// or just convert without SaveOptions
PdfSaveOptions pso = new PdfSaveOptions();
pso.setCompliance(PdfCompliance.PDF_17);

originalDoc.save(dataDir + "Output.pdf", pso);

將圖像轉換爲PDF

轉換爲PDF不受Microsoft Word文檔格式的限制。Aspose.Words支持的任何格式,包括以編程方式建立的格式,均可以轉換爲PDF。例如,咱們能夠將單頁圖像(例如JPEG,PNG,BMP,EMF或WMF)以及多頁圖像(例如TIFF和GIF)轉換爲PDF。

下面的代碼示例演示如何將JPEG和TIFF圖像轉換爲PDF:

//將指定格式的圖像轉換爲PDF。
ConvertImageToPDF(dataDir +  「 Test.jpg 」,dataDir +  「 TestJpg_out.pdf 」);
ConvertImageToPDF(dataDir +  「 Test.tiff 」,dataDir +  「 TestTif_out.pdf 」);
/**
  * Converts an image to PDF using Aspose.Words for Java.
  *
  * @param inputFileName File name of input image file.
  * @param outputFileName Output PDF file name.
  * @throws Exception
  */
  public static void ConvertImageToPDF(String inputFileName, String outputFileName) throws Exception {
  // Create Aspose.Words.Document and DocumentBuilder.
  // The builder makes it simple to add content to the document.
  Document doc = new Document();
  DocumentBuilder builder = new DocumentBuilder(doc);
   
  // Load images from the disk using the appropriate reader.
  // The file formats that can be loaded depends on the image readers available on the machine.
  ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
  ImageReader reader = ImageIO.getImageReaders(iis).next();
  reader.setInput(iis, false);
   
  // Get the number of frames in the image.
  int framesCount = reader.getNumImages(true);
   
  // Loop through all frames.
  for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) {
  // Insert a section break before each new page, in case of a multi-frame image.
  if (frameIdx != 0)
  builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
   
  // Select active frame.
  BufferedImage image = reader.read(frameIdx);
   
  // We want the size of the page to be the same as the size of the image.
  // Convert pixels to points to size the page to the actual image size.
  PageSetup ps = builder.getPageSetup();
  ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
  ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));
   
  // Insert the image into the document and position it at the top left corner of the page.
  builder.insertImage(
  image,
  RelativeHorizontalPosition.PAGE,
  0,
  RelativeVerticalPosition.PAGE,
  0,
  ps.getPageWidth(),
  ps.getPageHeight(),
  WrapType.NONE);
  }
   
  if (iis != null) {
  iis.close();
  reader.dispose();
  }
   
  doc.save(outputFileName);
  }

若是您有任何疑問或需求,請隨時加入Aspose技術交流羣(642018183),咱們很高興爲您提供查詢和諮詢

相關文章
相關標籤/搜索