/// <summary> /// Creates a new RangeDataEx object. /// </summary> /// <param name="rangeStart">The starting point of the specified range.</param> /// <param name="rangeEnd">The ending point of the specified range.</param> /// <param name="baseRange">An existing RangeDataEx from which to clone formatting information.</param> public RangeDataEx(int rangeStart, int rangeEnd, RangeDataEx baseRange) { m_Start = rangeStart; m_End = rangeEnd; m_Font = baseRange.m_Font; m_FontSize = baseRange.m_FontSize; m_Bold = baseRange.m_Bold; m_Italics = baseRange.m_Italics; m_InMath = baseRange.m_InMath; }
/// <summary> /// Determines if two ranges have the same formatting applied to them. /// </summary> /// <param name="ComparisonRange">A RangeDataEx to compare to.</param> /// <returns>True if identical, False otherwise.</returns> public bool IdenticalTo(RangeDataEx ComparisonRange) { if (m_Bold != ComparisonRange.m_Bold) { return(false); } else if (m_Italics != ComparisonRange.m_Italics) { return(false); } else if (m_Font != ComparisonRange.m_Font) { return(false); } else if (m_FontSize != ComparisonRange.m_FontSize) { return(false); } else { return(true); } }
/// <summary> /// Redact a range, replacing all marked text. /// </summary> /// <param name="range">A range to redact.</param> /// <param name="rangeData">A RangeDataEx containing properties about the range.</param> private void RedactRange(Word.Range range, RangeDataEx rangeData) { object ParagraphNumber = Word.WdNumberType.wdNumberParagraph; foreach (Word.Paragraph p in range.Paragraphs) { //get the para's range Word.Range ParagraphRange = p.Range; //trim to the selection, if needed if (range.Start > ParagraphRange.Start) { ParagraphRange.Start = range.Start; } if (range.End < ParagraphRange.End) { ParagraphRange.End = range.End; } if (ParagraphRange.End == p.Range.End - 1 && ParagraphRange.Start == p.Range.Start) { p.Range.ListFormat.ConvertNumbersToText(ref ParagraphNumber); //if the whole para was redacted, redact the numbering } //make it black on black ParagraphRange.Font.Shading.BackgroundPatternColor = Word.WdColor.wdColorAutomatic; ParagraphRange.HighlightColorIndex = Word.WdColorIndex.wdBlack; //moving to highlighting instead of text background ParagraphRange.Font.Color = Word.WdColor.wdColorBlack; //get rid of links and bookmarks foreach (Word.Hyperlink Hyperlink in ParagraphRange.Hyperlinks) { Hyperlink.Delete(); } foreach (Word.Bookmark Bookmark in ParagraphRange.Bookmarks) { Bookmark.Delete(); } //BUG 110: suppress proofing errors ParagraphRange.NoProofing = -1; //finally, replace the text Debug.Assert(ParagraphRange.ShapeRange.Count == 0, "Some Shapes were not redacted by RedactShapes."); if (ParagraphRange.InlineShapes.Count > 0) { //if there are images, then split into subranges and process text and images separately List <RangeData> Subranges = RedactCommon.SplitRange(ParagraphRange); for (int j = Subranges.Count - 1; j >= 0; j--) { //set start and end ParagraphRange.Start = Subranges[j].Start; ParagraphRange.End = Subranges[j].End; if (ParagraphRange.InlineShapes.Count > 0) { RedactInlineShape(ParagraphRange.InlineShapes[1]); } else { ParagraphRange.Text = RedactCommon.BuildFillerText(ParagraphRange, rangeData); } } } else { ParagraphRange.Text = RedactCommon.BuildFillerText(ParagraphRange, rangeData); } } }
/// <summary> /// Builds a string of filler characters of the same size as the input text. /// </summary> /// <param name="range">The Range for which to build filler text.</param> /// <param name="rangeData">The RangeDataEx containing properties about the range.</param> /// <returns>The filler text.</returns> internal static string BuildFillerText(Word.Range range, RangeDataEx rangeData) { string Result = string.Empty; Label Label = new Label(); //get the correct string if (rangeData.Bold && rangeData.Italic) { Label.Font = new System.Drawing.Font(rangeData.Font, rangeData.FontSize, FontStyle.Italic | FontStyle.Bold); } else if (rangeData.Bold) { Label.Font = new System.Drawing.Font(rangeData.Font, rangeData.FontSize, FontStyle.Bold); } else if (rangeData.Italic) { Label.Font = new System.Drawing.Font(rangeData.Font, rangeData.FontSize, FontStyle.Italic); } else { Label.Font = new System.Drawing.Font(rangeData.Font, rangeData.FontSize, FontStyle.Regular); } Label.AutoSize = true; char[] delim = { ' ', '\v', '\f' }; // \v is a soft break, \f is a page break string text = range.Text; if (text == null) { return(null); } string[] lines = text.Split(delim); int c = 0; Random rnd = new Random(); foreach (string line in lines) { if (!string.IsNullOrEmpty(line)) { Label.Text = line; float obfuscationFactor = (float)rnd.Next(90, 110) / 100; // make each word +/- 10% to make it harder to determine the original value float originalWidth = Label.PreferredWidth * obfuscationFactor; for (Label.Text = string.Empty; Label.PreferredWidth < originalWidth; Label.Text += "'") { ; } Result += Label.Text; c += line.Length; } if (c < text.Length) { Result += text[c]; c++; } } return(Result); }