Publish InfoPath 2007 Form as PDF Programmatically C#

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();

}

}

}

}

}

Comments

Got something to say?