aspose.word可以将word里的附件转换为PDF吗?

比如一个word文件中有粘贴的附件(可能是PDF、word、zip等格式),我需要将整个word文档一起转换为PDF,附件在转换后的PDF中还需要可以打开。或者将整个word文档内容转换成PDF后,附件直接转换成图片,然后给图片设置一个链接,可以跳转到附件的文件内容里。

请问这样的功能可以实现吗?示例文档见附件。

新建 好压 ZIP 压缩文件.zip (171.8 KB)

@chenxf

谢谢你的询问。 请注意,Aspose.Words模仿MS Word的行为。 如果使用MS Word将文档转换为PDF,则会得到相同的输出。

所以这个功能是可以实现的吗?

测试.zip (59.6 KB)

请参考这个压缩包。测试.docx是我要转换的文档,文字是“测试”,附件是“新建 DOCX 文档.pdf”(这个PDF我单独放出,你可以看到PDF的内容)。

如果将测试.docx转换成PDF后,附件“新建 DOCX 文档.pdf”的内容可以打开或者其他方式可以打开。

@chenxf

感谢您分享细节。 您可以使用Aspose.PDF从PDF文件中提取文本,并使用Aspose.Word将该文本插入Word文档。

此外,您可以使用Aspose.PDF将PDF文件的页面转换为图像,并使用Aspose.Words将它们插入到Word文档中。 请参阅以下文章。
将PDF页面转换为图像
插入图像

直接转换到pdf后,附件是以图片形式存储的,再使用为图片设置超链接的功能,为pdf文档中的图片添加上超链接。这样的方式可以实现吗?

@chenxf

谢谢你的询问。 在您的情况下,我们建议您使用以下解决方法。

1.使用Aspose.Words从Word文档中提取嵌入的PDF
2.将Word文档保存为PDF。
3.使用Aspose.PDF将提取的PDF文件作为附件添加到输出PDF文件中。
4.使用Aspose.PDF在PDF图标上创建链接到附件。
5.保存PDF文档。 打开输出PDF并双击PDF图标以打开文件。

下面的代码示例演示了如何实现上述解决方案。 希望这对你有所帮助。

Document doc = new Document(MyDir + "in.docx");

int i = 0;
Shape shape = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
System.out.println(shape.getOleFormat().getSuggestedExtension());
if(shape.getOleFormat() != null && shape.getOleFormat().getSuggestedExtension().equals(".pdf"))
{
    shape.getOleFormat().save(MyDir + "output.pdf");
}

doc.save(MyDir + "18.9.pdf");


com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(MyDir + "18.9.pdf");
com.aspose.pdf.FileSpecification fileSpecification = new com.aspose.pdf.FileSpecification(MyDir + "pdf file.pdf", "This is attachment.");
pdf.getEmbeddedFiles().add(fileSpecification);

com.aspose.pdf.ImagePlacementAbsorber ipa = new com.aspose.pdf.ImagePlacementAbsorber();
ipa.visit(pdf);

com.aspose.pdf.FileAttachmentAnnotation fileAttachment = new com.aspose.pdf.FileAttachmentAnnotation(pdf.getPages().get_Item(1), ipa.getImagePlacements().get_Item(1).getRectangle(), pdf.getEmbeddedFiles().get_Item(1));
fileAttachment.setTitle("Attached file");
fileAttachment.setOpacity(0.005);
pdf.getPages().get_Item(1).getAnnotations().add(fileAttachment);
pdf.save(MyDir + "LinkToAttachment.pdf");