Я надеюсь, что кто-то может помочь с этим вопросом. Я использую Open XMl для поиска и замены текста в верхнем и нижнем колонтитулах текстового документа, используя Open XML. Текст работает нормально. Но изображение не заменяет, как я ожидал. Изображение (@ "C: \ temp \ pic \ Lo go .gif") существует в источнике. Но никогда не отображается в документе слово правильно. Вы можете увидеть в регионе ниже (#region "Изменить заголовок Lo go") Кто-нибудь может помочь? Спасибо!
Ошибка в слове
public void SearchAndReplace(string templateFileNameTemplate, string targetFileName, ProjectModel project)
{
//copy template file
if (File.Exists(targetFileName) == true)
{
File.Delete(targetFileName);
}
File.Copy(templateFileNameTemplate, targetFileName);
string searchText = "", replaceText = "", headerLogo = "", headerText = "";
bool replace = false;
//open file
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(targetFileName, true))
{
//Gets all the headers
foreach (var headerPart in wordDoc.MainDocumentPart.HeaderParts)
{
//Gets the text in headers
foreach (var currentText in headerPart.RootElement.Descendants<Text>())
{
replace = false;
switch (currentText.Text)
{
case "<COMPANY NAME>":
replace = true;
replaceText = project.ClientName;
searchText = currentText.Text;
break;
**case "<INSERT YOUR LOGO HERE>":
headerLogo = @"C:\temp\pic\Logo.gif";//project.ClientLogo;**
headerText = currentText.Text;
break;
case "<ISSUE DATE>":
replace = true;
replaceText = project.AssignedDate.ToShortDateString();
searchText = currentText.Text;
break;
case "<REVIEW DATE>":
replace = true;
replaceText = project.ClientDueDate.ToShortDateString();
searchText = currentText.Text;
break;
default:
break;
}
if(searchText.Length>0 && replace==true)
currentText.Text = currentText.Text.Replace(searchText, replaceText);
}
}
//Gets all the footers
foreach (var footerPart in wordDoc.MainDocumentPart.FooterParts)
{
//Gets the text in headers
foreach (var currentText in footerPart.RootElement.Descendants<Text>())
{
replace = false;
switch (currentText.Text)
{
case "<COMPANY NAME>":
replace = true;
replaceText = project.ClientName;
searchText = currentText.Text;
break;
default:
break;
}
if (searchText.Length > 0 && replace == true)
currentText.Text = currentText.Text.Replace(searchText, replaceText);
}
}
#region "Modify Header Logo"
if(headerLogo != string.Empty)
{
ImagePart imagePart = wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Png);
using (FileStream stream = new FileStream(headerLogo, FileMode.Open))
{
imagePart.FeedData(stream);
}
searchText = headerText;
Text textPlaceHolder = null;
// Insert image (the image created with your function) after text place holder.
// Search for text holder
//Gets all the headers
foreach (var headerPart in wordDoc.MainDocumentPart.HeaderParts)
{
//Gets the text in headers
foreach (var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
{
textPlaceHolder = headerPart.RootElement.Descendants<Text>()
.Where((x) => x.Text == searchText).First();
}
}
if (textPlaceHolder != null)
{
var parent = textPlaceHolder.Parent;
if (parent is Run) // Parent should be a run element.
{
//add image
Drawing imageElement = GetImageElement(
wordDoc.MainDocumentPart.GetIdOfPart(imagePart),
headerLogo,
"my image",
475,
245);
// Insert image (the image created with your function) after text place holder.
textPlaceHolder.Parent.InsertAfter<Drawing>(imageElement, textPlaceHolder);
// Remove text place holder.
textPlaceHolder.Remove();
}
}
}
#endregion
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("<COMPANY NAME>");
docText = regexText.Replace(docText, project.ClientName);
//regexText = new Regex("<COMPANY NAME>");
//docText = regexText.Replace(docText, project.ClientName);
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
//wordDoc.SaveAs(targetFileName).Close();
}
}
private static Drawing GetImageElement(
string imagePartId,
string fileName,
string pictureName,
double width,
double height)
{
double englishMetricUnitsPerInch = 914400;
double pixelsPerInch = 96;
//calculate size in emu
double emuWidth = width * englishMetricUnitsPerInch / pixelsPerInch;
double emuHeight = height * englishMetricUnitsPerInch / pixelsPerInch;
var element = new Drawing(
new A.Wordprocessing.Inline(
new A.Wordprocessing.Extent { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight },
new A.Wordprocessing.EffectExtent { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new A.Wordprocessing.DocProperties { Id = (UInt32Value)1U, Name = pictureName },
new A.Wordprocessing.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new A.Pictures.Picture(
new A.Pictures.NonVisualPictureProperties(
new A.Pictures.NonVisualDrawingProperties { Id = (UInt32Value)0U, Name = fileName },
new A.Pictures.NonVisualPictureDrawingProperties()),
new A.Pictures.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }))
{
Embed = imagePartId,
CompressionState = A.BlipCompressionValues.Print
},
new A.Stretch(new A.FillRectangle())),
new A.Pictures.ShapeProperties(
new A.Transform2D(
new A.Offset { X = 0L, Y = 0L },
new A.Extents { Cx = (Int64Value)emuWidth, Cy = (Int64Value)emuHeight }),
new A.PresetGeometry(
new A.AdjustValueList())
{ Preset = A.ShapeTypeValues.Rectangle })))
{
Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
}))
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
return element;
}