开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 5265|回复: 2

RichEditContol 多个文档合并

[复制链接]

0

精华

205

贡献

377

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
71
软币
1039
在线时间
89 小时
注册时间
2014-2-12
发表于 2014-11-6 09:57:52 | 显示全部楼层 |阅读模式
本帖最后由 cyh 于 2014-11-6 10:40 编辑

[C#] 纯文本查看 复制代码
 public static Document MergeDouments(List<DocPrintEntity> fileStrems, DocumentFormat docFormat, bool isReplaceSymbols)
        {
            RichEditDocumentServer targetServer = new RichEditDocumentServer();
            RichEditDocumentServer sourceServer = new RichEditDocumentServer();
            Document targetDoc = targetServer.Document;
            Document sourceDoc = sourceServer.Document;

            for (int i = 0; i < fileStrems.Count; i++)
            {
                sourceServer.LoadDocument(fileStrems[i].DocStream, docFormat);

                if (isReplaceSymbols)
                {
                    ReplaceDoumentSymbols(sourceServer);
                }

                if (i > 0 && fileStrems[i].isNewPage == "是")
                    targetDoc.AppendSection();

                targetDoc.Sections[targetDoc.Sections.Count - 1].UnlinkHeaderFromPrevious();
                targetDoc.Sections[targetDoc.Sections.Count - 1].UnlinkFooterFromPrevious();
                SectionsMerger.Append(sourceDoc, targetDoc);

                if (i == fileStrems.Count - 1)
                    return targetDoc;

                targetDoc.AppendSection();

            }

            return targetDoc;
        }

        public static void MergeDouments(RichEditControl docControl, List<DocPrintEntity> fileStrems, DocumentFormat docFormat, bool isReplaceSymbols)
        {
            docControl.LoadDocument(fileStrems[0].DocStream, docFormat);

            for (int i = 1; i < fileStrems.Count; i++)
            {
                if (fileStrems[i].isNewPage == "是")
                    docControl.Document.AppendSection();

                docControl.Document.AppendDocumentContent(fileStrems[i].DocStream, docFormat);
            }

            if (isReplaceSymbols)
            {
                ReplaceDoumentSymbols(docControl);
            }
        }

[C#] 纯文本查看 复制代码
public class SectionsMerger
    {
        public static void Append(Document source, Document target)
        {
            int lastSectionIndexBeforeAppending = target.Sections.Count - 1;
            int sourceSectionCount = source.Sections.Count;

            MemoryStream ms = new MemoryStream();
            source.SaveDocument(ms, DocumentFormat.Doc);
            target.AppendDocumentContent(ms, DocumentFormat.Doc);

            for (int i = 0; i < sourceSectionCount; i++)
            {
                Section sourceSection = source.Sections[i];
                Section targetSection = target.Sections[lastSectionIndexBeforeAppending + i];

                // Copy standard header/footer
                AppendHeader(sourceSection, targetSection, HeaderFooterType.Odd);
                AppendFooter(sourceSection, targetSection, HeaderFooterType.Odd);
            }
        }

        private static void AppendHeader(Section sourceSection, Section targetSection, HeaderFooterType headerFooterType)
        {
            SubDocument source = sourceSection.BeginUpdateHeader(headerFooterType);
            SubDocument target = targetSection.BeginUpdateHeader(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() - 2, 2);
            target.Delete(emptyParagraph);

            sourceSection.EndUpdateHeader(source);
            targetSection.EndUpdateFooter(target);
        }

        private static void AppendFooter(Section sourceSection, Section targetSection, HeaderFooterType headerFooterType)
        {
            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() - 2, 2);
            target.Delete(emptyParagraph);

            sourceSection.EndUpdateFooter(source);
            targetSection.EndUpdateFooter(target);
        }
    }

评分

参与人数 4贡献 +3 赞扬 +4 收起 理由
a3398200 + 1 赞一个
982071787 + 1
嘿黑丶 + 1
羽叶 + 3 + 1 赞一个

查看全部评分

回复

使用道具 举报

0

精华

5273

贡献

5986

赞扬

管理员

帖子
1172
软币
22465
在线时间
4450 小时
注册时间
2013-6-7

黄马甲

发表于 2014-12-17 20:06:40 | 显示全部楼层
感谢楼主分享,帖子之前被系统自动判定为垃圾帖子放到回收站了。刚发现 抱歉抱歉~!
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
29
软币
250
在线时间
27 小时
注册时间
2015-7-26
发表于 2015-8-10 14:41:50 | 显示全部楼层
好帖,先收藏,留下坐标,以便之后需要参考
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-11-23 20:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表