在C#中合并word文档时保持格式

问题描述 投票:1回答:1

我已经搜索过并且无法找到正确的解决方案。我有多个word文档,我希望合并。当我去合并时,我丢失了页面格式(单行间距和0pt表示前面的空格和后面的空格)当我合并时,这些设置会恢复到多行,然后返回8pt。所以,我创建了一个空白页面模板,但在合并后它仍然不会保持格式化。

    class Word_Merge
    {
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
{
    Word._Application wordApplication = new Word.Application();
    object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
    object outputFile = outputFilename;
    object fileName = @"S:\template.docx";
    object end = Word.WdCollapseDirection.wdCollapseEnd;

    try
    {
        wordApplication.Visible = false;
        Word.Document wordDocument = wordApplication.Documents.Add(ref 
        fileName);                
        Word.Selection selection = wordApplication.Selection;
        wordDocument.PageSetup.TopMargin = (float)50;
        wordDocument.PageSetup.RightMargin = (float)50;
        wordDocument.PageSetup.LeftMargin = (float)50;
        wordDocument.PageSetup.BottomMargin = (float)50;
        selection.ParagraphFormat.LineSpacingRule = 
        Word.WdLineSpacing.wdLineSpaceSingle;
        selection.ParagraphFormat.SpaceAfter = 0.0F;
        selection.ParagraphFormat.SpaceBefore = 0.0F;

        foreach (string file in filesToMerge)
        {
            if (file.Contains("Scores.docx"))
            {
                selection.PageSetup.PaperSize = 
                Word.WdPaperSize.wdPaper11x17;
                selection.Collapse(ref end);
            }
            selection.InsertFile(file);

            if (!file.Contains("Scores.docx") && insertPageBreaks)
            {
                selection.InsertBreak(pageBreak);
            }
        }

        wordDocument.SaveAs(ref outputFile);
        wordDocument = null;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        wordApplication.Quit();
    }
}

}

c# ms-word
1个回答
0
投票

我能够自己解决这个问题,这里的代码对我有用。

    class Word_Merge
{
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
    {
        Word._Application wordApplication = new Word.Application();
        object pageBreak = Word.WdBreakType.wdSectionBreakContinuous;
        object outputFile = outputFilename;
        object fileName = @"S:\ODS\Insight 2 Oncology\Quality Division Project\Visual Review Scores\Scoring Template\MergeTemplate.docx";
        object end = Word.WdCollapseDirection.wdCollapseEnd;

        try
        {
            wordApplication.Visible = false;
            Word.Document wordDocument = wordApplication.Documents.Add(ref fileName);                
            Word.Selection selection = wordApplication.Selection;


            foreach (string file in filesToMerge)
            {
                if (file.Contains("Scores.docx"))
                {
                    selection.PageSetup.PaperSize = Word.WdPaperSize.wdPaper11x17;
                    selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;                      
                    selection.InsertFile(file);
                    selection.Collapse(ref end);
                }                    

                if (!file.Contains("Scores.docx") && insertPageBreaks)
                {                        
                    selection.InsertFile(file);
                    selection.InsertBreak(pageBreak);
                }
            }

            wordApplication.Selection.Document.Content.Select();
            wordDocument.PageSetup.TopMargin = (float)50;
            wordDocument.PageSetup.RightMargin = (float)50;
            wordDocument.PageSetup.LeftMargin = (float)50;
            wordDocument.PageSetup.BottomMargin = (float)50;
            selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            selection.ParagraphFormat.SpaceAfter = 0.0F;
            selection.ParagraphFormat.SpaceBefore = 0.0F;

            wordDocument.SaveAs(outputFile + ".docx");
            wordDocument.Close();
            wordDocument = wordApplication.Documents.Open(outputFile + ".docx");
            wordDocument.ExportAsFixedFormat(outputFile + ".pdf", Word.WdExportFormat.wdExportFormatPDF);
            wordDocument = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            wordApplication.Quit();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.