コード例 #1
0
        public static void InsertPageNumber(Document someDocument)
        {
            for (int i = 1; i < someDocument.Sections.Count; i++)
            {
                Section currentSection = someDocument.Sections[i];

                SubDocument   myFooter = currentSection.BeginUpdateFooter();
                DocumentRange range    = myFooter.InsertText(myFooter.CreatePosition(0), " PAGE NUMBER ");
                Field         fld      = myFooter.Fields.Create(range.End, "PAGE");
                myFooter.Fields.Update();

                ParagraphProperties pp = myFooter.BeginUpdateParagraphs(myFooter.Range);
                pp.Alignment = ParagraphAlignment.Right;
                myFooter.EndUpdateParagraphs(pp);

                currentSection.EndUpdateFooter(myFooter);
            }
        }
        void biClickMe_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            #region #formatting
            DocumentRange range = richEditControl1.Document.Selection;

            SubDocument doc = range.BeginUpdateDocument();

            CharacterProperties charprop = doc.BeginUpdateCharacters(range);
            charprop.BackColor = Colors.Yellow;
            charprop.AllCaps   = true;
            doc.EndUpdateCharacters(charprop);

            ParagraphProperties parprop = doc.BeginUpdateParagraphs(range);
            parprop.Alignment = ParagraphAlignment.Center;
            doc.EndUpdateParagraphs(parprop);

            range.EndUpdateDocument(doc);
            #endregion #formatting
        }
        private void ApplyFormatToSelectedText()
        {
            DocumentRange targetSelectedRange = richEditControl.Document.Selection;

            richEditControl.BeginUpdate();
            SubDocument targetSubDocument = targetSelectedRange.BeginUpdateDocument();
            SubDocument subDocument       = sourceSelectedRange.BeginUpdateDocument();

            DevExpress.XtraRichEdit.API.Native.CharacterProperties targetCharactersProperties = targetSubDocument.BeginUpdateCharacters(targetSelectedRange);
            DevExpress.XtraRichEdit.API.Native.CharacterProperties sourceCharactersProperties = subDocument.BeginUpdateCharacters(sourceSelectedRange);
            targetCharactersProperties.Assign(sourceCharactersProperties);
            subDocument.EndUpdateCharacters(sourceCharactersProperties);
            targetSubDocument.EndUpdateCharacters(targetCharactersProperties);

            DevExpress.XtraRichEdit.API.Native.ParagraphProperties targetParagraphProperties = targetSubDocument.BeginUpdateParagraphs(targetSelectedRange);
            DevExpress.XtraRichEdit.API.Native.ParagraphProperties sourceParagraphProperties = subDocument.BeginUpdateParagraphs(sourceSelectedRange);
            targetParagraphProperties.Assign(sourceParagraphProperties);
            subDocument.EndUpdateParagraphs(sourceParagraphProperties);
            targetSubDocument.EndUpdateParagraphs(targetParagraphProperties);

            sourceSelectedRange.EndUpdateDocument(subDocument);
            targetSelectedRange.EndUpdateDocument(targetSubDocument);
            richEditControl.EndUpdate();
        }
コード例 #4
0
        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;
                    }
                }
            }
        }
コード例 #5
0
        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;
                    }
                }
            }
        }