CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    4

    How to bypass the print dialogue programmatically?

    I'm trying to write a program to print documents(txt, doc,pdf,html and url) in the batch mode. Actually I'm using process.start method to launch shell process which works fine except that the print configure dialogue box always appeared before the html was printed. Is there any way to bypass the dialogue so that no any user's intervention is needed?

    Any help will be appreciated.



    Leo

  2. #2
    Join Date
    Oct 1999
    Location
    Germany
    Posts
    121

    Re: How to bypass the print dialogue programmatically?

    Hi,


    the main purpose of the PrintDialog is to get the DevNames and DevMode-members. If these values are available, you can create a device-context based on these values yourself.

    I never did it in c#, but i can explain this like i did it in C++, so you may get an idea about the approach:

    you need to get the m_hDevMode and m_hDevNames for the partcicular printer.
    I my programm, i have a configuration (property-sheet) where i can setup the properties of the desired printer . After selecting it, i retrieved this members from the PrinterContext of the selected printer and stored them for further use in registry.

    When printing programatically, i read these values from registry, created a Printer-Device-context and copied these members i've read from registry to the members of the newly created device-context.

    Then i output graphics and text to the device-context.

    ....


    unsigned char uca_printstruct_Mode[10000];
    unsigned char uca_printstruct_Names[10000];

    unsigned int ui_Mode_size;
    unsigned int ui_Names_size;

    CPrintDialog cpd(TRUE,PD_ALLPAGES,CWnd::GetDesktopWindow());

    m_hDevMode = GlobalAlloc(GHND, ui_Mode_size);
    BYTE* lpCopy = (BYTE*)GlobalLock(m_hDevMode);
    memcpy(lpCopy,uca_printstruct_Mode,ui_Mode_size);
    GlobalUnlock(m_hDevMode);

    m_hDevNames = GlobalAlloc(GHND, ui_Names_size);
    lpCopy = (BYTE*)GlobalLock(m_hDevNames);
    memcpy(lpCopy,uca_printstruct_Names,ui_Names_size);
    GlobalUnlock(m_hDevNames);

    int xx=0;

    cpd.m_pd.hDevMode=m_hDevMode;
    cpd.m_pd.hDevNames=m_hDevNames;

    hdc=cpd.CreatePrinterDC();

    dc.Attach(hdc);
    pDC=&dc;
    ....


    pDC->StartPage();
    // Do some writing to the device-context using graphics-funktions
    // (TextOut..., LieTo etc.)
    pDC->EndPage();

    pDC->EndDoc();


    dc.Detach();
    DeleteDC(hDC);


    ( Some code listed are taken from C++-Forum, so i will not claim, that ALL of the above code is developed myself.)

    Hope this helps.
    Jost

  3. #3
    Join Date
    Jul 2005
    Posts
    4

    Re: How to bypass the print dialogue programmatically?

    Thanks for your reply, even though it doesn't really help in my case.

    What I'm trying to do is programmatically printing many different files without user's intervention. These files could be PDFs, HTMLs, Text, .doc as long as they are printable and accessible, and they could be mixed in a list provided. Since I didn't want to develop specific program to deal with different files, I decided to launch seperated processes using Process.start() method in c#.net to let each different application to handle corresponding file, like using Adobe Reader to print pdf, using Word to print DOC and so on. But my problem is when the html is needed to be printed, the print dialogue box always appears and user has to press "ok" button to proceed, which is not anticipated.

    I guess there must be some ways to disable or bypass the print dialogue, either through application configuration or in the code, actually somebody suggested using batch command to interact with windows and workaround this problem, which I don't know how to do it.

    Another thing is I'm not sure what I'm doing is a good way or not to do the batch printing, if anybody has any idea and let me know, that'd be very appreciated.

    Thanks again. I really appreciate your reply.

    Leo

  4. #4
    Join Date
    Oct 1999
    Location
    Germany
    Posts
    121

    Re: How to bypass the print dialogue programmatically?

    Hi ,

    sorry, i completely misunderstood you.

    Printing different files in a batch is a different story.

    You can use DDE to print files. For example, if you right-click on a Word-Document and selecting "Print", WinWord starts and prints the complete document to the default printer without any userinvention.

    To make NOTEPAD to print a document, just run the command "notepad /p x.txt" (ok, x.txt should be a existing file....)


    To get an idea on how the parameters work on Word, you may have a look in Explorer/Extra/Folderoptions/Filetypes.
    Look for the document-typs you need and look on the DDE-Parameters below "PRINT".

    Hope this is more helpfull than the previous post.

    Regards
    Jost

  5. #5
    Join Date
    Jul 2005
    Posts
    4

    Re: How to bypass the print dialogue programmatically?

    Hi Jost,

    Thanks.

    I followed your instruction and tried to print Word document, which worked as I expected - no print dialogue appears and no user's intervention needed. However, when I tried printing HTML in the same way, it didn't work - print dialogue still appearred before printing. I checked the file association, the "print" action was

    "C:\Program Files\Microsoft Office2000\Office\msohtmed.exe" /p %1

    Is there any way I can configure or add parameter to this application (msohtmed.exe) so that the print dialogue can be bypassed?

    Actually what I'm doing in my program is exactly same as what you said - calling DDE application associated to each file and print. For now this program works with almost all kinds of files but HTML. I guess there might be some parameters that can prevent print dialogue from appearing.


    Regards

    Leo

  6. #6
    Join Date
    Oct 1999
    Location
    Germany
    Posts
    121

    Unhappy Re: How to bypass the print dialogue programmatically?

    Huuuh ..... no idea.

    I played around but like you realized, the print-box pops up all the time.

    At the moment,... no further ideas .....

    Good luck !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured