如何使用文档处理控件Aspose.Word将图像转换为PDF文档

如何使用文档处理控件Aspose.Word将图像转换为PDF文档,第1张

对于文档处理控件Aspose.words,它的具有代表性的功能是在没有安装Microsoft Word的情况下,也能实现生成、打印、渲染、邮件合并,文档格式转换等功能。今天在使用Aspose.Words过程中,意外的发现这款文档处理软件的另外一个功能,它可以将图像转换为PDF文件,接下来就为大家分享一下实现这一功能的具体代码:

using System

using System.Drawing

using System.Drawing.Imaging

using System.IO

using System.Reflection

 

using Aspose.Words

using Aspose.Words.Drawing

 

namespace ImageToPdf

{

    class Program

    {

        public static void Main(string[] args)

        {

            // Sample infrastructure.

            string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar

            string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath

 

            ConvertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg Out.pdf")

            ConvertImageToPdf(dataDir + "Test.png", dataDir + "TestPng Out.pdf")

            ConvertImageToPdf(dataDir + "Test.wmf", dataDir + "TestWmf Out.pdf")

            ConvertImageToPdf(dataDir + "Test.tiff", dataDir + "TestTiff Out.pdf")

            ConvertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif Out.pdf")

        }

 

        /// <summary>

        /// Converts an image to PDF using Aspose.Words for .NET.

        /// </summary>

        /// <param name="inputFileName">File name of input image file.</param>

        /// <param name="outputFileName">Output PDF file name.</param>

        public static void ConvertImageToPdf(string inputFileName, string outputFileName)

        {

            // 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)

 

            // Read the image from file, ensure it is disposed.

            using (Image image = Image.FromFile(inputFileName))

            {

                // Find which dimension the frames in this image represent. For example 

                // the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension". 

                FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]) 

 

                 // Get the number of frames in the image.

                int framesCount = image.GetFrameCount(dimension)

 

                // Get the number of frames in the image.

                int framesCount = image.GetFrameCount(FrameDimension.Page)

 

                // 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 TIFF.

                    if (frameIdx != 0)

                        builder.InsertBreak(BreakType.SectionBreakNewPage)

 

                    // Select active frame.

                    image.SelectActiveFrame(dimension, 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.PageSetup

                    ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution)

                    ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution)

 

                    // 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.PageWidth,

                        ps.PageHeight,

                        WrapType.None)

                }

            }

 

            // Save the document to PDF.

            doc.Save(outputFileName)

        }

    }

}

最近项目上需要用到office文档转pdf的需求,之前了解到aspose插件可做此事,因此本文总结一下相关经验。

闲话少说,本文结尾处附有.NET源码,如对过程没兴趣,可跳过;

主要是做转换word,excel,ppt;

要实现此功能一般有两种做法,分别是使用第三方插件和使用office自带的另存为功能。

以下是我使用这两个的方案的对比:

本文仅介绍使用aspose这个第三方插件的使用。

office接口实现方式,请看我的另外两篇文章:

1. Windows服务-Office转PDF文件

2. Windows服务-Office转PDF服务部署到服务器一系列问题

网上有很多资料,我这边放一下我参考的资料:

https://blog.csdn.net/bianqing0305/article/details/94602472

此文基本就实现了这个插件的转化功能。

这里面还有一个很好的干货,就是有一个可用的aspose key,这对于我来做测试是再好不过的了,心里暗喜!

但是我在使用的时候遇到了一些问题,就是PPT文件转pdf始终是失败,后台分析原因发现可能是版本的问题。

于是我做了以下尝试:

以下是源码地址:

https://gitee.com/zxws1009/Aspose.DocumentConvert.git

温馨提醒,如要使用此插件,请购买正版aspose key;

欢迎一起留言交流!

Aspose.PDF不能将word文件转换成PDF文件,因此就会出现aspose把word文档转换为pdf文档时出现乱码情况。

aspose的官网上查了下文档,Aspose.PDF无法直接转换word文档,需要先有个中间步骤。就是先把word转换成XML格式的文件,再使用Aspose.PDF绑定这个XML,再保存为PDF格式。如果word文档中有图片,则生成XML的时候会在临时文件夹中生成图片。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10653887.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存