How to force the dimensions of print out to 'overwrite' the printers default setting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    How to force the dimensions of print out to 'overwrite' the printers default setting

    Hello all,

    My application prints my windows form. When they click on the print button it does NOT give the customer the choice to select the printer or alter defaults. In this example, there is only 1 printer they can print too and it is hard coded into the code (we'll ignore the dangers of doing it this way for the moment ;) )

    I want to force the height and width of the print out to 'overwrite' the printers default settings.

    Code:
    PrintDocument pd = new PrintDocument();
    PaperSize ps = new PaperSize("Custom", 100, 100); 
     pd.PrintPage += new PrintPageEventHandler(printInfo);
     pd.PrinterSettings.PrinterName = selectedPrinter;
     pd.PrinterSettings.DefaultPageSettings.PaperSize = ps;
     //pd.PrinterSettings.DefaultPageSettings.PaperSize.Height = 100;
    // pd.PrinterSettings.DefaultPageSettings.PaperSize.Width = 100;
    
     lb.Text = pd.PrinterSettings.DefaultPageSettings.PaperSize.ToString();
      pd.print()
    Despite my code telling the printer what WxH to use, it seems to revert to the default.

    Any ideas?

    Dave
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You appear to be correct. Much of the default page settings seem to be ignored.

    For example:
    [code=c#]
    string selectedPrinter = "Adobe PDF";
    PaperSize ps = new PaperSize("Plat erCustom", 100, 100);

    PrintDocument pd = new PrintDocument() ;
    pd.DocumentName = "My Custom Document";
    pd.PrinterSetti ngs.PrinterName = selectedPrinter ;
    //pd.PrinterSetti ngs.PrintToFile = true;
    string cFilename = pd.PrinterSetti ngs.PrintFileNa me;
    if (pd.PrinterSett ings.IsValid)//if printer exists
    {
    pd.PrinterSetti ngs.PrintFileNa me = pd.DocumentName ;
    PrinterSettings .PaperSizeColle ction psc1 = pd.PrinterSetti ngs.PaperSizes;//allowed sizes
    PrinterResoluti on pr1 = pd.PrinterSetti ngs.DefaultPage Settings.Printe rResolution;//current resolution
    pd.PrinterSetti ngs.DefaultPage Settings.PaperS ize = ps;

    pd.PrinterSetti ngs.DefaultPage Settings.Margin s = new Margins(0, 0, 0, 0);//it seems to ignore this too?
    //pd.OriginAtMarg ins = true;//this shoves the "start" over by margin amount, but not the amount i just assigned??

    pd.PrintPage += new PrintPageEventH andler(pd_Print Page);
    pd.QueryPageSet tings += new QueryPageSettin gsEventHandler( pd_QueryPageSet tings);

    string pagesize = pd.PrinterSetti ngs.DefaultPage Settings.PaperS ize.ToString();//still good here
    pd.Print();
    }


    static void pd_QueryPageSet tings(object sender, QueryPageSettin gsEventArgs e)
    {
    PrintAction PA = e.PrintAction;
    PageSettings sPage = e.PageSettings;
    int bp = 1;//Settings are still correct at this time
    }

    static void pd_PrintPage(ob ject sender, PrintPageEventA rgs e)
    {
    Graphics g = e.Graphics;
    Rectangle rMargin = e.MarginBounds;
    Rectangle rPage = e.PageBounds;
    PageSettings sPage = e.PageSettings;
    int bp = 1;

    g.DrawRectangle (Pens.Yellow, sPage.Bounds);// This seems to be the papersize from the DefaultPageSett ings
    g.DrawRectangle (Pens.Black, rMargin);//gets drawn with a standard adjustment(100p ixels?)
    g.DrawRectangle (Pens.Blue, rPage);//is the absolute outer edge so have to zoom in to see it

    Rectangle rPageMinus1 = TransformRectan gle(rPage, -1);//= new Rectangle(rPage .X + 1, rPage.Y + 1, rPage.Width - 2, rPage.Height - 2);
    g.DrawRectangle (Pens.Red, rPageMinus1);//will be an outer page boarder of 1px
    }
    public static Rectangle TransformRectan gle(Rectangle r, int AdjustmentValue )
    {
    int a=AdjustmentVal ue;
    return new Rectangle(r.X - a, r.Y - a, r.Width + (2 * a), r.Height + (2 * a));
    }
    [/code]

    I see the same troubles, my page size gets set to 850x1100 which seems roughly 8.5' x 11' multiplied by 100, so 100pixels per inch resolution.
    And despite setting the margins to 0, they seem to have reverted back to 100pixels(or 1 inch)
    This is odd.
    The pd.PrinterSetti ngs.DefaultPage Settings.Printa bleArea property shows where the 850x1100 comes from, but it is readonly.
    Last edited by Plater; Sep 15 '10, 06:09 PM.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Ok. I think I figured out what is going on.

      When you assign the Custom size with 100x100, it flips through pd.PrinterSetti ngs.PaperSource s looking for a match.
      When it doesn't find one, it picks the first one it finds that can encompass the desired size (850x1100).
      It then sets all the printable area/bounds to THAT paper size, but in the PageSettings applies the correct 100x100 settings.
      I went to the printer's driver page through the windows dialogs and ADD my papersize to the list. You will need to do some math to get 100pixels since that is not a choice (inches, mm or points)

      Now I get 100x100 listed in the PrintableArea, but it still has the 850x1100 size listed in the PrintPage event.

      So I guess i'm really not any closer.


      Edit: At some point it just started working, I don't know if it was because i set my custom size as the "default" in the printing prefereces or because i started using a new filename or what, but now my page sizes are matching the custom size (139x139 which I guess means 100points=139pi xels)
      Last edited by Plater; Sep 15 '10, 06:48 PM.

      Comment

      • DaveRook
        New Member
        • Jul 2007
        • 147

        #4
        Plater,

        Thank you for looking into this with/for me. It is appreciated.

        In a strange way I'm glad it's difficult because it's always embarrasing to ask a question which has an obvious answer.

        The issue I have is I am printing over a terminal server. This means each time I log on, it creates a new session ID which creates a new instance of the printer and default settings. Therefore, I can't specify the paper sizes in the printer diag. box because next time the user logs on, it has a new session ID and therefore default settings. I need to find a way to tell the printer what settings to use regardless of what the printer has in its list of default printer names.

        Sadly, I have to find a way to do this so if I do resolve it, I will of course post my findings.

        Do you know if there is another way to do this? To recap, because we're printing over a terminal server (TS), we can't set up default printers as the server keeps forgetting the defaults. So, the program I'm writing must send the information to the printer and overwrite the printers defaults. Any ideas?

        Again, thank you for your help.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I did not popup the printing dialog in my program, i did it roughly through control panel.
          Even if the printer is located elsewhere, the drivers need to be installed locally (and the port becomes some type of networking port, instead of say LTP1) meaning that you can apply the "computer default" settings of adding that page.
          Or do you connect to this printer in some other fashion?
          If it appears as a choice in the PrintDocument, I am pretty sure it must have an entry in the Printers section?

          Comment

          • DaveRook
            New Member
            • Jul 2007
            • 147

            #6
            Hi

            Well, it does work for me now but only in Adobe PDF. As soon as I try it with a printer, it fails.

            I don't think is going to work over a terminal server, only over a local network. Looks like I'll have to create a VPN before my software will launch... joy!!!

            Thank you again for all your help

            Dave

            Comment

            Working...