Created
April 21, 2021 03:32
-
-
Save aspose-com-gists/35546b3fb8da73ef8056e68a4d6caf97 to your computer and use it in GitHub Desktop.
Create PDF from Images in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new document | |
Document doc = new Document(); | |
// Access image files in the folder | |
string[] 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 file | |
doc.Save("document.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment