use aspose-words-21.11 convert word to pdf
- User
use aspose-words-21.11 convert word to pdf
- Assistant
As an AI Language model, I cannot perform the required action. However, here is an example of how you can use Aspose.Words for Java to convert a Microsoft Word document to PDF format:
-
First, download the Aspose.Words for Java library from the official website or Maven Repository.
-
Add the following dependencies to your project:
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>21.11</version> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>21.11</version> </dependency> -
Create a new Java program and import necessary classes:
import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.aspose.pdf.devices.PdfSaveOptions; -
Load the Word document using the
Documentclass:Document doc = new Document("input.docx"); -
Create an instance of
PdfSaveOptionsclass:PdfSaveOptions saveOptions = new PdfSaveOptions(); -
Set the output PDF file format using the
SaveFormatclass:saveOptions.setSaveFormat(SaveFormat.PDF); -
Save the Word document to PDF format using the
doc.save()method:doc.save("output.pdf", saveOptions); -
Finally, run the Java program to convert the Microsoft Word document to PDF format.
Note: Before running the program make sure that you have access to both Word Document and PDF files in the system.
-
- User
不要把pdf保存成文件,我希望是一个 output 类型
- Assistant
As an AI language model, I am not sure what you mean by an "output type". However, I assume you want to convert a Word document to PDF format using Aspose.Words for Java and retrieve the PDF document as a byte array instead of saving it to a file.
You can use the
ByteArrayOutputStreamclass in conjunction with Aspose.Words and Aspose.PDF for Java to achieve this. Here is some sample code that demonstrates how to do this:import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.aspose.pdf.devices.PdfSaveOptions; import com.aspose.pdf.facades.PdfConverter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class WordToPdfConverter { public static byte[] convertToPdf(byte[] wordDoc) throws IOException { // load Word document from a byte array ByteArrayInputStream input = new ByteArrayInputStream(wordDoc); Document doc = new Document(input); // set PDF save options PdfSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.setSaveFormat(SaveFormat.PDF); // convert Word document to PDF ByteArrayOutputStream output = new ByteArrayOutputStream(); doc.save(output, saveOptions); // load PDF document from the output byte array byte[] pdfDoc = output.toByteArray(); PdfConverter converter = new PdfConverter(); converter.bindPdf(new ByteArrayInputStream(pdfDoc)); // extract PDF pages as images and append them to a new PDF document ByteArrayOutputStream finalOutput = new ByteArrayOutputStream(); converter.doConvert(); converter.saveAsTIFF(finalOutput); // return the final PDF document as a byte array return finalOutput.toByteArray(); } }Note that this code loads a Word document from a byte array, converts it to a PDF document, and then extracts the PDF pages as images before appending them to a new PDF document. Finally, the code returns the byte array of the final PDF document. You can modify this code to suit your specific requirements.
- User
use aspose-cells-21.11 convert word to pdf output
- Assistant
Aspose.Cells is a library primarily intended for working with Microsoft Excel documents, not Word documents. As for Word to PDF conversions, Aspose.Words is the appropriate library to use.
That being said, if you have a Word document that contains Excel tables or charts and you want to convert it to PDF format using Aspose.Cells, you can use the following sample code as a starting point:
import com.aspose.cells.Workbook; import com.aspose.cells.WorkbookRender; import com.aspose.words.Document; import com.aspose.words.SaveFormat; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class WordToPdfConverter { public static byte[] convertToPdf(byte[] wordDoc) throws IOException { // load Word document from a byte array ByteArrayInputStream input = new ByteArrayInputStream(wordDoc); Document doc = new Document(input); // save Word document to a temporary file ByteArrayOutputStream tempFile = new ByteArrayOutputStream(); doc.save(tempFile, SaveFormat.DOCX); // load temporary file into an Aspose.Cells Workbook ByteArrayInputStream tempInput = new ByteArrayInputStream(tempFile.toByteArray()); Workbook workbook = new Workbook(tempInput); // create an instance of the WorkbookRender class WorkbookRender wr = new WorkbookRender(workbook, new PdfSaveOptions()); // set rending options wr.setHideAutoFilterDropDowns(true); wr.getSaveOptions().setOnePagePerSheet(true); // render workbook to PDF ByteArrayOutputStream output = new ByteArrayOutputStream(); wr.toPdf(output); // return the PDF document as a byte array return output.toByteArray(); } }In this code, we first load the Word document from a byte array and save it to a temporary file in DOCX format (which can be opened by Aspose.Cells). Next, we load the temporary file into an Aspose.Cells Workbook and use the WorkbookRender class to convert it to PDF format. Finally, we return the resulting PDF document as a byte array.
Note that this code assumes that the Word document contains an Excel table or chart that is supported by Aspose.Cells.