Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing without ShowDialog gives blank pages

Tags:

c#

printing

wpf

I am experiencing an odd issue with printing from a WPF project. I'm printing a screen capture of the application for reporting purposes, and all that works just fine. Currently the user presses print, the print dialog appears, and they print out the capture image.

However, I want to be able to print directly to the default printer without showing the dialog box. This should be easily done by commenting out the ShowDialog() statement and allowing the rest to just happen. The printer still prints, but the pages are always blank. Can anyone explain this behavior?

private void PrintCurrentScreen()
{
    PrintDialog PD = new PrintDialog();
    PD.PrintTicket.OutputColor = OutputColor.Grayscale;
    PD.PrintTicket.OutputQuality = OutputQuality.Draft;

    PrintTicket PT = new PrintTicket();
    PT.PageOrientation = PageOrientation.Landscape;
    PT.CopyCount = 1;
    PT.PageBorderless = System.Printing.PageBorderless.Borderless;

    //---Blank pages print when commented out---//
    //if (PD.ShowDialog() == true)
    //{
    PD.PrintTicket = PT;

    DrawingVisual DV = new DrawingVisual();
    DV.Offset = new Vector(20, 20);
    DrawingContext DC = DV.RenderOpen();
    DC.DrawImage(previewimage.Source, new Rect(new Size(PD.PrintableAreaWidth - 40, PD.PrintableAreaHeight - 40)));
    DC.Close();

    PD.PrintVisual(DV, "TEST");
    //}
}
like image 929
Lee Harrison Avatar asked Aug 31 '12 16:08

Lee Harrison


People also ask

Why are my pages coming out blank when I print?

Clean the print head, if necessary. Make sure the paper size, orientation, and layout settings in your printer software are correct. Make sure your document does not contain blank pages. If your printer software has a Preview option, you can check for blank pages before you print and remove them, if necessary.

Why does my printer keep printing a blank page after every document?

Corrupted or incomplete software could cause the printer to feed a blank page after each print job. Disconnect the USB cable from the printer, if necessary. In Windows, search for and open the Control Panel. Click Programs and Features.

Why is word adding a blank page when printing?

An Odd Page break causes the following text to start a new odd page. If the text before the break ends on an odd page, Word will insert a blank even page between the two odd pages. This page is completely invisible to the user (except in Print Preview with facing pages displayed) but will be “printed” by the printer.


1 Answers

Try doing a Measure, Arrange, and UpdateLayout right before the printvisual, like this:

DV.Measure(new System.Windows.Size(PD.PrintableAreaWidth,
              PD.PrintableAreaHeight));
DV.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0),
              DV.DesiredSize));

DV.UpdateLayout();
PD.PrintVisual(DV, "TEST");
like image 77
digiteknique Avatar answered Sep 19 '22 10:09

digiteknique