Aspose.Words for .NET使用教程:将word和图像转换为PDF

Aspose.Words无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。本文将与大家分享如何将word和图像转换为PDF

将Microsoft Word文档转换为PDF

要将Microsoft Word文档转换为PDF,只需调用Document.Save方法并将文件的扩展名指定为“.PDF”。下面的代码示例使用默认选项将整个文档从DOC转换为PDF。你可以从此处下载此示例的模板文件。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();

// Load the document from disk.
Document doc = new Document(dataDir + "Template.doc");

dataDir = dataDir + "Template_out.pdf";

// Save the document in PDF format.
doc.Save(dataDir);
复制代码


将图像档转换为PDF

这里介绍如何使用Aspose.Words从图像创建PDF文档。虽然将图像转换为PDF不是Aspose.Words的主要特征,但此示例显示了Aspose.Words十分简洁易用。要使此代码有效,你需要将Aspose.Words和System.Drawing的参考文件添加到项目中。下面的代码允许转换单帧图像,如JPEG,PNG,BMP,EMF或WMF,以及多帧TIF图像和GIF到PDF。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();

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 + "TestTif_out.pdf");
ConvertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif_out.pdf");
复制代码


ConvertImageToPdf方法

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create 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);

    // 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);复制代码


下一篇文章将给大家带来其他格式的文档转换方法,敬请期待~

                          为你推荐:Aspose专题 - Aspose最新资源合集

                                         【下载Aspose.Words for .NET最新试用版


转载于:https://juejin.im/post/5c6e09a7e51d451254331505

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Aspose.Words for .NET提供了将Word文档中的内容转换为图片的功能,可以使用以下代码将Word文档转换为图片并调整图片格式列宽: ```csharp // 加载Word文档 Document doc = new Document("input.docx"); // 创建ImageSaveOptions对象 ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png); // 设置图片格式列宽 options.HorizontalResolution = 200; options.VerticalResolution = 200; // 遍历文档中的每个页面,并将其转换为图片 for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++) { // 创建MemoryStream对象 MemoryStream stream = new MemoryStream(); // 将当前页面保存为图片 doc.Save(stream, options); // 加载图片 Image image = Image.FromStream(stream); // 调整图片格式列宽 int newWidth = image.Width * 2; int newHeight = image.Height * 2; // 创建新的Bitmap对象 Bitmap newBitmap = new Bitmap(newWidth, newHeight); // 创建Graphics对象 Graphics graphics = Graphics.FromImage(newBitmap); // 绘制图像 graphics.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); // 保存图片 newBitmap.Save("output.png", ImageFormat.Png); // 释放资源 stream.Dispose(); image.Dispose(); graphics.Dispose(); newBitmap.Dispose(); } ``` 在上述代码中,我们使用了ImageSaveOptions对象来设置生成的图片的格式,包括水平分辨率和垂直分辨率。然后,我们遍历文档中的每个页面,并将其转换为图片。对于每个页面,我们先将其保存为MemoryStream对象,然后将其加载为Image对象。接着,我们根据需要调整图片的格式列宽,然后创建新的Bitmap对象,并使用Graphics对象将原始图像绘制到新的Bitmap对象中。最后,我们保存新的Bitmap对象为PNG格式的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值