Printdocument Printpageeventargs c# wpf Pagesize WPF

Asked

Viewed 133 times

5

I have the following code:

  PrintDocument pd = new PrintDocument();
  pd.PrintPage += new PrintPageEventHandler(arquivoImpressao);
  pd.DefaultPageSettings.PaperSize = new PaperSize("teste", 300, 600);
  PrintPreviewDialog printPrvDlg2 = new PrintPreviewDialog();
  ((Form)printPrvDlg2).WindowState = FormWindowState.Maximized;
  printPrvDlg2.Document = pd;
  printPrvDlg2.ShowDialog();
  printDocument.DocumentName = descricao;
  pd.Print();

When he opens the PrintPreviewDialog, it shows correctly on the image, but when it prints on the PDF printer ("Microsoft Print to PDF"), it generates in the size of the A4. Where am I going wrong?

I did a test in Word and made a change in the paper size, when it was generated, the paper was according to the size stipulated in Word, in case, I believe the problem is even in C#.

2 answers

1

The problem is that you changed the sheet size but did not inform the printing system that you want to use a custom sheet size through the value PaperKind.Custom. What makes the system choose the missing size (A4 in Brazil or Letter in the USA) and scale the image to the page.

To fix this problem you need to modify the property Rawkind 119 Papersize object indicating to the printing system its intention to use a custom sheet size.

Your code will look approximately like this:

  PrintDocument pd = new PrintDocument();
  pd.PrintPage += new PrintPageEventHandler(arquivoImpressao);
  pd.DefaultPageSettings.PaperSize = new PaperSize("teste", 300, 600);
  //******************modificação***************************//

  pd.DefaultPageSettings.PaperSize.RawKind = 119;

  //*******************************************************//
  PrintPreviewDialog printPrvDlg2 = new PrintPreviewDialog();
  ((Form)printPrvDlg2).WindowState = FormWindowState.Maximized;
  printPrvDlg2.Document = pd;
  printPrvDlg2.ShowDialog();
  printDocument.DocumentName = descricao;
  pd.Print();
  • I added this element, but is giving error, Rawkind does not accept this value, because they are different values

  • The documentation of Rawkind says that for custom sheet should be used an integer larger than 118. I will make the change.

-1

since you made the change in paper size, you could change in this line

pd.DefaultPageSettings.PaperSize = new PaperSize("teste", 300, 600);

put on the size of the paper you were able to print on

I did a test in word and made a change in the paper size...

Make the same change in C#

  • I’m making the change... that’s the line I’m putting, but when I print it prints on A4

  • but I say change this line instead of 300,600 , put another value that corresponds to what you used in word

  • I did this test and it doesn’t work, it only works in the view, but when I "print" in the pdf it generates in the A4

  • configure any printer to print at a certain size by the paper type in your computer’s device manager, then use printDoc.PrinterSettings.Printername = "printer nameProtector"; sometimes if you do this you can

Browser other questions tagged

You are not signed in. Login or sign up in order to post.