怎样实现实际大小插入页面,然后实际大小打印出来? 图片是300dpi扫描的
另外发现, 在打印时, 选择如下图的选项后图片打出来会大一些(如何用代码静默打印时设置该项?), 但仍然不是扫描的图片的实际尺寸,
刚上手测试pdf组件还不太熟悉, 感谢帮助!
- Code: Select all
string imagePaths = @"C:\Users\administrator\Desktop\a.png;C:\Users\administrator\Desktop\b.png";
string result = MergeImagesToPdf(imagePaths);
Console.WriteLine(result);
string MergeImagesToPdf(string imagePaths)
{
// 获取图像路径数组
string[] paths = imagePaths.Split(';');
// 创建一个PDF文档
PdfDocument document = new PdfDocument();
// 添加一个A4页面
PdfPageBase page = document.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
// 加载图片
Image image1 = Image.FromFile(paths[0]);
Image image2 = Image.FromFile(paths[1]);
// 获取图片的真实尺寸(以点为单位,1英寸=72点)
float image1Width = image1.Width * 72 / image1.HorizontalResolution;
float image1Height = image1.Height * 72 / image1.VerticalResolution;
float image2Width = image2.Width * 72 / image2.HorizontalResolution;
float image2Height = image2.Height * 72 / image2.VerticalResolution;
// 页面宽高
float pageWidth = page.Canvas.ClientSize.Width;
float pageHeight = page.Canvas.ClientSize.Height;
// 图片之间的间隔
float spacing = 100;
// 计算图片位置,使其居中
float image1X = (pageWidth - image1Width) / 2;
float image1Y = (pageHeight - (image1Height + image2Height + spacing)) / 2;
float image2X = (pageWidth - image2Width) / 2;
float image2Y = image1Y + image1Height + spacing;
// 插入图片
page.Canvas.DrawImage(PdfImage.FromImage(image1), image1X, image1Y, image1Width, image1Height);
page.Canvas.DrawImage(PdfImage.FromImage(image2), image2X, image2Y, image2Width, image2Height);
// 保存PDF文档
string outputPdfPath = @"C:\Users\administrator\Desktop\MergedImages.pdf";
document.SaveToFile(outputPdfPath);
//document.Print(); //打印文档
document.Close();
return outputPdfPath;
}