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

C# Timer Sample – Simple

November 24, 2008

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Timers;

 

namespace WindowsTimer

{


class
Program

{


static
void Main(string[] args)

{


Timer timer = new
Timer();

timer.Elapsed += new
ElapsedEventHandler(timer_Elapsed);

timer.Interval = 1000;

timer.Start();

}

 


static
void timer_Elapsed(object sender, ElapsedEventArgs e)

{


Console.WriteLine(DateTime.Now);

 

}

}

}

Retrieving COM class factory failed due to the following error: 80040154

November 24, 2008

Vista x64 dev machine trying to do some COM interop for a RFID project. Annoying error but found the answer in the end…

WORKAROUND:

The possible workaround is modify your project’s platform from ‘Any CPU’ to ‘X86′ (in Project’s Properties, Build/Platform’s Target)

ROOTCAUSE

The VSS Interop is a managed assembly using 32-bit Framework and the dll contains a 32-bit COM object. If you run this COM dll in 64 bit environment, you will get the error message.

http://forums.asp.net/t/1119052.aspx

 

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

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

 

Debugging SharePoint

November 10, 2008

User friendly error pages are not what we need when trying to debug something…

Modify your web.config as follows, and all will be well.

< ?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>

< configuration>


< configSections>



configSections>


< SharePoint>


< SafeMode MaxControls=”200″ CallStack=”true”

DirectFileDependencies=”10″ TotalFileDependencies=”50″

AllowPageLevelTrace=”false”>


< PageParserPaths>


PageParserPaths>


SafeMode>


CallStack=”true” being the important setting here.


 

Then further on down the file:

< customErrors mode=”Off”
/>

Visual Studio 2010 and .NET Framework 4.0 CTP Download

November 9, 2008

Find the Virtual PC download of the CTP at the following link : https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=9790

Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0

July 26, 2007

Currently a Program Manager on assignment at MCS Reading we created a solution using Web Parts on Windows SharePoint Services v3. The assumption is that the platform would provide us with a good starting point. However it was easier

Next Page »