Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created April 21, 2021 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/35546b3fb8da73ef8056e68a4d6caf97 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/35546b3fb8da73ef8056e68a4d6caf97 to your computer and use it in GitHub Desktop.
Create PDF from Images in C#
// 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