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),咱们很高兴为您提供查询和咨询

相关文章
相关标签/搜索