protected void DoWriteImage(string fileName, ImageOptions options) { options ??= new ImageOptions(); var book = options.Book?.Document ?? Document; using (new UsingProcessor(() => book.BeginUpdate(), () => book.EndUpdate())) { DocumentPosition rangeStart = null, rangeEnd = null; DocumentImage image; if (options.Image != null) { image = book.Images.Append(options.Image); } else if (!string.IsNullOrWhiteSpace(fileName)) { fileName = Project.Current.MapPath(fileName); var source = DocumentImageSource.FromFile(fileName); image = book.Images.Append(source); } else { throw new Exception("WriteImage - unknown input object."); } image.ScaleX = options.ScaleX; image.ScaleY = options.ScaleY; var imageRange = image.Range; if (rangeStart == null) { rangeStart = imageRange.Start; } if (!options.NoLineBreaks) { imageRange = book.AppendText(Environment.NewLine); } rangeEnd = imageRange.End; if (rangeStart != null && rangeEnd != null) { var range = book.CreateRange(rangeStart, rangeEnd.ToInt() - rangeStart.ToInt()); if (!string.IsNullOrWhiteSpace(options.ParagraphStyle) || options.FirstLineIdent.HasValue || options.FirstLineIndentType.HasValue || options.LeftIndent.HasValue || options.RightIndent.HasValue) { var pp = book.BeginUpdateParagraphs(range); try { if (!string.IsNullOrWhiteSpace(options.ParagraphStyle)) { var style = book.ParagraphStyles[options.ParagraphStyle] ?? throw new Exception($"Paragraph style '{options.ParagraphStyle}' does not exist."); pp.Style = style; } if (options.FirstLineIdent.HasValue) { pp.FirstLineIndent = options.FirstLineIdent; } if (options.FirstLineIndentType.HasValue) { pp.FirstLineIndentType = (DevExpress.XtraRichEdit.API.Native.ParagraphFirstLineIndent)options.FirstLineIndentType; } if (options.LeftIndent.HasValue) { pp.LeftIndent = options.LeftIndent; } if (options.RightIndent.HasValue) { pp.RightIndent = options.RightIndent; } } finally { book.EndUpdateParagraphs(pp); } } Script.Book.SCBook.AddComments(book, range, options); } if (rangeEnd != null) { book.CaretPosition = rangeEnd; ResetBookFormatting(book, rangeEnd); ScrollToCaret(); } } }
public SCBook WriteImage(string fileName, ImageOptions options = null) { ExecuteSynchronized(options, () => DoWriteImage(fileName, options)); return(this); }