翻譯|使用教程|編輯:黃竹雯|2019-02-21 09:32:32.000|閱讀 1249 次
概述:本文主要介紹如何使用Aspose.Words通過代碼將word和圖像轉換為PDF。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何將word和圖像轉換為PDF。
【下載Aspose.Words for .NET最新試用版】
要將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);
這里介紹如何使用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");
// 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);
下一篇文章將給大家帶來其他格式的文檔轉換方法,敬請期待~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn