PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Dec 06, 2024 4:57 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Nov 13, 2024 8:47 pm 
Offline

Joined: Wed Nov 13, 2024 7:57 pm
Posts: 1
I’m encountering an issue when trying to print a PDF file that opens correctly in Adobe Reader. When I select ‘Microsoft Print to PDF’ in the PrintDialog UI, the resulting PDF file is empty. I suspect the problem lies in the following code, particularly with the XPdfForm.FromFile(filePath) and DrawImage methods, which seem to fail to render the PDF page onto the print page. In detail, the pdfForm, line 3, does not have Format field value, it is null. Does the DrawImage need that information? I can't set the Format and the action is not allowed!

I am using PDFsharp-MigraDoc-GDI, v6.1.1. Any help will be appreciated.

public void PrintPdfXPdForm(string filePath)
{
// Load the PDF document
XPdfForm pdfForm = XPdfForm.FromFile(filePath);

// Check if the PDF form is loaded correctly
if (pdfForm.PageCount == 0)
{
MessageBox.Show($"The PDF file could not be loaded or is empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Create a PrintDialog
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
// Create a PrintDocument
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += (sender, e) =>
{
// Select the first page of the PDF
pdfForm.PageNumber = 1;

// Calculate the scaling factor
double scaleX = e.PageBounds.Width / pdfForm.PixelWidth;
double scaleY = e.PageBounds.Height / pdfForm.PixelHeight;
double scale = Math.Min(scaleX, scaleY);

// Calculate the position to center the PDF content
double offsetX = (e.PageBounds.Width - pdfForm.PixelWidth * scale) / 2;
double offsetY = (e.PageBounds.Height - pdfForm.PixelHeight * scale) / 2;

// Draw the PDF page onto the PrintDocument with scaling
XGraphics gfx = XGraphics.FromGraphics(e.Graphics, new XSize(e.PageBounds.Width, e.PageBounds.Height), null);
gfx.ScaleTransform(scale);
gfx.DrawImage(pdfForm, offsetX / scale, offsetY / scale, pdfForm.PixelWidth, pdfForm.PixelHeight);
};

// Set the printer settings
printDocument.PrinterSettings = printDialog.PrinterSettings;

// Print the document
printDocument.Print();
}
}


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2024 8:46 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1019
Location: CCAA
PDFsharp cannot render PDF (see FAQ).
You cannot bypass that limitation, no matter how complicated your code looks.

DrawImage works for PDF only if the XGraphics object was created for a PDF page.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group