Hello
I have rewritten it.
Code:
public void Rotate(string filename, int desiredRotation)
{
if (desiredRotation % 90 != 0)
throw new ArgumentException("Rotation must be multiple of 90");
var tempFilename = Path.Combine(Path.GetDirectoryName(filename), "rotate.pdf");
using (var image = XImage.FromFile(filename))
{
var newDocument = new PdfSharp.Pdf.PdfDocument();
var newPage = newDocument.AddPage();
var graphics = XGraphics.FromPdfPage(newPage);
var document = PdfSharp.Pdf.IO.PdfReader.Open(filename);
if (desiredRotation / 90 == 1 || desiredRotation / 90 == 3)
{
if (document.PageCount > 0)
{
var page = document.Pages[0];
var temp = page.Width;
page.Width = page.Height;
page.Height = temp;
}
}
graphics.RotateAtTransform(desiredRotation, new XPoint(newPage.Width / 2.0, newPage.Height / 2.0));
graphics.DrawImage(image, (newPage.Width - image.PixelWidth) / 2.0,
(newPage.Height - image.PixelHeight) / 2.0, image.PixelWidth, image.PixelHeight);
newDocument.Save(tempFilename);
}
File.Delete(filename);
File.Move(tempFilename, filename);
}
Now with this the content is centered and rotated. But the content is stretched. Do I use the wrong DrawImage methods?