static void EditSeparator(Document document) { #region #EditSeparator document.LoadDocument("Documents//Grimm.docx"); //Check whether the footnotes already have a separator: if (document.Footnotes.HasSeparator(NoteSeparatorType.Separator)) { //Initiate the update session: SubDocument noteSeparator = document.Footnotes.BeginUpdateSeparator(NoteSeparatorType.Separator); //Clear the separator range: noteSeparator.Delete(noteSeparator.Range); //Append a new text: noteSeparator.AppendText("***"); CharacterProperties characterProperties = noteSeparator.BeginUpdateCharacters(noteSeparator.Range); characterProperties.ForeColor = System.Drawing.Color.Blue; noteSeparator.EndUpdateCharacters(characterProperties); //Finalize the update: document.Footnotes.EndUpdateSeparator(noteSeparator); } #endregion #EditSeparator }
public static void SetTextWatermark(RichEditControl richEditControl, string text) { Section section = richEditControl.Document.Sections[0]; SubDocument subDocument = section.BeginUpdateHeader(); subDocument.Delete(subDocument.Range); Shape shape = subDocument.Shapes.InsertTextBox(subDocument.Range.Start); shape.ShapeFormat.TextBox.Document.AppendText(text); CharacterProperties cp = shape.ShapeFormat.TextBox.Document.BeginUpdateCharacters(shape.ShapeFormat.TextBox.Document.Range); cp.FontName = "Comic Sans MS"; cp.FontSize = 32; cp.ForeColor = Color.Red; Font measureFont = new Font(cp.FontName, cp.FontSize.Value); shape.ShapeFormat.TextBox.Document.EndUpdateCharacters(cp); shape.RotationAngle = -45; Size sizeInPixels = TextRenderer.MeasureText(text, measureFont); shape.Size = new SizeF(Units.PixelsToDocumentsF(sizeInPixels.Width, richEditControl.DpiX), Units.PixelsToDocumentsF(sizeInPixels.Height, richEditControl.DpiY)); shape.ShapeFormat.TextBox.HeightRule = TextBoxSizeRule.Auto; shape.Offset = new PointF(section.Page.Width / 2 - shape.Size.Width / 2, section.Page.Height / 2 - shape.Size.Height / 2); section.EndUpdateHeader(subDocument); }
public void AddImageWatermark(object sender, EventArgs e) { Image watermarkImage = null; OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "Select Image"; dlg.Filter = "image files (*.png)|*.png"; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { watermarkImage = Image.FromFile(dlg.FileName); } dlg.Dispose(); if (watermarkImage == null) { return; } Section currentSection = GetCurrentSection(); SubDocument subDocument = currentSection.BeginUpdateHeader(); subDocument.Delete(subDocument.Range); Shape shape = subDocument.Shapes.InsertPicture(subDocument.Range.Start, watermarkImage); shape.RotationAngle = -45; shape.Offset = new PointF(Convert.ToInt64(currentSection.Page.Width) / 2 - Convert.ToInt64(shape.Size.Width) / 2, Convert.ToInt64(currentSection.Page.Height) / 2 - Convert.ToInt64(shape.Size.Height) / 2); currentSection.EndUpdateHeader(subDocument); }
public void RemoveImageWatermark(object sender, EventArgs e) { Section currentSection = GetCurrentSection(); SubDocument subDocument = currentSection.BeginUpdateHeader(); subDocument.Delete(subDocument.Range); currentSection.EndUpdateHeader(subDocument); }
public static void SetImageWatermark(RichEditControl richEditControl, Image image) { Section section = richEditControl.Document.Sections[0]; SubDocument subDocument = section.BeginUpdateHeader(); subDocument.Delete(subDocument.Range); Shape shape = subDocument.Shapes.InsertPicture(subDocument.Range.Start, image); shape.RotationAngle = -45; shape.Offset = new PointF(section.Page.Width / 2 - shape.Size.Width / 2, section.Page.Height / 2 - shape.Size.Height / 2); section.EndUpdateHeader(subDocument); }
private static void AppendFooter(Section sourceSection, Section targetSection, HeaderFooterType headerFooterType) { if (!sourceSection.HasFooter(headerFooterType)) { return; } SubDocument source = sourceSection.BeginUpdateFooter(headerFooterType); SubDocument target = targetSection.BeginUpdateFooter(headerFooterType); target.Delete(target.Range); target.InsertDocumentContent(target.Range.Start, source.Range, InsertOptions.KeepSourceFormatting); // Delete empty paragraphs DocumentRange emptyParagraph = target.CreateRange(target.Range.End.ToInt() - 1, 1); target.Delete(emptyParagraph); sourceSection.EndUpdateFooter(source); targetSection.EndUpdateFooter(target); }
static void buttonCustomAction_ItemClick_Replace(object sender, ItemClickEventArgs e) { RichEditControl richEdit = e.Item.Tag as RichEditControl; DocumentRange range = richEdit.Document.Selection; int selLength = range.Length; string s = new String('*', selLength); SubDocument doc = range.BeginUpdateDocument(); doc.InsertSingleLineText(range.Start, s); DocumentRange rangeToRemove = doc.CreateRange(range.Start, selLength); doc.Delete(rangeToRemove); range.EndUpdateDocument(doc); }
static void EditFootnote(Document document) { #region #EditFootnote document.LoadDocument("Documents//Grimm.docx"); //Access the first fottnote's content: SubDocument footnote = document.Footnotes[0].BeginUpdate(); //Exclude the reference mark and the space after it from the range to be edited: DocumentRange noteTextRange = footnote.CreateRange(footnote.Range.Start.ToInt() + 2, footnote.Range.Length - 2); //Clear the range: footnote.Delete(noteTextRange); //Append a new text: footnote.AppendText("the text is removed"); //Finalize the update: document.Footnotes[0].EndUpdate(footnote); #endregion #EditFootnote }
static void EditSeparator(Document document) { #region #EditSeparator document.LoadDocument("Documents//Grimm.docx"); //Check whether the footnotes already have a separator: if (document.Footnotes.HasSeparator(NoteSeparatorType.Separator)) { //Initiate the update session: SubDocument noteSeparator = document.Footnotes.BeginUpdateSeparator(NoteSeparatorType.Separator); //Clear the separator range: noteSeparator.Delete(noteSeparator.Range); //Append a new text: noteSeparator.AppendText("***"); //Finalize the update: document.Footnotes.EndUpdateSeparator(noteSeparator); } #endregion #EditSeparator }
private void clearSubDocument(SubDocument document) { document.Delete(document.Range); }
protected virtual void SetHeaderFooterOptions(Document book, HeaderFooterKind headerFooterKind, string text, HeaderFooterOptions options) { options ??= new HeaderFooterOptions(); Section section; int sectionNum = options.SectionNum ?? -1; if (sectionNum > 0 && sectionNum <= book.Sections.Count) { section = book.Sections[sectionNum - 1]; } else if (sectionNum < 0 && -sectionNum <= book.Sections.Count) { section = book.Sections[book.Sections.Count - (-sectionNum)]; } else { throw new Exception("Invalid SectionNum."); } SubDocument document = null; DocumentRange range; var type = options.Type; try { switch (headerFooterKind) { case HeaderFooterKind.Header: if (options.LinkToNext) { section.LinkHeaderToNext((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); } if (options.LinkToPrevious) { section.LinkHeaderToPrevious((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); } document = section.BeginUpdateHeader((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); break; case HeaderFooterKind.Footer: if (options.LinkToNext) { section.LinkFooterToNext((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); } if (options.LinkToPrevious) { section.LinkFooterToPrevious((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); } document = section.BeginUpdateFooter((DevExpress.XtraRichEdit.API.Native.HeaderFooterType)type); break; default: throw new Exception("Invalid DocumentType."); } document.Delete(document.Range); if (options.Html) { //var insertOptions = string.IsNullOrWhiteSpace(ParagraphStyle) ? InsertOptions.KeepSourceFormatting : InsertOptions.UseDestinationStyles; var insertOptions = InsertOptions.KeepSourceFormatting; if (options.UseDestinationStyles) { insertOptions = InsertOptions.UseDestinationStyles; } if (options.KeepSourceFormatting) { insertOptions = InsertOptions.KeepSourceFormatting; } range = document.AppendHtmlText(text, insertOptions); } else { range = document.AppendText(text); } if (!string.IsNullOrWhiteSpace(options.CharacterStyle)) { var style = book.CharacterStyles[options.CharacterStyle] ?? throw new Exception($"Character style '{options.CharacterStyle}' does not exist."); var cp = document.BeginUpdateCharacters(range); try { cp.Style = style; } finally { document.EndUpdateCharacters(cp); } } if (!string.IsNullOrWhiteSpace(options.ParagraphStyle)) { var style = book.ParagraphStyles[options.ParagraphStyle] ?? throw new Exception($"Paragraph style '{options.ParagraphStyle}' does not exist."); var pp = document.BeginUpdateParagraphs(range); try { pp.Style = style; } finally { document.EndUpdateParagraphs(pp); } } if (options.ExpandFields) { ExpandFieldsInBookRange(range, Host?.Spreadsheet?.Workbook); } } finally { if (document != null) { switch (headerFooterKind) { case HeaderFooterKind.Header: section.EndUpdateHeader(document); break; case HeaderFooterKind.Footer: section.EndUpdateFooter(document); break; } } } }
protected virtual void SetupHeaderFooter(Document book) { Section section; int sectionNum = SectionNum ?? -1; if (sectionNum > 0 && sectionNum <= book.Sections.Count) { section = book.Sections[sectionNum - 1]; } else if (sectionNum < 0 && -sectionNum <= book.Sections.Count) { section = book.Sections[book.Sections.Count - (-sectionNum)]; } else { throw new Exception("Invalid SectionNum."); } SubDocument document = null; DocumentRange range; var type = Type; try { switch (HeaderFooter) { case DocumentType.Header: if (LinkToNext) { section.LinkHeaderToNext(type); } if (LinkToPrevious) { section.LinkHeaderToPrevious(type); } document = section.BeginUpdateHeader(type); break; case DocumentType.Footer: if (LinkToNext) { section.LinkFooterToNext(type); } if (LinkToPrevious) { section.LinkFooterToPrevious(type); } document = section.BeginUpdateFooter(type); break; default: throw new Exception("Invalid DocumentType."); } document.Delete(document.Range); if (Html) { //var insertOptions = string.IsNullOrWhiteSpace(ParagraphStyle) ? InsertOptions.KeepSourceFormatting : InsertOptions.UseDestinationStyles; var insertOptions = InsertOptions.KeepSourceFormatting; if (UseDestinationStyles) { insertOptions = InsertOptions.UseDestinationStyles; } if (KeepSourceFormatting) { insertOptions = InsertOptions.KeepSourceFormatting; } range = document.AppendHtmlText(Text, insertOptions); } else { range = document.AppendText(Text); } if (!string.IsNullOrWhiteSpace(CharacterStyle)) { var style = book.CharacterStyles[CharacterStyle] ?? throw new Exception($"Character style '{CharacterStyle}' does not exist."); var cp = document.BeginUpdateCharacters(range); try { cp.Style = style; } finally { document.EndUpdateCharacters(cp); } } if (!string.IsNullOrWhiteSpace(ParagraphStyle)) { var style = book.ParagraphStyles[ParagraphStyle] ?? throw new Exception($"Paragraph style '{ParagraphStyle}' does not exist."); var pp = document.BeginUpdateParagraphs(range); try { pp.Style = style; } finally { document.EndUpdateParagraphs(pp); } } if (ExpandFields) { ExpandFieldsInBookRange(range, HostSpreadsheet); } } finally { if (document != null) { switch (HeaderFooter) { case DocumentType.Header: section.EndUpdateHeader(document); break; case DocumentType.Footer: section.EndUpdateFooter(document); break; } } } }