示例#1
0
        void AddFooterToDocument(DevExpress.XtraRichEdit.API.Native.Document document, string htmlText)
        {
            SubDocument doc = document.Sections[0].BeginUpdateFooter();

            doc.AppendHtmlText(htmlText);
            document.Sections[0].EndUpdateFooter(doc);
        }
        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;
                    }
                }
            }
        }
示例#3
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;
                    }
                }
            }
        }