Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
- added fix for dangling commentrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
desjarlais committed Mar 10, 2021
1 parent 50abed5 commit 9ffccb8
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 9 deletions.
16 changes: 15 additions & 1 deletion WinForms/FrmFixDocument.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions WinForms/FrmFixDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void ResetCheckboxes()
RdoNotes.Checked = false;
RdoFixNotesPageWithFile.Checked = false;
RdoTblGrid.Checked = false;
RdoFixComments.Checked = false;
}

/// <summary>
Expand All @@ -52,6 +53,7 @@ public void EnableWordUI()
RdoRev.Enabled = true;
RdoEndnotes.Enabled = true;
RdoTblGrid.Enabled = true;
RdoFixComments.Enabled = true;
}

/// <summary>
Expand Down Expand Up @@ -94,6 +96,10 @@ private void BtnOk_Click(object sender, EventArgs e)
{
OptionSelected = "NotesWithFile";
}
else if (RdoFixComments.Checked)
{
OptionSelected = "FixComments";
}

DialogResult = DialogResult.OK;
Close();
Expand Down
100 changes: 100 additions & 0 deletions WinForms/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4266,6 +4266,103 @@ public void FixBookmarks()
}
}

public void FixCorruptComments()
{
try
{
LstDisplay.Items.Clear();
Cursor = Cursors.WaitCursor;

using (WordprocessingDocument document = WordprocessingDocument.Open(TxtFileName.Text, true))
{
// TODO
// loop each comment in comments.xml and get a list of comment ref #'s
// loop each comment in document, compare id to commentRef
// if the id does not exist, it needs to be deleted
WordprocessingCommentsPart commentsPart = document.MainDocumentPart.WordprocessingCommentsPart;
IEnumerable<OpenXmlUnknownElement> unknownList = document.MainDocumentPart.Document.Descendants<OpenXmlUnknownElement>();
bool saveFile = false;
bool cRefIdExists = false;

if (commentsPart == null)
{
LogInformation(LogType.EmptyCount, StringResources.wComments, string.Empty);
}
else
{
foreach (OpenXmlUnknownElement uk in unknownList)
{
// for some reason these dangling refs are considered unknown types, not commentrefs
// convert to an openxmlelement then type it to a commentref to get the id
if (uk.LocalName == "commentReference")
{
// so far I only see the id in the outerxml
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(uk.OuterXml);

// traverse the outerxml until we get to the id
if (xDoc.ChildNodes.Count > 0)
{
foreach (XmlNode xNode in xDoc.ChildNodes)
{
if (xNode.Attributes.Count > 0)
{
foreach (XmlAttribute xa in xNode.Attributes)
{
if (xa.LocalName == "id")
{
// now that we have the id number, we can use it to compare with the comment part
// if the id exists in commentref but not the commentpart, it can be deleted
foreach (O.Wordprocessing.Comment cm in commentsPart.Comments)
{
int cId = Convert.ToInt32(cm.Id);
int cRefId = Convert.ToInt32(xa.Value);

if (cId == cRefId)
{
cRefIdExists = true;
}
}

if (cRefIdExists == false)
{
uk.Remove();
saveFile = true;
}
else
{
cRefIdExists = false;
}
}
}
}
}
}
}
}

if (saveFile)
{
document.MainDocumentPart.Document.Save();
LstDisplay.Items.Add("** Corrupt Comment Fixed **");
}
else
{
LstDisplay.Items.Add("** No Corrupt Comment Found **");
}
}
}
}
catch (Exception ex)
{
LogInformation(LogType.LogException, "BtnFixCorruptComments", ex.Message);
}
finally
{
Cursor = Cursors.Default;
}
}

/// <summary>
/// this fix is for a known issue where files contain a table
/// with a tblGrid element before the first table row, that is not valid per the schema
Expand Down Expand Up @@ -4674,6 +4771,9 @@ private void BtnFixDocument_Click(object sender, EventArgs e)
case "TblGrid":
FixTblGrid();
break;
case "FixComments":
FixCorruptComments();
break;
default:
LstDisplay.Items.Add("No Option Selected");
LoggingHelper.Log("BtnFixDocument - No Option Selected");
Expand Down
2 changes: 0 additions & 2 deletions WinForms/FrmParagraphs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Xml.Linq;
using Office_File_Explorer.Word_Helpers;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion bin/Debug/Office File Explorer.application
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>LTgOozYHdAhmC9UgGJdXCQj6Vd2e+dLa5ft6K2N4Aj8=</dsig:DigestValue>
<dsig:DigestValue>VYhkgUaxjAfs1M+0e1cm38oXTIHUy3Zzb/Gmxiv9yYk=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file modified bin/Debug/Office File Explorer.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions bin/Debug/Office File Explorer.exe.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Office File Explorer.exe" size="4488704">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Office File Explorer.exe" size="4490752">
<assemblyIdentity name="Office File Explorer" version="1.0.2.50" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>EJR/AvBYx4uImKsS4Qxy20Ae8u8lTcCrEnCKDAuDNag=</dsig:DigestValue>
<dsig:DigestValue>JUI6X+y6cXWa7XqzBbBD0R0OK6QGToJxmHh7FDob+c8=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file modified bin/Debug/Office File Explorer.pdb
Binary file not shown.
Binary file modified bin/Debug/app.publish/Office File Explorer.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion obj/Debug/Office File Explorer.application
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>LTgOozYHdAhmC9UgGJdXCQj6Vd2e+dLa5ft6K2N4Aj8=</dsig:DigestValue>
<dsig:DigestValue>VYhkgUaxjAfs1M+0e1cm38oXTIHUy3Zzb/Gmxiv9yYk=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file modified obj/Debug/Office File Explorer.csproj.GenerateResource.cache
Binary file not shown.
Binary file modified obj/Debug/Office File Explorer.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions obj/Debug/Office File Explorer.exe.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Office File Explorer.exe" size="4488704">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="Office File Explorer.exe" size="4490752">
<assemblyIdentity name="Office File Explorer" version="1.0.2.50" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>EJR/AvBYx4uImKsS4Qxy20Ae8u8lTcCrEnCKDAuDNag=</dsig:DigestValue>
<dsig:DigestValue>JUI6X+y6cXWa7XqzBbBD0R0OK6QGToJxmHh7FDob+c8=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file modified obj/Debug/Office File Explorer.pdb
Binary file not shown.

0 comments on commit 9ffccb8

Please sign in to comment.