Publish InfoPath 2007 Form as PDF Programmatically C#

December 19, 2008

Simple way to publish a Infopath Form to PDF using C# and VSTA

Clients require the following download:

namespace Template_Test

{


///


///


///


public
partial
class
FormCode

{


///


/// Internal Startup


///


public
void InternalStartup()

{

EventManager.FormEvents.Submit += new
SubmitEventHandler(FormEvents_Submit);

}


///


/// Form Submit Event


///


///


///


public
void FormEvents_Submit(object sender, SubmitEventArgs e)

{


//SUGGEST WE FIND A SAFE AND PERSONAL PLACE TO STORE THE FILE, NO MUCKING WITH PERMISSIONS


string fileName = string.Format(@”{0}\{1}.pdf”, System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), “TestFile”);


//CHANGE THE VIEW TO THE REPORT VIEW (IF SUCH A THING IS DEFINED)


this.Application.ActiveWindow.XmlForm.ViewInfos.SwitchView(“Public”);


//EXPORT THE FILE TO PDF


this.CurrentView.Export(fileName, ExportFormat.Pdf);


//CONVERT THE FILE TO BINARY READING IT FROM THE APPLICATION DATA FOLDER


string base64 = ConvertPDFtoBinary(fileName);


//STORE THE FILE BLOB IN THE CURRENT XML DOCUMENT

StoreFileData(base64);


//TEST METHOD TO PERSIST THE FILE FROM BASE64

ConvertBinaryToPDF();

}


///


/// Store the File Data in the current xml structure as base64


///


///
string containing base64 data


public
void StoreFileData(string base64)

{


XPathNavigator navigator = this.MainDataSource.CreateNavigator();


XPathNavigator field = navigator.SelectSingleNode(“//my:assetTracking/my:fileStorage”, this.NamespaceManager);


//CHECK IF “nil” ATTRIBUTE EXISTS ON THIS NODE

DeleteNil(field);

field.SetValue(base64);

}


///


/// Check if the “nil” attribute exists on this node


///


///
Node to check


public
void DeleteNil(XPathNavigator node)

{


if (node.MoveToAttribute(“nil”, “http://www.w3.org/2001/XMLSchema-instance”))

node.DeleteSelf();

}


///


/// Convert PDF file to Binary string base64


///


///
Name of the file


///


public
string ConvertPDFtoBinary(string fileName)

{

System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);


byte[] buffer = new
Byte[fileInfo.Length];


using (System.IO.FileStream fileStream = fileInfo.OpenRead())

{

fileStream.Read(buffer, 0, buffer.Length);

}


return System.Convert.ToBase64String(buffer);

}


///


/// Convert Binary base64 string back to a file and save in application data


///


///


/// This is simply a test method to prove the concept


///


public
void ConvertBinaryToPDF()

{


XPathNavigator navigator = this.MainDataSource.CreateNavigator();


XPathNavigator field = navigator.SelectSingleNode(“//my:something/my:fileStorage”, this.NamespaceManager);


string base64 = (field.Value as
string);


if (base64 is
string)

{


byte[] buffer = System.Convert.FromBase64String(base64);


using (FileStream fileStream = new
FileStream(string.Format(@”{0}\{1}.pdf”, System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), “TestFileFromBinary”), FileMode.CreateNew))

{

fileStream.Write(buffer, 0, buffer.Length);

fileStream.Close();

}

}

}

}

}

Excel Services Access Denied – Single Server

December 18, 2008

Accessing Special Folders C#

November 13, 2008

How to create and read files for various purposes to “special” folders   

 

     ///


///


///


///


///


private
void Save_Click(object sender, RoutedEventArgs e)

{

Microsoft.Office.InfoPath.FormControl infoPath = windowsFormsHost.Child as Microsoft.Office.InfoPath.FormControl;

 


string folderName = string.Format(@”{0}\{1}.xml”, Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), Guid.NewGuid().ToString());

infoPath.XmlForm.SaveAs(folderName);

 

}

Creating Wizards in InfoPath

November 13, 2008

Here is a good post to get basic functionality working using Wizard-like pattern in InfoPath.

http://blogs.msdn.com/infopath/archive/2006/04/20/wizards-and-we-re-not-talking-harry-potter.aspx

 

Windows SharePoint Services Document: Application Templates for Windows SharePoint Services 3.0 – Under the Hood

July 27, 2007

As per my previous post, there were things MS managed to do , but form the products was not immediately evident how it was done. This white

Smart Tags Communicating with the Custom Task Pane using Windows Communications Foundation (WCF)

March 20, 2007