日期:2014-05-20  浏览次数:21061 次

如何使用文档处理控件Aspose.Word将图像转换为PDF文档
对于文档处理控件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))
            {