-
Notifications
You must be signed in to change notification settings - Fork 538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatting of Images in word document from rich text control is not aligned. #1652
Comments
Hi @sarabukranthikumar, How are you inserting the images? Can you send a repro? |
Hi @sarabukranthikumar, I received the image you sent, but it cuts off and I can't read all the code. Can you copy and paste the code text into a code block, so all the text is there? |
Hi @sarabukranthikumar, Looking at the image you sent it looks like you are receiving an html string and replacing some of its content. Can you share where you are using the OOXML SDK in your code? |
Hello @mikeebowen, as per your request below are the code where we are using OOXML SDK. /// <summary>
/// Main Generate funtion
/// </summary>
public byte[] Generate(XDocument xDoc, IOrderedEnumerable<WorkProductModuleProfile> modules, Boolean isPageBreak, byte[] templateBytes, string kaTitle, string kaSubject, string kaAuthor, int kaClassification)
{
try
{
if (null == modules)
return null;
if (null == xDoc)
return null;
string coverPageFile = "";
WorkProductModuleProfile module = modules.FirstOrDefault();
string fileName = getDocumentFile(module);
WordprocessingDocument doc = CreateDoc(xDoc.Root, templateBytes, fileName);
coverPageFile = fileName;
return createMainDocument(xDoc, coverPageFile, isPageBreak, kaTitle, kaSubject, kaAuthor, kaClassification);
}
catch (Exception ex)
{
throw;
}
}
public WordprocessingDocument CreateDoc(XElement document, byte[] templateBytes, string fileName)
{
try
{
if (null == document)
throw new ArgumentNullException("document");
WordprocessingDocument doc = null;
byte[] byteArray = templateBytes;
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
MarkupCompatibilityProcessSettings ms = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Microsoft365);
OpenSettings os = new OpenSettings();
os.MarkupCompatibilityProcessSettings = ms;
using (doc = WordprocessingDocument.Open(stream, true, os))
{
XDocument xDoc = doc.MainDocumentPart.GetXDocument();
string selector = generateModuleDataSelector(doc);
if (String.IsNullOrEmpty(selector))
{
save(fileName, stream);
return doc;
}
XElement newRootElement = (XElement)Transform(doc, xDoc.Root, document.XPathSelectElement(selector));
if (null != newRootElement)
xDoc.Elements().First().ReplaceWith(newRootElement);
doc.MainDocumentPart.PutXDocument();
}
save(fileName, stream);
stream.Close();
}
return doc;
}
catch (Exception ex)
{
LoggingHelper.LogInformation("Error in Create Doc", LoggingCategory.Debug, "Report");
LogException(ex);
throw;
}
}
private byte[] createMainDocument(XDocument xDoc, string coverPageFile, Boolean isPageBreak, string kaTitle, string kaSubject, string kaAuthor, int kaClassification)
{
try
{
if (ConfigurationHelper.LogEnabled)
LoggingHelper.LogInformation("coverPageFile path - " + coverPageFile, LoggingCategory.Debug, "Report");
string copyRight = "Unknown";
if (kaClassification == 1)
{
copyRight = "Copyrighted";
}
SetCustomProperty(coverPageFile, "Copyright Status", copyRight, PropertyTypes.Text);
SetCustomProperty(coverPageFile, "Copyright Notice", "testing", PropertyTypes.Text);
MarkupCompatibilityProcessSettings ms = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Microsoft365);
OpenSettings os = new OpenSettings();
os.MarkupCompatibilityProcessSettings = ms;
using (wordDoc = WordprocessingDocument.Open(coverPageFile, true, os))
{
try
{
foreach (DocumentFormat.OpenXml.Packaging.HeaderPart header in wordDoc.MainDocumentPart.HeaderParts.ToList())
{
XDocument xHeaderDoc = header.GetXDocument();
XElement newRootElement = (XElement)Transform(wordDoc, xHeaderDoc.Root, xDoc.Root);
xHeaderDoc.Root.ReplaceWith(newRootElement);
header.PutXDocument(xHeaderDoc);
}
}
catch (Exception ex)
{
LoggingHelper.LogInformation("Error while processing HeaderParts", LoggingCategory.Error, "KA Report");
LogException(ex);
throw;
}
// code to update the core properties ends
XNamespace ep = "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
XNamespace dc = "http://purl.org/dc/elements/1.1/";
XNamespace cp = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
XDocument coreProperties = wordDoc.CoreFilePropertiesPart.GetXDocument();
XDocument eProperties = wordDoc.ExtendedFilePropertiesPart.GetXDocument();
XDocument customProperties = wordDoc.CustomFilePropertiesPart.GetXDocument();
XElement title = coreProperties.Root.Element(dc + "title");
if (null == title)
{
title = new XElement(dc + "title", kaTitle);
coreProperties.Root.Add(title);
}
else
{
title.Value = kaTitle;
}
XElement subject = coreProperties.Root.Element(dc + "subject");
if (null == subject)
{
subject = new XElement(dc + "subject", kaSubject);
coreProperties.Root.Add(subject);
}
else
{
subject.Value = kaSubject;
}
XElement creator = coreProperties.Root.Element(dc + "creator");
if (null == creator)
{
creator = new XElement(dc + "creator", kaAuthor);
coreProperties.Root.Add(creator);
}
else
{
creator.Value = kaAuthor;
}
wordDoc.CoreFilePropertiesPart.GetXDocument().Root.ReplaceWith(coreProperties.Root);
wordDoc.CoreFilePropertiesPart.PutXDocument(coreProperties);
XDocument extendedProperties = wordDoc.ExtendedFilePropertiesPart.GetXDocument();
extendedProperties.Element(ep + "Properties").Element(ep + "Company").Value = "testing";
wordDoc.ExtendedFilePropertiesPart.GetXDocument().Root.ReplaceWith(extendedProperties.Root);
wordDoc.ExtendedFilePropertiesPart.PutXDocument(extendedProperties);
try
{
foreach (DocumentFormat.OpenXml.Packaging.FooterPart footer in wordDoc.MainDocumentPart.FooterParts.ToList())
{
XDocument xFooterDoc = footer.GetXDocument();
XElement newRootElement = (XElement)Transform(wordDoc, xFooterDoc.Root, xDoc.Root);
xFooterDoc.Root.ReplaceWith(newRootElement);
footer.PutXDocument(xFooterDoc);
}
}
catch (Exception ex)
{
LoggingHelper.LogInformation("Error during update FooterParts", LoggingCategory.Error, "KA Report");
LogException(ex);
throw;
}
wordDoc.MainDocumentPart.Document.Save();
}
byte[] bytes = null;
try
{
using (FileStream stream = new FileStream(coverPageFile, FileMode.Open))
{
bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
}
File.Delete(coverPageFile);
if (Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), documentDirectory)))
Directory.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), documentDirectory));
}
catch (Exception ex)
{
LoggingHelper.LogInformation("Error during final document", LoggingCategory.Error, "Work Product Report");
LogException(ex);
}
return bytes;
}
catch (Exception ex)
{
LoggingHelper.LogInformation("Error during creatmain document function", LoggingCategory.Error, "Work Product Report");
LogException(ex);
throw;
}
} |
Hi we are using open xml sdk to generate word document. The content into word document is fetched from external resource crm. we are using a word template defined in crm and adding component into the word document from the fetched rich text control source.
The issue is mainly when the images are between the text words, not when image is after the lines.
The text was updated successfully, but these errors were encountered: