生成word文档转pdf的java代码
时间: 2023-05-22 11:07:11 浏览: 128
很高兴回答您的问题,以下是一些生成Word文档转PDF的Java代码示例:
```
import com.aspose.words.Document;
import com.aspose.words.PdfSaveOptions;
public class WordToPdfConverter {
public static void main(String[] args) throws Exception {
// Load Word document
Document doc = new Document("input.docx");
// Save document as PDF
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
doc.save("output.pdf", pdfSaveOptions);
}
}
```
```
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.codec.Base64;
public class WordToPdfConverter {
public static void main(String[] args) throws Exception {
// Load Word document as Base64 string
String base64WordDoc = "JVBERi0xLjcKJc..." // some base64 string
// Decode Base64 string to byte array
byte[] wordDocBytes = Base64.decode(base64WordDoc);
// Create new PDF document
Document pdfDoc = new Document();
PdfWriter writer = PdfWriter.getInstance(pdfDoc, new FileOutputStream("output.pdf"));
pdfDoc.open();
// Add Word document to PDF
InputStream is = new ByteArrayInputStream(wordDocBytes);
doc.add(new Paragraph(""));
doc.add(new com.itextpdf.text.Paragraph(new Chunk("Word document:")));
doc.add(Chunk.NEWLINE);
doc.newPage();
PdfReader reader = new PdfReader(is);
PdfImportedPage page;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
doc.add(instance);
}
doc.close();
}
}
```
希望这些代码对您有所帮助!
阅读全文