我已经搜索过,但无法找到合适的解决方案。我有多个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();
}
}
}
答案 0 :(得分: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();
}
}
}