Printing: Fix slight translocation to the bottom right
Test / Run tests (push) Successful in 3m2s

This commit is contained in:
2026-08-10 13:10:10 +02:00
parent 2bf926c84c
commit f13a836ebd
+7 -6
View File
@@ -9,15 +9,13 @@ namespace Elwig.Helpers.Printing {
private readonly IntPtr _handle;
private readonly int _pageCount;
private readonly double _dpi;
private int _currentPage;
public PdfPrintDocument(string path, string? password = null, double dpi = 300.0) :
public PdfPrintDocument(string path, string? password = null) :
base() {
_handle = PdfiumNative.FPDF_LoadDocument(path, password);
_pageCount = PdfiumNative.FPDF_GetPageCount(_handle);
_dpi = dpi;
}
protected override void Dispose(bool disposing) {
@@ -35,8 +33,8 @@ namespace Elwig.Helpers.Printing {
IntPtr page = PdfiumNative.FPDF_LoadPage(_handle, _currentPage);
double width = PdfiumNative.FPDF_GetPageWidth(page);
double height = PdfiumNative.FPDF_GetPageHeight(page);
int pixelWidth = (int)(width / 72.0 * _dpi);
int pixelHeight = (int)(height / 72.0 * _dpi);
int pixelWidth = (int)(width / 72.0 * (evt.Graphics?.DpiX ?? 300.0));
int pixelHeight = (int)(height / 72.0 * (evt.Graphics?.DpiY ?? 300.0));
IntPtr bitmap = PdfiumNative.FPDFBitmap_Create(pixelWidth, pixelHeight, 1);
PdfiumNative.FPDF_RenderPageBitmap(bitmap, page, 0, 0, pixelWidth, pixelHeight, 0, 0);
@@ -44,7 +42,10 @@ namespace Elwig.Helpers.Printing {
IntPtr buffer = PdfiumNative.FPDFBitmap_GetBuffer(bitmap);
int stride = PdfiumNative.FPDFBitmap_GetStride(bitmap);
using (var bmp = new Bitmap(pixelWidth, pixelHeight, stride, PixelFormat.Format32bppArgb, buffer)) {
evt.Graphics?.DrawImage(bmp, evt.PageBounds);
evt.Graphics?.TranslateTransform(
-evt.PageSettings.HardMarginX,
-evt.PageSettings.HardMarginY);
evt.Graphics?.DrawImage(bmp, new RectangleF(0, 0, (float)(width / 72.0 * 100.0), (float)(height / 72.0 * 100.0)));
}
_currentPage++;