- 使用C#从图像创建PDF
 
为了从图像创建PDF文件,我们将使用.NET API的
   整合所有格式API处理控件Aspose.Total永久授权火热促销中,联系客服立马1分钟了解全部! 
从图像创建PDF文件的步骤
以下是使用Aspose.PDF for .NET从图像创建PDF的步骤。
- 首先,创建一个新的PDF文档。
 - 获取数组中图像文件名称的列表。
 - 
对于列表中的每个图像,请执行以下操作:
- 将新页面添加到PDF文档并设置其属性。
 - 从列表中加载每个图像文件。
 - 将图像添加到页面的段落集合中。
 
 - 最后,保存PDF文档。
 
使用C#从图像创建PDF
以下是从图像创建PDF的步骤以及API参考。
- 创建一个Document 类的实例 。
 - 使用Directory.GetFiles(string)方法获取数组中图像文件名称的列表。
 - 
对于列表中的每个图像文件,请执行以下操作:
- 使用Document.Pages.Add() 方法将新的Page添加 到PDF文档 并设置页面的属性。
 - 将图像文件加载到 FileStream 对象中。
 - 使用Page.Paragraphs.Add(Image) 方法将新图像添加 到页面 。
 - 使用Image.ImageStream属性设置图像的流 。
 
 - 最后,使用Document.Save(String) 方法保存PDF文档 。
 
以下代码示例显示了如何从图像创建PDF。
// Create a new documentDocument doc = new Document();// Access image files in the folderstring[] fileList = Directory.GetFiles(@"D:/images/");foreach (String file in fileList){    // Add a page to pages collection of document    var page = doc.Pages.Add();    // Load image into stream    FileStream imageStream = new FileStream(file, FileMode.Open);    // Set margins so image will fit, etc.    page.PageInfo.Margin.Bottom = 0;    page.PageInfo.Margin.Top = 0;    page.PageInfo.Margin.Left = 0;    page.PageInfo.Margin.Right = 0;    page.CropBox = new Rectangle(0, 0, 400, 400);    // Create an image object    Image image1 = new Image();    // Add the image into paragraphs collection of the section    page.Paragraphs.Add(image1);    // Set the image file stream    image1.ImageStream = imageStream;}// Save resultant PDF filedoc.Save("document.pdf");
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!