1

INTRODUCTION AND RELEVANT INFORMATION:

I am maintaining an old application and I need to implement feature that generates reports based on data calculated from a database. User must also have an additional option of printing the file ( user must be able to choose printer, and must be able to see print preview ).

To improve my chances of getting satisfactory answer, allow me to additionally clarify matters with a small "example":

  • User presses a button -> application calculates data; // Done

  • Application creates file and populates it with the result; // Done

  • User gets informed that report is generated; // Done

  • User presses another button that is charged for printing; // Stuck here!

  • Print preview pops up and an option to run print dialog. // Stuck here

  • User starts the print dialog, chooses the printer and application prints the file;

The application is coded in C++ using raw WinAPI ( no MFC ). I am working on Windows XP.

PROBLEM:

I was able to successfully use OLE Automation to generate/save Word and Excel reports but I can not create print preview that matches exactly the print preview Word would create.

If I use OLE Automation to show Excel's print dialog/print preview, there is a following problem that might occur :

User can simply click "Close Print Preview", or can simply close print property sheet ( please see picture below ) and return to the document which might compromise document's data.

enter image description here

My employers do not like this ( they have no software engineering / developing background, so no matter what I say it will end up as an "echo in the wind"... ) and they wish that my print preview matches exactly the print preview Word generates.

Therefore, I need a solution for generating a print preview for the user, in a way that matches exactly print preview Word would create. Furthermore, user should be able only to see how this looks like, but not to be able to open the file from my application. User should be able to choose the printer that will print the file.

An implementation similar to the one from the picture would be fine.

QUESTION:

INTRODUCTORY NOTES:

The question is too broad to be answered in one post so I must limit myself only for seeking advice / general concept / pointing in the right direction.

THE ACTUAL QUESTIONS :

I did the best I could with the OLE Automation and Excel / Word files, but as you can see there are problems with generating a print preview, hence the following questions :

  1. Since this is my first time to tackle this kind of task, can you recommend me the proper way to handle it ( general concepts of course )?

  2. Is there a chance that there is a workaround for my printing problem ( like sending WM_PRINTCLIENT message to the Word / Excel or something like that )?

REMARKS :

Again, I realize the question is too broad so just give me general concepts / pointers, so I could post separate questions if I get stuck somewhere in the way.

I do not need to use Word / Excel and OLE Automation, it was my choice at the moment.

If you can recommend better solution I will gladly accept. I do not wish to use libraries.

If additional information is required, ask and I will edit my post.

AlwaysLearningNewStuff
  • 2,939
  • 3
  • 31
  • 84
  • have you considered making the document read only – Cheers and hth. - Alf May 31 '14 at 23:00
  • @Cheersandhth.-Alf: No I haven't, because I am tackling this for the first time. How would I populate the document if it is read-only? Or do you mean to make it read-only **after I populate it**? – AlwaysLearningNewStuff May 31 '14 at 23:03
  • I maent right after the create-file-and-populate-it. – Cheers and hth. - Alf May 31 '14 at 23:05
  • @Cheersandhth.-Alf: My pardon for previous comment, I misunderstood your comment. But even in that case user can return to the document by clicking "Close Print Preview" which is also dangerous... – AlwaysLearningNewStuff May 31 '14 at 23:15
  • "Application creates file and populates it with the result" -- does the file contain the report in plain text? RTF? or data (CSV, tab-delimited text, ...)? Would you be willing to paint the complete report yourself? – Edward Clements Jun 01 '14 at 08:02
  • @EdwardClements: `does the file contain the report in plain text? ` It does in most part ( logo bitmap should be in the header of the document ). `RTF? or data (CSV, tab-delimited text, ...)?` Data should be represented in tables, so I guess *RTF* would be the way to go. `Would you be willing to paint the complete report yourself?` Yes, of course! – AlwaysLearningNewStuff Jun 01 '14 at 11:19
  • Can you please clarify in your question why it is a problem that the user can return to the document once the print dialog is opened? Why is that dangerous, but it's not dangerous for them to have the document *before* attempting the print? It seems to me that once the file is generated, the user can do whatever he likes with the file regardless. – JBentley Jun 03 '14 at 02:05
  • @JBentley: My employers simply do not "like it". They are not software developers, nor they have a clue about programming ( we can not choose our bosses sometimes... ). They wish to see the print preview, choose the printer that will do the printing, and also have the content saved in the file **exactly the way it looks in the window that will do the printing.** I hope this makes things clear. If not please ask for further clarifications ( reconsider the downvote please... ). – AlwaysLearningNewStuff Jun 03 '14 at 02:52

1 Answers1

1

You would need to bring up a dialog box when the user clicks the print button, the dialog box would contain a control that shows the preview for the default printer and three buttons to select another printer, print and exit.

The preview control could be a static control where the WM_PAINT handler would draw the preview (alternatives: a Web Browser control which is fed html text or a RichText control which is fed RTF text, but then you are constrained by the functionality of these controls).

Quote: Print preview isn't that special. It just means that you have to render to screen (or bitmap) what you'd otherwise would render to the printer DC. This primarly means using the page size, and providing a UI control for the prev/next page.

This SO Answer describes in detail the work you need to do to paint the preview.

This CodeProject article has some simple code for print-preview; it's in simple MFC which should not be too difficult to translate to plain C++/WinAPI.

Community
  • 1
  • 1
Edward Clements
  • 5,040
  • 2
  • 21
  • 27
  • If I paint the report myself, how will I save its content to a file? I can not use GDI / GDI+ to draw inside Excel / Word / PDF / or any other file format that I know of... That is my problem, the content of the file and the content of the print preview **must be the same.** I have already checked the submitted articles/questions before posting this question, but thank you anyway. – AlwaysLearningNewStuff Jun 02 '14 at 11:41
  • If the contents of the file need to match the preview, the options I can think of are RTF, HTML or PDF (not necessarily in that order) and embed a corresponding viewer control in your preview dialog box – Edward Clements Jun 02 '14 at 12:41
  • Well PDF requires a fee for using their SDK as far as I know, and I do not wish to use libraries either, so RTF or HTML are the way to go... Still, I do not know how to implement your suggestion ( embedding the viewer control + automating it ), but I am willing to learn. I will try to find tutorials on my own so I do not bother you, and will ask for your help if I fail. Thank you for your help. We shall "stay in touch". – AlwaysLearningNewStuff Jun 02 '14 at 15:21
  • I have decided to try to do my own print preview and printing, and will probably output the report int o RTF or HTML. I haven't found a way to embed viewer for Word into my window's client area ( can you recommend me one? ). Still, +1 from me and I have awarded you the bounty. Thank you for your help. Best regards until next time. – AlwaysLearningNewStuff Jun 08 '14 at 12:39
  • Thanks for the bounty! I too was searching for a Word Viewer ActiveX control, but it seems that only older versions of Office used to support OLE Embedding. It looks like the best way forward is to embed an invisible RichEdit control, load the RTF text and use it to print to your preview DC like [here](http://msdn.microsoft.com/en-us/library/windows/desktop/bb787875(v=vs.85).aspx), the links in my answer may also help. – Edward Clements Jun 09 '14 at 07:17
  • I do not know how to "draw" table inside the RichEdit control, and I do not know if bitmap/EMF can be put inside RichEdit as well. My primary concern is text inside a table. Thank you for your help, I have decided to try and do print/print preview on my own but [got a few problems](http://stackoverflow.com/questions/24098435/expected-and-actual-printing-results-do-not-match). Perhaps you cam help? Best regards. – AlwaysLearningNewStuff Jun 09 '14 at 12:32
  • I thought you were going to load the RTF text into the RichEdit Control? [this](http://msdn.microsoft.com/en-us/library/aa140283(v=office.10).aspx#rtfspec_tabledef) is the reference for RTF-table -- maybe another way would be to create a similar looking table in Word, save as RTF and check the file contents... – Edward Clements Jun 09 '14 at 15:37