Export InfoPath-form to pdf-file with individual name - InfoPath Dev
in

InfoPath Dev

Export InfoPath-form to pdf-file with individual name

Last post 05-31-2016 01:57 PM by John1006. 38 replies.
Page 1 of 3 (39 items) 1 2 3 Next >
Sort Posts: Previous Next
  • 08-08-2008 01:16 AM

    Export InfoPath-form to pdf-file with individual name

    Hello,

    I have a problem to export an InfoPath-form to pdf

    If the form is filled out, I want to make a pdf-file of it with an onclick-butten-event.
    here is the code that I use:

    Sub print_OnClick(eventObj)
     Me.CurrentView.Export("C:\MyView.pdf", ExportFormat.Pdf) //VB-Code
     'XDocument.View.Export("C:\\MyView.pdf", "PDF");  
     'this.CurrentView.Export(@"C:\MyView.pdf", ExportFormat.Pdf); //C#-Code
    End Sub

    If I implement this code, I can't open the form. It throws an exeption, that the form can't be opened.
    I tested all lines of code, but no one works.

    I also want to give the pdf-file a name like name_ID.pdf
    Is it possible to do this with the following code:
    var name = XDocument.DOM.selctSingleNode("//my:name").text;  //VB-Code
    var ID = XDocument.DOM.selectSingleNode("//my:ID").text;  
    XDocument.View.Export("c:\\name +"_" + ID + ".pdf", "PDF");
    Is it possible to do this all in JScript?
    export the form with an individual pdf-file-name to the pdf-format programmatically?


    - Form is fully trusted
    - I have the rights do save data at C:\

    The form is built up in Infopath 2007
    I also installed the add-in "export to pdf or xps"

    Hope someone can help me.

    Best regards,
    Matthias

  • 08-09-2008 09:44 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Hi there!

    Wow, you have a lot of different languages going on. To start with, it looks as if you are using VB.NET, but did you know that the double slashes are a comment mark for C#? So your line:

    Me.CurrentView.Export("C:\MyView.pdf", ExportFormat.Pdf) //VB-Code

    won't compile, because of the "//VB-Code" at the end of it -- the compiler doesn't know what to do! And later, it looks like you have a jscript sample, but for some reason it also has the "//VB-Code" comment in it. Fortunately, "//" is a comment mark in jscript, but if you try to mix jscript with your VB.NET, you will continue to have issues.

    First, let's talk about how to find out what language your form is using and how to reset it if necessary. For IP 2007, go to Tools/Form Options/Programming. There you will be able to at least see what language the form is set to. If you want to change it, click the Remove Code button. Keep in mind the buton does what it says -- it will remove all existing code. Also, you say you are using IP 2007, but the OnClick event belongs to the IP 2003 object model. You may want to remove your code and select either Visual Basic or C# for your language, as the samples I'm about to give you use the 2007 object model.

    You'll need to add an event handler for your button after removing your code. Right click on your button and select Button Properties. Be sure the Action is set to Rules and Custom Code. Give your button a meaningful ID and select Edit Form Code. This will add an event handler for your button. In the following samples, my button's ID is Print. The first line of your code will vary according to your button ID.

    Now to what you want to do. You are correct that the form will have to be full trust and that you need the add in. Here is a VB.NET sample that exports to pdf and uses the name of a field for the file name:

    Public Sub Print_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)      'Declare our variable
         Dim fileName As String
         Dim nameNode As XPathNavigator

         'Set the values. Replace "/my:myFields/my:textField" with the xpath to the field that has the value you want as the file name
         nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager)
         fileName = nameNode.Value +
    ".pdf"

         'Do the work -- notice we use the variable fileName as part of the path for the file
         Me.CurrentView.Export("C:\" + fileName, ExportFormat.Pdf)

    End Sub

    And here is C#:

    public void Print_Clicked(object sender, ClickedEventArgs e)

    {

         //Declare our variables
        
    string fileName;
        
    XPathNavigator nameNode;

         //Set the values. Replace "/my:myFields/my:textField" with the xpath to the field that has the value you want as the file name
        
    nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager);
         fileName = nameNode.Value +
    ".pdf";

         //Do the work -- notice we use the variable fileName as part of the path for the file.
        
    this.CurrentView.Export(@"C:\" + fileName, ExportFormat.Pdf);

    }

    I'll upload the samples I did to our "Basic Stuff" section in the downloads -- you'll be able to find them here if you need them, okay? Let me know if you need more help.

    Hilary Stoupa

  • 08-11-2008 01:18 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Hello Mrs. Stoupa,

    thanks for your feedback.

    I tested the code only one by one because the languages don't go together.
    So I put the "//VB-Code" comment there as I wrote this post.

    Thanks for the information about how to change languages and remove code.
    The problem that I have is, that I haven't installed Visual Studio and so I only can code in JScript or VBScript. I thought it is possible to implement the VB.net code to VBScript.

    Every properties and conditions are like you said.
    Now I installed the VSTA and also the VS 2005. And I also downloaded your example but if I want to have a look at the code (right-click on the button, edit form code) I get the error:

    The event handler can't be added.
    The system can't find the indicated file.

    I also get this error if I open my form and want to write some code with doing the right-click on the button -> edit form code. And also if I do this over the menu

    What's the misstake, what do I wrong?

    The Add-in "Save to PDF" is also installed. I have installed MS Office 2003 and InfoPath 2007. In InfoPath I added the VSTA. So I think there is a problem with the VSTA. But I don't know what to do now.

    Can you help me, please?

  • 08-11-2008 05:41 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Hello, now most things are working. The VSTA now works fine and I can edit the code, but I got o lot errors.

    The errors exists at the underlined words:

    Imports Microsoft.Office.InfoPath

    Imports System

    Imports System.Windows.Forms

    Imports System.Xml

    Imports System.Xml.XPath

    Imports mshtml

     

    Private Sub InternalStartup(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Startup AddHandler DirectCast(EventManager.ControlEvents("Print"), ButtonEvent).Clicked, AddressOf Print_Clicked

    End Sub

    Public Sub Print_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)

    'Declare our variables

    Dim fileName As String

    Dim nameNode As XPathNavigator

    'Set the values. Replace "/my:myFields/my:textField" with the xpath to the field that has the value you want as the file name

    nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:id_form", NamespaceManager)

    fileName = nameNode.Value + ".pdf"

    'Do the work -- notice we use the variable fileName as part of the path for the file.

    Me.CurrentView.Export("C:\" + fileName, ExportFormat.Pdf)

    End Sub

    I don't know how to fix these errors.I imported other namespaces but the problems already exist.
  • 08-11-2008 07:06 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    What are the errors? Make sure you have your form's programming language set to use Visual Basic and not VB for InfoPath 2003.

    Hilary Stoupa

  • 08-11-2008 07:40 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Thanks for this information. I set the programming language to VB for InfoPath 2003. :)

    Now everything works! Thanks a lot!

     

    I have now another question:  Is it possible to do an pdf-Export over more views that are implemented in the infopath-form? It will be great if it is possible to do this outside from infopath, so that I can fix all views of one form into one pdf-file. Example: I have a form with view A, B and C. So I want to get one pdf-file with view A, B and C (exactly in this order)

  • 08-11-2008 07:50 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Here is a link to the MSDN documentation on the SwitchView method. I would create a single view that has all three views in it (you can copy/paste the content of each view into a new view) and then when your user clicks the button, try switching the view, exporting the new view to pdf and switching back.

    There may be a way to append each view to a single PDF, but I'm not seeing anything in the documentation on the Export method about that....

    Hilary Stoupa

  • 08-11-2008 07:58 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Thanks a lot for your very fast help!

    I'm happy that everything works. Thanks for the information with the SwitchView method. I'll have a look at it.

    Regards,

    Matthias

  • 09-09-2008 07:19 PM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Hello..

    I am recieving an error in the code. 

    The error occurs in this piiece of code: fileName = nameNode.Value + ".pdf"

    The error is NullReferenceException was unhandled by user code

    Object reference not set to use an instance of an object

    Am also getting trhis error:

    System.NullReferenceException
    Object reference not set to an instance of an object.
       at SelfAssessmentForm.FormCode.Print_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    I am not 100% sure i what needs to be added after the "/my: ......." so also could you tell me if what i added was right?  what i added SelfAssesmentForm is the name of my form, and LastName is the name of the field i wan the form to be saved as.

    I will post my codeing below just to show you

    Public Sub Print_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)

    Dim fileName As String

    Dim nameNode As XPathNavigator

    'Set the values. Replace "/my:myFields/my:textField" with the xpath to the field that has the value you want as the file name

    nameNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:SelfAssessmentForm/my:LastName", NamespaceManager)

    fileName = nameNode.Value + ".pdf"

    'Do the work -- notice we use the variable fileName as part of the path for the file

    Me.CurrentView.Export("C:\forms" + fileName, ExportFormat.Pdf)

    End Sub

     

     

     

  • 09-09-2008 07:37 PM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    My best guess would be that the xpath you've put in the line that starts with "nameNode = " is incorrect and that you are getting a null reference because the xpath "my:SelfAssessmentForm/my:LastName") doesn't return a node. In InfoPath 2007, you can right click on a node in the data source task pane (in your case, your LastName node) and select Copy XPath. Do that, and then paste the xpath into the quotes where you currently have "my:SelfAssessmentForm/my:LastName".

    Hilary Stoupa

  • 09-09-2008 07:43 PM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    thanks for that.

    I now get an error;

    SecurityException was unhandled by user code

    Request for permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089

     

  • 09-23-2008 10:49 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    Do you have any jscript example of this code to export the Infopath data to a PDF...

    SAM

  • 09-23-2008 10:57 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    I'm sorry, I don't have a jscript version. As to the issue above, your form will need to be full trust to use this code.

    Hilary Stoupa

  • 07-29-2009 08:02 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    This is an excellent write up. Does anyone know if it's possible to write the converted document (XPS, PDF, MHT) directly to SharePoint?

  • 10-19-2010 08:55 AM In reply to

    Re: Export InfoPath-form to pdf-file with individual name

    I have copied and pasted your C# code and am getting the following errors - can you assist me please?

    Error 1 'Microsoft.Office.InfoPath.View' does not contain a definition for 'Export' 

    Error 2 The name 'ExportFormat' does not exist in the current context 

     

Page 1 of 3 (39 items) 1 2 3 Next >
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.