Hi,
I have a write code-behind in infopath form to convert word document using openXML
Below code i have used to convert:
I underlined the code which is getting error!!!
using Microsoft.Office.InfoPath;using System;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace SampleForm
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft InfoPath.
// It can be modified using Microsoft InfoPath.
private TableCell createCell(string content)
{
// Create a new table cell
TableCell tc = new TableCell();
// Specify the width of the table cell
TableCellWidth tcw = new TableCellWidth();
tcw.Width = 2400;
tcw.Type = TableWidthUnitValues.Dxa;
tc.Append(new TableCellProperties(tcw));
// Specify the content of the table cell
tc.Append(new Paragraph(new Run(new Text(content))));
// Return the new table cell
return tc;
}
private TableRow createRow(string cell1, string cell2, string cell3)
{
// Create a new table row
TableRow tr = new TableRow();
// Add the table cells to the table row
tr.Append(createCell(cell1));
tr.Append(createCell(cell2));
tr.Append(createCell(cell3));
// Return the new table row
return tr;
}
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["CTRL6_5"]).Clicked += new ClickedEventHandler(CTRL6_5_Clicked);
}
public void CTRL6_5_Clicked(object sender, ClickedEventArgs e)
{
// Write your code here.
// Get a reference to the main data source
XPathNavigator root = MainDataSource.CreateNavigator();
// Copy the template and create a new document
string newFilePath = @"C:\NewDoc.docx";
File.Copy(@"C:\QuickQuote.docx", newFilePath, true);
using (WordprocessingDocument myDoc =
WordprocessingDocument.Open(newFilePath, true))
{
// Add an aFChunk part to the package
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart
.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Xhtml, altChunkId);
// Retrieve the rich text field contents
// and store it into the aFChunk part
StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.Append(root.SelectSingleNode(
"//my:RtxtField", NamespaceManager).InnerXml);
sb.Append("</html>");
string html = sb.ToString();
using (MemoryStream ms =
new MemoryStream(Encoding.UTF8.GetBytes(html)))
{
chunk.FeedData(ms);
}
// Add the aFChunk to the document
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.Append(altChunk);
// Create an empty table and specify formatting for its borders
Table table = new Table();
TableBorders borders = new TableBorders();
TopBorder tb = new TopBorder();
tb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Dashed);
tb.Size = 24;
borders.AppendChild<TopBorder>(tb);
BottomBorder bb = new BottomBorder();
bb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Dashed);
bb.Size = 24;
borders.AppendChild<BottomBorder>(bb);
LeftBorder lb = new LeftBorder();
lb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Dashed);
lb.Size = 24;
borders.AppendChild<LeftBorder>(lb);
RightBorder rb = new RightBorder();
rb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Dashed);
rb.Size = 24;
borders.AppendChild<RightBorder>(rb);
InsideHorizontalBorder ihb = new InsideHorizontalBorder();
ihb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Single);
ihb.Size = 24;
ihb.Color = new DocumentFormat.OpenXml.StringValue("#FF0000");
borders.AppendChild<InsideHorizontalBorder>(ihb);
InsideVerticalBorder ivb = new InsideVerticalBorder();
ivb.Val = new DocumentFormat.OpenXml
.EnumValue<BorderValues>(BorderValues.Dashed);
ivb.Size = 24;
borders.AppendChild<InsideVerticalBorder>(ivb);
TableProperties tblProp = new TableProperties(borders);
table.AppendChild<TableProperties>(tblProp);
// Loop through the repeating table and create rows in the table
XPathNodeIterator iter = root.Select("//my:group2",
NamespaceManager);
while (iter.MoveNext())
{
string cell1 = iter.Current.SelectSingleNode(
"my:cell1", NamespaceManager).Value;
string cell2 = iter.Current.SelectSingleNode(
"my:cell2", NamespaceManager).Value;
string cell3 = iter.Current.SelectSingleNode(
"my:cell3", NamespaceManager).Value;
TableRow tr = createRow(cell1, cell2, cell3);
table.Append(tr);
}
// Add the table to the document
mainPart.Document.Body.Append(table);
// Save the document
mainPart.Document.Save();
}
}
}
}