1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
private void ProcessFileWord(SPFile file)
{
Microsoft.Office.Interop.Word._Document document = null;
object missing = System.Reflection.Missing.Value;
object fileName = this.webSiteUrl + "/" + file.Url;
try
{
if (this.wordApplication == null)
{
wordApplication = new Microsoft.Office.Interop.Word.Application();
wordApplication.Visible = false;
}
//Ouverture du document
document = wordApplication.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
if (document != null)
{
this.InitWordContentControl(document, document.ContentControls);
foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
{
foreach (Microsoft.Office.Interop.Word.HeaderFooter header in section.Headers)
this.InitWordContentControl(document, header.Range.ContentControls);
foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in section.Footers)
this.InitWordContentControl(document, footer.Range.ContentControls);
}
// Sauvegarde du document
document.Save();
}
}
finally
{
// Fermeture du document
if (document != null)
document.Close(ref missing, ref missing, ref missing);
}
}
private void InitWordContentControl(Microsoft.Office.Interop.Word._Document doc,
Microsoft.Office.Interop.Word.ContentControls contentControls)
{
if (contentControls != null)
{
foreach (Microsoft.Office.Interop.Word.ContentControl cc in contentControls)
{
if (cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText
|| cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText)
{
if (cc.Tag == "ProjectName")
{
cc.Range.Text = this.projectName;
}
if (cc.Tag == "CustomerName")
{
cc.Range.Text = this.customerName;
}
}
else if (cc.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlPicture)
{
if (!this.customerLogo.Equals(System.IO.Stream.Null))
{
if (cc.Tag == "CustomerLogo" ||
cc.Tag == "CustomerLogoHeader")
{
InsertPicInWord(doc, cc);
}
}
}
}
}
}
private bool InsertPicInWord(Microsoft.Office.Interop.Word._Document document,
Microsoft.Office.Interop.Word.ContentControl cc)
{
if (this.customerLogo.Equals(System.IO.Stream.Null) ||
document == null)
return false;
System.IO.MemoryStream ms = new System.IO.MemoryStream(this.customerLogo);
string ImageText = System.Convert.ToBase64String(ms.ToArray());
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlNode declarationNode = doc.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, String.Empty, String.Empty);
doc.AppendChild(declarationNode);
System.Xml.XmlElement imageElement = doc.CreateElement("Image");
System.Xml.XmlText imageElementValue = doc.CreateTextNode(ImageText);
doc.AppendChild(imageElement);
doc.LastChild.AppendChild(imageElementValue);
Microsoft.Office.Core.CustomXMLPart customXmlPart = document.CustomXMLParts.Add(doc.InnerXml, System.Type.Missing);
cc.XMLMapping.SetMapping("/Image", String.Empty, customXmlPart);
return true;
} |
Partager