1.如何使用文档处理控件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"); } /// /// Converts an image to PDF using Aspose.Words for .NET. /// /// File name of input image file. /// Output PDF file name. 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);="" }="" }="">
转载请注明出处51数据库 » aspose网页导出word