private static void Main() { DateTime n = DateTime.Now; var tempDi = new DirectoryInfo( $"ExampleOutput-{n.Year - 2000:00}-{n.Month:00}-{n.Day:00}-{n.Hour:00}{n.Minute:00}{n.Second:00}"); tempDi.Create(); var di = new DirectoryInfo("../../../"); foreach (FileInfo file in di.GetFiles("*.docx")) { Console.WriteLine(file.Name); var newFile = new FileInfo(Path.Combine(tempDi.FullName, file.Name.Replace(".docx", "out.docx"))); File.Copy(file.FullName, newFile.FullName); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(newFile.FullName, true)) { var settings = new FormattingAssemblerSettings { ClearStyles = true, RemoveStyleNamesFromParagraphAndRunProperties = true, CreateHtmlConverterAnnotationAttributes = true, OrderElementsPerStandard = true, RestrictToSupportedLanguages = true, RestrictToSupportedNumberingFormats = true }; FormattingAssembler.AssembleFormatting(wDoc, settings); } } }
static void Main(string[] args) { DirectoryInfo di = new DirectoryInfo("../../"); foreach (var file in di.GetFiles("*out.docx")) { file.Delete(); } foreach (var file in di.GetFiles("*.docx")) { Console.WriteLine(file.Name); var newFile = new FileInfo("../../" + file.Name.Replace(".docx", "out.docx")); File.Copy(file.FullName, newFile.FullName); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(newFile.FullName, true)) { FormattingAssemblerSettings settings = new FormattingAssemblerSettings() { ClearStyles = true, RemoveStyleNamesFromParagraphAndRunProperties = true, CreateHtmlConverterAnnotationAttributes = true, OrderElementsPerStandard = true, RestrictToSupportedLanguages = true, RestrictToSupportedNumberingFormats = true, }; FormattingAssembler.AssembleFormatting(wDoc, settings); } } }
private static ListItemSource InitializeStyleListItemSource(XDocument numXDoc, XDocument stylesXDoc, XElement paragraph, string paragraphStyleName, out int?ilvl, out bool?zeroNumId) { zeroNumId = null; XElement pPr = FormattingAssembler.ParagraphStyleRollup(paragraph, stylesXDoc, GetDefaultParagraphStyleName(stylesXDoc)); if (pPr != null) { XElement styleNumberingProperties = pPr .Elements(W.numPr) .FirstOrDefault(); if (styleNumberingProperties != null && styleNumberingProperties.Element(W.numId) != null) { int numId = (int)styleNumberingProperties .Elements(W.numId) .Attributes(W.val) .FirstOrDefault(); ilvl = (int?)styleNumberingProperties .Elements(W.ilvl) .Attributes(W.val) .FirstOrDefault(); if (ilvl == null) { ilvl = 0; } if (numId == 0) { zeroNumId = true; return(null); } // make sure that the numId is valid XElement num = numXDoc .Root .Elements(W.num) .Where(e => (int)e.Attribute(W.numId) == numId) .FirstOrDefault(); if (num == null) { zeroNumId = true; return(null); } ListItemSource listItemSource = new ListItemSource(numXDoc, stylesXDoc, numId); listItemSource.Style_ilvl = (int)ilvl; zeroNumId = false; return(listItemSource); } } ilvl = null; return(null); }
public void FA001_DocumentsWithRevTracking(string testId, string src) { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Load the source document DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/"); FileInfo sourceDocxFi = new FileInfo(Path.Combine(sourceDir.FullName, src)); WmlDocument wmlSourceDocument = new WmlDocument(sourceDocxFi.FullName); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Create the dir for the test var rootTempDir = TestUtil.TempDir; var thisTestTempDir = new DirectoryInfo(Path.Combine(rootTempDir.FullName, testId)); if (thisTestTempDir.Exists) { Assert.True(false, "Duplicate test id: " + testId); } else { thisTestTempDir.Create(); } var tempDirFullName = thisTestTempDir.FullName; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copy src DOCX to temp directory, for ease of review var sourceDocxCopiedToDestFileName = new FileInfo(Path.Combine(tempDirFullName, sourceDocxFi.Name)); if (!sourceDocxCopiedToDestFileName.Exists) { wmlSourceDocument.SaveAs(sourceDocxCopiedToDestFileName.FullName); } var sourceDocxAcceptedCopiedToDestFileName = new FileInfo(Path.Combine(tempDirFullName, sourceDocxFi.Name.ToLower().Replace(".docx", "-accepted.docx"))); var wmlSourceAccepted = RevisionProcessor.AcceptRevisions(wmlSourceDocument); wmlSourceAccepted.SaveAs(sourceDocxAcceptedCopiedToDestFileName.FullName); var outFi = new FileInfo(Path.Combine(tempDirFullName, "Output.docx")); FormattingAssemblerSettings settings = new FormattingAssemblerSettings(); var assembledWml = FormattingAssembler.AssembleFormatting(wmlSourceDocument, settings); assembledWml.SaveAs(outFi.FullName); var outAcceptedFi = new FileInfo(Path.Combine(tempDirFullName, "Output-accepted.docx")); var assembledAcceptedWml = RevisionProcessor.AcceptRevisions(assembledWml); assembledAcceptedWml.SaveAs(outAcceptedFi.FullName); Validate(outFi); }
public static void CopyFormattingAssembledDocx(FileInfo source, FileInfo dest) { var ba = File.ReadAllBytes(source.FullName); using (MemoryStream ms = new MemoryStream()) { ms.Write(ba, 0, ba.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true)) { RevisionAccepter.AcceptRevisions(wordDoc); SimplifyMarkupSettings simplifyMarkupSettings = new SimplifyMarkupSettings { RemoveComments = true, RemoveContentControls = true, RemoveEndAndFootNotes = true, RemoveFieldCodes = false, RemoveLastRenderedPageBreak = true, RemovePermissions = true, RemoveProof = true, RemoveRsidInfo = true, RemoveSmartTags = true, RemoveSoftHyphens = true, RemoveGoBackBookmark = true, ReplaceTabsWithSpaces = false, }; MarkupSimplifier.SimplifyMarkup(wordDoc, simplifyMarkupSettings); FormattingAssemblerSettings formattingAssemblerSettings = new FormattingAssemblerSettings { RemoveStyleNamesFromParagraphAndRunProperties = false, ClearStyles = false, RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, CreateHtmlConverterAnnotationAttributes = true, OrderElementsPerStandard = false, ListItemRetrieverSettings = new ListItemRetrieverSettings() { ListItemTextImplementations = ListItemRetrieverSettings.DefaultListItemTextImplementations, }, }; FormattingAssembler.AssembleFormatting(wordDoc, formattingAssemblerSettings); } var newBa = ms.ToArray(); File.WriteAllBytes(dest.FullName, newBa); } }
public void Sample(string fileName) { var file = new FileInfo(Path.Combine("../../../Word/Samples/FormattingAssembler/", fileName)); var newFile = new FileInfo(Path.Combine(TempDir, file.Name.Replace(".docx", "out.docx"))); File.Copy(file.FullName, newFile.FullName, true); using var wDoc = WordprocessingDocument.Open(newFile.FullName, true); var settings = new FormattingAssemblerSettings() { ClearStyles = true, RemoveStyleNamesFromParagraphAndRunProperties = true, CreateHtmlConverterAnnotationAttributes = true, OrderElementsPerStandard = true, RestrictToSupportedLanguages = true, RestrictToSupportedNumberingFormats = true, }; FormattingAssembler.AssembleFormatting(wDoc, settings); }