本帖最后由 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);
}
}
|