彩票走势图

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

翻译|使用教程|编辑:黄竹雯|2019-02-21 09:32:32.000|阅读 1187 次

概述:本文主要介绍如何使用Aspose.Words通过代码将word和图像转换为PDF。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

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

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

将Microsoft Word文档转换为PDF


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

// For complete examples and data files, please go to //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 //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 //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相关信息的朋友可以点击" "~

标签:文档管理文件格式转换wordAspose.words文档处理

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP