Skip to content

Instantly share code, notes, and snippets.

@aont
Created November 6, 2010 12:58
Show Gist options
  • Save aont/665397 to your computer and use it in GitHub Desktop.
Save aont/665397 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp;
using System.Reflection;
using System.IO;
using System.Windows.Forms;
namespace Images2PDF
{
class Program
{
static void Main(string[] args)
{
string SavePath = new FileInfo(Application.ExecutablePath).DirectoryName + "\\Images.pdf";
using (PdfDocument Document = new PdfDocument())
{
foreach (string ImagePath in args)
{
Console.WriteLine(ImagePath);
PdfPage page = Document.AddPage();
page.Size = PageSize.A4;
using (XImage image = XImage.FromFile(ImagePath))
{
//page.Width = image.PointWidth;
//page.Height = image.PointHeight;
using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
double Height = image.PointHeight * page.Width / image.PointWidth;
if (Height < page.Height)
gfx.DrawImage(image, 0, (page.Height - Height) / 2, page.Width, Height);
else
{
double Width = image.PointWidth * page.Height / image.PointHeight;
gfx.DrawImage(image, (page.Width - Width) / 2, 0, Width, page.Height);
}
}
}
}
Document.Save(SavePath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment