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);
}
}
}
6 个解决方案
#1
哦? 长见识了~
#2
感谢分享,其实网上类似的东西倒是满多的。
#3
谢谢楼上分享;
我最近也在看这个文档,以后多多交流》
其实核心代码就两句:
Document _wordDoc = new Document(@"doc文档路径");
_wordDoc.Save(@"SaveFileName", SaveFormat.Pdf);
我最近也在看这个文档,以后多多交流》
其实核心代码就两句:
Document _wordDoc = new Document(@"doc文档路径");
_wordDoc.Save(@"SaveFileName", SaveFormat.Pdf);
#4
分享一下文档处理控件Aspose.words试用版下载:http://www.evget.com/zh-CN/product/564/download.aspx
#5
谢谢分享。顺带提个问题哈,既然这个能实现图像和PDF的转化,那是不是它也可以将Word 转化成图片呢
#6
可以,不过不支持中文,中文转为PDF以后就是乱码了
#1
哦? 长见识了~
#2
感谢分享,其实网上类似的东西倒是满多的。
#3
谢谢楼上分享;
我最近也在看这个文档,以后多多交流》
其实核心代码就两句:
Document _wordDoc = new Document(@"doc文档路径");
_wordDoc.Save(@"SaveFileName", SaveFormat.Pdf);
我最近也在看这个文档,以后多多交流》
其实核心代码就两句:
Document _wordDoc = new Document(@"doc文档路径");
_wordDoc.Save(@"SaveFileName", SaveFormat.Pdf);
#4
分享一下文档处理控件Aspose.words试用版下载:http://www.evget.com/zh-CN/product/564/download.aspx
#5
谢谢分享。顺带提个问题哈,既然这个能实现图像和PDF的转化,那是不是它也可以将Word 转化成图片呢
#6
可以,不过不支持中文,中文转为PDF以后就是乱码了