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

bgjoqu24954 2013-03-04 05:14:22
对于文档处理控件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);
}
}
}
...全文
743 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
williamxia8 2015-04-01
  • 打赏
  • 举报
回复
可以,不过不支持中文,中文转为PDF以后就是乱码了
u010264677 2014-02-27
  • 打赏
  • 举报
回复
引用 楼主 bgjoqu24954 的回复:
对于文档处理控件Aspose.words,它的具有代表性的功能是在没有安装Microsoft Word的情况下,也能实现生成、打印、渲染、邮件合并,文档格式转换等功能。今天在使用Aspose.Words过程中,意外的发现这款文档处理软件的另外一个功能,它可以将图像转换为PDF文件,接下来就为大家分享一下实现这一功能的具体代码:
谢谢分享。顺带提个问题哈,既然这个能实现图像和PDF的转化,那是不是它也可以将Word 转化成图片呢
bgjoqu24954 2013-03-12
  • 打赏
  • 举报
回复
分享一下文档处理控件Aspose.words试用版下载:http://www.evget.com/zh-CN/product/564/download.aspx
  • 打赏
  • 举报
回复
谢谢楼上分享; 我最近也在看这个文档,以后多多交流》 其实核心代码就两句: Document _wordDoc = new Document(@"doc文档路径"); _wordDoc.Save(@"SaveFileName", SaveFormat.Pdf);
  • 打赏
  • 举报
回复
感谢分享,其实网上类似的东西倒是满多的。
xuhautian 2013-03-06
  • 打赏
  • 举报
回复
哦? 长见识了~

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧