I've been looking for code snippets to be able to convert an Infopath view to Html and then use a converter service to create a PDf document. The form is published via InfoPath Forms Services so the export to PDF function is not available.
Inspired by the snippets I've found, I ended up with a nice piece of lean and mean code:
Hereby assuming that the name of the (current) view is equal to the name of the .xsl file in the XSN package.
How to convert Html to PDF:
Inspired by the snippets I've found, I ended up with a nice piece of lean and mean code:
public void CreatePDF_Clicked(object sender, ClickedEventArgs e)
{
// Create HTML.
try
{
string sourceFileName = null;
byte[] sourceFile = null;
XPathNavigator root = MainDataSource.CreateNavigator();
string myViewName = this.CurrentView.ViewInfo.Name;
string myViewXslFile = myViewName + ".xsl";
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(ExtractFromPackage(myViewXslFile));
// Generate a temporary HTML file
string fileName = Guid.NewGuid().ToString() + ".htm";
string filePath = Path.Combine(Path.GetTempPath(), fileName);
using (XmlWriter writer = XmlWriter.Create(filePath))
{
// Convert the XML to HTML
transform.Transform(root, writer);
// writer.Close();
}
// Specify the HTML to convert
sourceFile = File.ReadAllBytes(filePath);
//** etc...
}
catch
{
}
}
public XmlDocument ExtractFromPackage(string fileName)
{
try
{
XmlDocument doc = new XmlDocument();
using (Stream stream = Template.OpenFileFromPackage(fileName))
doc.Load(stream);
return doc;
}
catch (Exception ex)
{
throw new Exception(string.Format("Error extracting '{0}': {1}",
fileName, ex.Message), ex);
}
}
Hereby assuming that the name of the (current) view is equal to the name of the .xsl file in the XSN package.
How to convert Html to PDF:
- Programmatically Converting Web / HTML pages to PDF format
- Printing InfoPath 2007 Web-based Forms to PDF