Hello,
I have a problem with image resizing. It seems like the image is cropped instead of resized. Unfortunately I cannot attach the files I am working on because of the forum restrictions.
The image I am testing with can be found at
http://d.lib.ncsu.edu/adore-djatoka/res ... 39.88-2004Any pdf will do fine for testing.
Code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace PdfSharpTest
{
class Program
{
private const int MarginForPictures = 15;
static void Main(string[] args)
{
var pdfDocument = PdfReader.Open(EmptyPdf(), PdfDocumentOpenMode.Modify);
AddImage(pdfDocument);
pdfDocument.Save("D://result.pdf");
}
private static Stream EmptyPdf()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("PdfSharpTest.empty.pdf");
}
private static Stream TestImage()
{
return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("PdfSharpTest.resolver.jpeg");
}
private static void AddImage(PdfDocument pdfDocument)
{
using (var image = Image.FromStream(TestImage()))
{
var addPage = pdfDocument.AddPage();
addPage.Size = PageSize.Letter;
addPage.TrimMargins = new TrimMargins { All = MarginForPictures };
var gfx = XGraphics.FromPdfPage(addPage);
var pdfImage = XImage.FromGdiPlusImage(image);
var margin = XUnit.FromMillimeter(MarginForPictures).Point;
var widthFitRate = pdfImage.PointWidth / (addPage.Width.Point - (margin * 2));
var heightFitRate = pdfImage.PointHeight / (addPage.Height.Point - (margin * 2));
var fitRate = Math.Max(widthFitRate, heightFitRate);
var fitWidth = pdfImage.PointWidth / fitRate;
var fitHeight = pdfImage.PointHeight / fitRate;
gfx.DrawImage(pdfImage, margin, margin, fitWidth, fitHeight);
}
}
}
}