You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Hello, I create generation of word file from dotx template. But in desktop Word Office app I can't open file, but with another ways, I can open (WPS, Libre Office, Google Docs, Online Office)
What the problem? Is this because I clone dotx file and generate docx?
`
public ActionResult GenerateDocument(int reportViewId, int rid)
{
string templatePath = Server.MapPath("~/Content/templates/word/Template.dotx");
if (RepView.WordTemplateFileName != null)
{
templatePath = Server.MapPath("\\UserFiles\\ReportViewWordTemplates\\" + RepView.WordTemplateFileId);
}
string outputPath = Server.MapPath("~/generated.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templatePath, false))
{
wordDoc.Clone(memoryStream);
}
}
catch
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templatePath, false))
{
wordDoc.Clone(memoryStream);
}
}
using (WordprocessingDocument generatedDoc = WordprocessingDocument.Open(memoryStream, true))
{
memoryStream.Position = 0;
var mainDocumentPart = generatedDoc.MainDocumentPart;
var body = mainDocumentPart.Document.Body;
var headerParts = generatedDoc.MainDocumentPart.HeaderParts;
var footerParts = generatedDoc.MainDocumentPart.FooterParts;
foreach (var parts in headerParts)
{
foreach (var currentText in parts.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
{
currentText.Text = currentText.Text.Replace("Header", header);
}
}
foreach (var parts in footerParts)
{
foreach (var currentText in parts.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
{
currentText.Text = currentText.Text.Replace("Bottom", bottom);
}
}
ImagePart imgp = mainDocumentPart.AddImagePart(ImagePartType.Png);
var textElements = body.Descendants<Text>()
.Where(t => t.Text.Contains("text"))
.FirstOrDefault();
var run = new Run(new Text());
if (textElements != null)
{
run = textElements.Parent as Run;
}
foreach (var item in headers)
{
if (blocks.Where(p => p.DocumentConstructorHeaderId == item.DocumentConstructorHeaderId).Count() != 0)
{
if (item.SizeTitle > 1)
{
AddHeading(body, item.Title, 1, mainDocumentPart.StyleDefinitionsPart);
}
else
{
AddHeading(body, item.Title, 2, mainDocumentPart.StyleDefinitionsPart);
}
}
foreach (var list in newBlocks.Where(p => p.DocumentConstructorHeaderId == item.DocumentConstructorHeaderId).OrderBy(p => p.Npp).ToList())
{
if (list.ImageData != null)
{
using (var imageStream = new MemoryStream(list.ImageData))
{
imgp.FeedData(imageStream);
}
AddImageToBody(mainDocumentPart, list.ImageData);
}
/*if (list.Content != null)
{
AddHeading(body, item.Title, 1, mainDocumentPart.StyleDefinitionsPart);
}*/
Paragraph para = body.AppendChild(new Paragraph());
if (textElements != null)
{
// Извлекаем свойства Run
var runProperties = run.RunProperties?.CloneNode(true);
var newRun = new Run(new Text(list.Content));
newRun.PrependChild(runProperties);
para.AppendChild(newRun);
}
else
{
para.AppendChild(new Run(new Text(list.Content)));
}
ReplaceText(body, "text", "");
}
}
generatedDoc.Close();
}
return File(memoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "generated.docx");
}
}
`
The text was updated successfully, but these errors were encountered:
Describe the bug
Hello, I create generation of word file from dotx template. But in desktop Word Office app I can't open file, but with another ways, I can open (WPS, Libre Office, Google Docs, Online Office)
What the problem? Is this because I clone dotx file and generate docx?
`
public ActionResult GenerateDocument(int reportViewId, int rid)
{
}
`
The text was updated successfully, but these errors were encountered: