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();
}
}
}
}
}
InfoPath 2007 Form Hosted in WPF as a Wizard Sample
November 13, 2008
After looking around there are few sample out there using InfoPath in various ways. My objectives are as follows:
- Keep all the cool features of InfoPath like integration with SharePoint and use them as standard.
- Harness the design Capabilities
- Exploit the ability to use Xml for all data without having to do too much
- Deploy versioned forms without having to redeploy the app
- Use an offline sync provision should it be required
This sample does not cover all of this but what it does show is how it can be done.
Step 1 – Hosting the Form
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
