Printing a view via code - InfoPath Dev
in

InfoPath Dev

Printing a view via code

Last post 05-12-2012 10:59 PM by swamy. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 04-19-2006 07:33 AM

    Printing a view via code

    Is it possible to print a view within code without actually switching to that view? I would like to have print buttons for several views along the bottom of my form, and would like the user to be able to click and print a view from anywhere. I've tried switching views and the printing, but I get permission denied errors. How can this be accomplished - can't I just issue a printout command and specify the view I want printed? If you must switch views, what is the correct way this should be coded to avoid permission denied and view not ready errors?

    Ryan
  • 04-19-2006 09:36 AM In reply to

    Re: Printing a view via code

    From the Views task pane, double-click the View and switch to the Print Settings tab. From here you can set another view as the Print view. When you press print (or issue a print command via code) while on the original view InfoPath will print the view that has been designated as the Print view.

    As for printing via code - the reason you are getting permission denied error is because the XDocument.PrintOut() method requires full trust.

    Hope this helps!
    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 04-19-2006 10:25 AM In reply to

    Re: Printing a view via code

    Got it, thanks!

    I guess what I was more interested in is printing any view I want from any other given view with the click of a button, without any regard to what the default print view happens to be on that page that I am "clicking" from. I want to be able to have a main "control panel" of buttons, where users can click buttons to print view A, or view B, etc...

    What I can't seem to get how to do is, to print a view via code that is not specified as the default print view from the page I am on. I understand I can set the default print view, but I already have a default print view for that page. I really need to know how to either print whatever view I want via code, or set the default print view via code. I guess either would work.

    Thanks,
    Ryan
  • 04-19-2006 09:29 PM In reply to

    Re: Printing a view via code

    Hi Ryan,
    How about adding rules to your control panel buttons to switch the view and then print. The print will have to be done in code and, as Matt states, will require FT, but it's doable.
    Patrick Halstead
    Project Manager at Qdabra
  • 04-20-2006 07:00 AM In reply to

    Re: Printing a view via code

    Patrick,
    That would solve part of my problem. So I would:

    1)assign a button rule to switch the view
    2)code the PrintOut() function to the OnClick event of the button
    3)this is where I am confused - how do I switch back to the view I started from, without getting the dreaded view not ready?? Is there an event that I could tie into to issue the switchview command to switch back? what event triggers when a view is switched, so I could say effectively "when you switch to this (print) view, print it out and switch back to the view you just came from? How could I accomplish this?

    Also, if I want to print a view that is not assigned anywhere as a default print view, how would I access it to print it? I have a view which is designed to be printed out, but it is not assigned to any particular page (it's actually a master report of all views combined), so the only way I can figure to get to it and print it, is to switch to it as a view, print it, then switch back. Then I run into the whole view is not ready problem. Printing a view that is defined as the default print view on any given page is easy enough, but I need to know how to also print views that are not linked defined as print views to any particular view. I'd like to have a page that has a panel of buttons that say "Print View A", "Print View B", etc. None of those views I want to print would be assigned anywhere as a "default print view", so I need to either print them directly, or somehow temporarily make the view in question the default print view for the page I am on while printing, so when I issue the print out command, it prints the right view.

    Thanks guys,
    Ryan
  • 04-20-2006 07:10 AM In reply to

    Re: Printing a view via code

    Ryan,
    Unfortunately,I dont think what u exactly want is possible.
    U need to do as what Patrick said.
    U can have buttons namely "Print View A" and "Print View B".
    On clicking "Print View A",switch view to "View A".
    Have a button in "View A" to Print.

    Similarly for the other views.

    Regards,
    Abhishek.
  • 04-20-2006 03:33 PM In reply to

    Re: Printing a view via code

    Patrick,
    I've got the printing working as you described, but I still can't figure out how to get it to switch back the view to the default view automatically (via code) after printing. Currently as a workaround, I have a button on the print view that says "Finished printing", but it really is a stopgap - seems very "kludgey". There has got to be a way to return to the default view by way of code. For something seemingly so simple in a program that is very powerful, seems awful complicated.

    I tried creating a field and changing the value upon the click of the print button, and then tried hooking into the OnAfterChange event of that field to switch back to the default view - but I still keep getting the "view not ready" errors, even when triggering it from OnAfterChange. This suprised me because I thought you only got those if you tried to switch views twice in the same procedure, which I am not doing (at least to my knowledge).

    So if the view isn't ready, how do I get it ready??? There has GOT to be a way to do this.

    Ryan
  • 08-09-2006 07:43 AM In reply to

    Re: Printing a view via code

    Try putting the switch view code after the printout code. It will switch back to the original view.
  • 08-09-2006 09:30 AM In reply to

    Re: Printing a view via code

    Unfortunately InfoPath locks the view switching during this operation. We ran into a similar view switch issue on another project and had to use a tricky workaround. We set an onContextChange handler to pickup when the cursor was in the first field int he view and then did the switch back. In your case this would prevent the user from ever actually going to the second view. What you would have to do is write code to special case the onContextChange to only switch back if you were in a print state mode. Say by setting a boolean flag or the value of a hidden DOM node to a specific value during the printing scenario.
    Shiraz Cupala / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 05-25-2007 01:23 PM In reply to

    Re: Printing a view via code

    Brilliant! Thanks for sharing this, Shiraz. It works like a champ.
  • 06-22-2010 12:58 PM In reply to

    Re: Printing a view via code

    In case anyone needs the code...

    Public Sub FormEvents_ContextChanged(ByVal sender As Object, ByVal e As ContextChangedEventArgs)

    If (e.ChangeType = "ContextNode") Then

        'If the data field ends up on the PrintView's first field, then switch back via code to SelectExistingRecord view

        Select Case e.Context.Name

        Case "dppd" 'this is the field that gets the focus on the print view

            Me.ViewInfos.SwitchView("SelectExistingRecord")  'switch back to the view that called the Print

        End Select

        Return

    End If

    End Sub

  • 08-09-2010 04:00 AM In reply to

    Re: Printing a view via code

    Hi, i try to do same as you, but ı am extremlly new about InfoPath, and could you tell me please, where did you write this "code the PrintOut() function to the OnClick event of the button" i don t know, how can i reach Code behind of the button on the form or list in InfoPath????????Could you explain step by step on InfoPath2010. Thank you :)
  • 05-12-2012 10:59 PM In reply to

    • swamy
    • Not Ranked
      Male
    • Joined on 05-12-2012
    • Bangalore
    • Posts 9

    Re: Printing a view via code

    HI Shiraz,

    I have a simillar requirement "There is one InfoPath form with 4 views, One button on main default view, When user clicks on the this button, need to export all other views to PDF file.

    Tried using below code

    public void CTRL2_6_Clicked(object sender, ClickedEventArgs e)

    {

              ViewInfoCollection viewInfos = ViewInfos;

              foreach (ViewInfo thisView in viewInfos)

             {

                 this.ViewInfos.SwitchView(thisView);

                 this.CurrentView.Export(@"d:\this"+this.CurrentView.ViewInfo.Name+".pdf", ExportFormat.Pdf);

             } 

    }

    But , I am getting error "InfoPath cannot execute any view-related object model calls. The view is not ready".

    Please let me know how to achieve this?

     

Page 1 of 1 (13 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.