<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      .NET操作Word/WPS打造專業文檔 - 頁面設置與打印控制完全指南

      本文將詳細介紹如何使用MudTools.OfficeInterop.Word庫來設置頁面參數、管理頁眉頁腳以及控制文檔打印。我們將深入探討從基礎的紙張設置到高級的分節頁面控制,從簡單的頁眉頁腳到復雜的多區域布局,以及如何精確控制文檔的打印輸出。最后,我們將通過一個實戰示例,創建一個具有專業格式的文檔模板,并演示如何進行打印設置,讓你真正掌握Word自動化處理的精髓。

      開源項目地址:https://gitee.com/mudtools/OfficeInterop
      項目官方網址:MudTools OfficeInterop

      頁面設置 (PageSetup Object)

      頁面設置是文檔格式化的重要組成部分,它決定了文檔在紙張上的布局方式。通過IWordPageSetup接口,我們可以控制頁面的各個方面,包括紙張大小、方向、頁邊距等。

      想要創建出專業、美觀的文檔,第一步就是要掌握頁面設置。無論是制作商務報告、學術論文還是其他類型的文檔,合適的頁面布局都是成功的關鍵。

      設置紙張大小、方向、頁邊距

      在Word文檔處理中,最常見的頁面設置需求是調整紙張大小、方向和頁邊距。這些設置直接影響文檔的外觀和可讀性。

      using MudTools.OfficeInterop;
      using MudTools.OfficeInterop.Word;
      using System;
      
      // 創建或打開文檔
      using var wordApp = WordFactory.BlankWorkbook();
      var document = wordApp.ActiveDocument;
      
      // 獲取頁面設置對象
      var pageSetup = document.Sections[1].PageSetup;
      
      // 設置紙張大小為A4
      pageSetup.PaperSize = WdPaperSize.wdPaperA4;
      
      // 設置頁面方向為橫向
      pageSetup.Orientation = WdOrientation.wdOrientLandscape;
      
      // 設置頁邊距(單位:磅)
      pageSetup.TopMargin = 72;     // 1英寸 = 72磅
      pageSetup.BottomMargin = 72;
      pageSetup.LeftMargin = 72;
      pageSetup.RightMargin = 72;
      
      // 或者使用頁面寬度和高度直接設置(單位:磅)
      pageSetup.PageWidth = 595;    // A4紙寬度
      pageSetup.PageHeight = 842;   // A4紙高度
      

      應用場景:創建標準化報告模板

      在企業環境中,通常需要創建符合公司標準的報告模板。這些模板需要遵循特定的頁面設置規范。

      using MudTools.OfficeInterop;
      using MudTools.OfficeInterop.Word;
      using System;
      
      // 報告模板生成器
      public class ReportTemplateGenerator
      {
          /// <summary>
          /// 創建標準化報告模板
          /// </summary>
          /// <param name="templateName">模板名稱</param>
          /// <param name="paperSize">紙張大小</param>
          /// <param name="isLandscape">是否橫向</param>
          public void CreateStandardReportTemplate(string templateName, WdPaperSize paperSize, bool isLandscape = false)
          {
              try
              {
                  // 創建新文檔
                  using var wordApp = WordFactory.BlankWorkbook();
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 遍歷所有節并設置頁面格式
                  foreach (IWordSection section in document.Sections)
                  {
                      var pageSetup = section.PageSetup;
                      
                      // 設置紙張大小
                      pageSetup.PaperSize = paperSize;
                      
                      // 設置頁面方向
                      pageSetup.Orientation = isLandscape ? 
                          WdOrientation.wdOrientLandscape : 
                          WdOrientation.wdOrientPortrait;
                      
                      // 設置標準頁邊距(上下1英寸,左右1.25英寸)
                      pageSetup.TopMargin = 72;      // 1英寸
                      pageSetup.BottomMargin = 72;
                      pageSetup.LeftMargin = 90;     // 1.25英寸
                      pageSetup.RightMargin = 90;
                      
                      // 設置裝訂線(如果需要)
                      pageSetup.Gutter = 36;         // 0.5英寸裝訂線
                      pageSetup.GutterPos = WdGutterStyle.wdGutterPosLeft;
                  }
                  
                  // 保存為模板文件
                  string outputPath = $@"C:\Templates\{templateName}.dotx";
                  document.SaveAs(outputPath, WdSaveFormat.wdFormatXMLTemplate);
                  document.Close();
                  
                  Console.WriteLine($"標準化報告模板已創建: {outputPath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"創建報告模板時發生錯誤: {ex.Message}");
              }
          }
          
          /// <summary>
          /// 為現有文檔應用標準頁面設置
          /// </summary>
          /// <param name="documentPath">文檔路徑</param>
          public void ApplyStandardPageSetup(string documentPath)
          {
              try
              {
                  // 打開現有文檔
                  using var wordApp = WordFactory.Open(documentPath);
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 應用標準頁面設置
                  foreach (IWordSection section in document.Sections)
                  {
                      var pageSetup = section.PageSetup;
                      
                      // 設置為A4紙張
                      pageSetup.PaperSize = WdPaperSize.wdPaperA4;
                      
                      // 設置縱向
                      pageSetup.Orientation = WdOrientation.wdOrientPortrait;
                      
                      // 設置標準頁邊距
                      pageSetup.TopMargin = 72;
                      pageSetup.BottomMargin = 72;
                      pageSetup.LeftMargin = 90;
                      pageSetup.RightMargin = 90;
                  }
                  
                  // 保存文檔
                  document.Save();
                  document.Close();
                  
                  Console.WriteLine($"已為文檔應用標準頁面設置: {documentPath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"應用頁面設置時發生錯誤: {ex.Message}");
              }
          }
      }
      

      高級頁面設置選項

      除了基本的頁面設置外,IWordPageSetup還提供了更多高級選項,如文本列、行號、裝訂線等。

      // 獲取頁面設置對象
      var pageSetup = document.Sections[1].PageSetup;
      
      // 設置文本列
      pageSetup.TextColumns.SetCount(2); // 設置為兩列
      pageSetup.TextColumns.Width = 200; // 設置列寬
      pageSetup.TextColumns.Spacing = 30; // 設置列間距
      
      // 設置行號
      pageSetup.LineNumbering.Active = true; // 啟用行號
      pageSetup.LineNumbering.RestartMode = WdNumberingRule.wdRestartContinuous; // 連續編號
      pageSetup.LineNumbering.DistanceFromText = 36; // 行號與文本距離
      
      // 設置裝訂線
      pageSetup.Gutter = 36; // 0.5英寸裝訂線
      pageSetup.GutterStyle = WdGutterStyleOld.wdGutterStyleLatin; // 裝訂線樣式
      pageSetup.GutterPos = WdGutterStyle.wdGutterPosLeft; // 裝訂線位置
      

      應用場景:創建學術論文模板

      學術論文通常有特定的格式要求,包括多列布局、行號等。

      // 學術論文模板生成器
      public class AcademicPaperTemplateGenerator
      {
          /// <summary>
          /// 創建學術論文模板
          /// </summary>
          /// <param name="templatePath">模板保存路徑</param>
          public void CreateAcademicPaperTemplate(string templatePath)
          {
              try
              {
                  // 創建新文檔
                  using var wordApp = WordFactory.BlankWorkbook();
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 設置整體頁面格式
                  var pageSetup = document.Sections[1].PageSetup;
                  
                  // A4紙張,縱向
                  pageSetup.PaperSize = WdPaperSize.wdPaperA4;
                  pageSetup.Orientation = WdOrientation.wdOrientPortrait;
                  
                  // 設置頁邊距
                  pageSetup.TopMargin = 72;      // 1英寸
                  pageSetup.BottomMargin = 72;
                  pageSetup.LeftMargin = 72;
                  pageSetup.RightMargin = 72;
                  
                  // 為正文部分設置兩列布局(通常用于摘要后的內容)
                  // 這里我們為第二節設置兩列(假設第一節是標題和摘要)
                  if (document.Sections.Count > 1)
                  {
                      var bodyPageSetup = document.Sections[2].PageSetup;
                      bodyPageSetup.TextColumns.SetCount(2); // 兩列
                      bodyPageSetup.TextColumns.EvenlySpaced = true;
                      bodyPageSetup.TextColumns.LineBetween = true; // 顯示分隔線
                  }
                  
                  // 為特定節啟用行號(如用于審稿的版本)
                  var reviewPageSetup = document.Sections[1].PageSetup;
                  reviewPageSetup.LineNumbering.Active = true;
                  reviewPageSetup.LineNumbering.RestartMode = WdNumberingRule.wdRestartContinuous;
                  reviewPageSetup.LineNumbering.DistanceFromText = 18; // 1/4英寸
                  
                  // 保存模板
                  document.SaveAs(templatePath, WdSaveFormat.wdFormatXMLTemplate);
                  document.Close();
                  
                  Console.WriteLine($"學術論文模板已創建: {templatePath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"創建學術論文模板時發生錯誤: {ex.Message}");
              }
          }
      }
      

      頁眉與頁腳 (HeadersFooters Collection)

      頁眉和頁腳是文檔中重要的組成部分,它們通常包含頁碼、文檔標題、日期等信息。通過IWordHeadersFootersIWordHeaderFooter接口,我們可以靈活地控制每個節的頁眉頁腳。

      專業的文檔不僅內容要精彩,外觀也要精致。頁眉頁腳就像文檔的"名片",不僅提供導航信息,還能增強文檔的專業性和一致性。通過巧妙地設計頁眉頁腳,你可以讓文檔在眾多普通文檔中脫穎而出。

      為不同節設置不同的頁眉頁腳

      在復雜文檔中,可能需要為不同節設置不同的頁眉頁腳。這在章節結構復雜的文檔中非常有用。

      using MudTools.OfficeInterop;
      using MudTools.OfficeInterop.Word;
      using System;
      
      // 打開或創建文檔
      using var wordApp = WordFactory.BlankWorkbook();
      var document = wordApp.ActiveDocument;
      
      // 為第一節設置頁眉頁腳
      var firstSection = document.Sections[1];
      
      // 設置首頁不同頁眉頁腳
      firstSection.PageSetup.DifferentFirstPageHeaderFooter = 1; // 1表示啟用
      
      // 設置奇偶頁不同頁眉頁腳
      firstSection.PageSetup.OddAndEvenPagesHeaderFooter = 1; // 1表示啟用
      
      // 設置首頁頁眉
      var firstHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
      firstHeader.Range.Text = "這是首頁頁眉\n";
      
      // 設置奇數頁頁眉
      var oddHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
      oddHeader.Range.Text = "這是奇數頁頁眉\n";
      
      // 設置偶數頁頁眉
      var evenHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages];
      evenHeader.Range.Text = "這是偶數頁頁眉\n";
      
      // 設置頁腳(所有頁相同)
      foreach (IWordSection section in document.Sections)
      {
          var footer = section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
          footer.Range.Text = "這是頁腳內容\n";
      }
      

      應用場景:創建多章節技術文檔

      技術文檔通常包含多個章節,每個章節可能需要不同的頁眉信息。

      // 技術文檔頁眉頁腳管理器
      public class TechnicalDocumentHeaderFooterManager
      {
          /// <summary>
          /// 為技術文檔設置頁眉頁腳
          /// </summary>
          /// <param name="document">Word文檔</param>
          /// <param name="documentTitle">文檔標題</param>
          public void SetupTechnicalDocumentHeadersFooters(IWordDocument document, string documentTitle)
          {
              try
              {
                  // 為所有節設置首頁不同
                  foreach (IWordSection section in document.Sections)
                  {
                      section.PageSetup.DifferentFirstPageHeaderFooter = 1;
                  }
                  
                  // 設置首頁頁眉(通常是文檔標題)
                  var firstSection = document.Sections[1];
                  var firstHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
                  firstHeader.Range.Text = documentTitle;
                  firstHeader.Range.Font.Name = "微軟雅黑";
                  firstHeader.Range.Font.Size = 16;
                  firstHeader.Range.Font.Bold = true;
                  firstHeader.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                  
                  // 為各節設置頁眉(顯示章節標題)
                  for (int i = 1; i <= document.Sections.Count; i++)
                  {
                      var section = document.Sections[i];
                      var header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                      
                      // 根據節號設置不同的頁眉內容
                      switch (i)
                      {
                          case 1:
                              header.Range.Text = "引言";
                              break;
                          case 2:
                              header.Range.Text = "系統架構";
                              break;
                          case 3:
                              header.Range.Text = "詳細設計";
                              break;
                          case 4:
                              header.Range.Text = "實施指南";
                              break;
                          default:
                              header.Range.Text = $"第{i}章";
                              break;
                      }
                      
                      // 格式化頁眉
                      header.Range.Font.Name = "微軟雅黑";
                      header.Range.Font.Size = 10;
                      header.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                  }
                  
                  // 設置頁腳(顯示頁碼)
                  foreach (IWordSection section in document.Sections)
                  {
                      var footer = section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                      
                      // 插入頁碼
                      footer.Range.Text = "第 ";
                      footer.Range.Font.Name = "微軟雅黑";
                      footer.Range.Font.Size = 9;
                      
                      // 添加頁碼域
                      var pageNumRange = footer.Range.Duplicate;
                      pageNumRange.Collapse(WdCollapseDirection.wdCollapseEnd);
                      pageNumRange.Fields.Add(pageNumRange, WdFieldType.wdFieldPage);
                      
                      var totalPagesRange = footer.Range.Duplicate;
                      totalPagesRange.Collapse(WdCollapseDirection.wdCollapseEnd);
                      totalPagesRange.Text = " 頁,共 ";
                      totalPagesRange.Fields.Add(totalPagesRange, WdFieldType.wdFieldNumPages);
                      totalPagesRange.Text += " 頁";
                      
                      // 設置頁腳居中對齊
                      footer.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"設置頁眉頁腳時發生錯誤: {ex.Message}");
              }
          }
      }
      

      在頁眉頁腳中插入頁碼、日期、公司Logo等信息

      頁眉頁腳中經常需要包含頁碼、日期、圖片等元素。MudTools.OfficeInterop.Word提供了相應的方法來處理這些內容。

      // 為文檔添加帶有頁碼和日期的頁腳
      var footer = document.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
      
      // 插入日期
      footer.Range.Text = "創建日期: ";
      footer.Range.Fields.Add(footer.Range, WdFieldType.wdFieldDate);
      footer.Range.Text += "     頁面: ";
      
      // 插入當前頁碼
      var pageRange = footer.Range.Duplicate;
      pageRange.Collapse(WdCollapseDirection.wdCollapseEnd);
      pageRange.Fields.Add(pageRange, WdFieldType.wdFieldPage);
      
      // 插入總頁數
      pageRange.Text += " / ";
      pageRange.Fields.Add(pageRange, WdFieldType.wdFieldNumPages);
      
      // 插入公司Logo
      var logoRange = footer.Range.Duplicate;
      logoRange.Collapse(WdCollapseDirection.wdCollapseEnd);
      var logoShape = logoRange.InlineShapes.AddPicture(@"C:\Images\CompanyLogo.png");
      logoShape.Width = 100;
      logoShape.Height = 30;
      

      應用場景:創建企業文檔模板

      企業文檔通常需要包含公司標識、頁碼等信息。

      // 企業文檔模板生成器
      public class CorporateDocumentTemplateGenerator
      {
          /// <summary>
          /// 創建企業文檔模板
          /// </summary>
          /// <param name="templatePath">模板路徑</param>
          /// <param name="companyLogoPath">公司Logo路徑</param>
          public void CreateCorporateDocumentTemplate(string templatePath, string companyLogoPath)
          {
              try
              {
                  // 創建新文檔
                  using var wordApp = WordFactory.BlankWorkbook();
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 為所有節設置首頁不同頁眉頁腳
                  foreach (IWordSection section in document.Sections)
                  {
                      section.PageSetup.DifferentFirstPageHeaderFooter = 1;
                  }
                  
                  // 設置首頁頁眉(包含公司Logo和文檔標題)
                  var firstSection = document.Sections[1];
                  var firstHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
                  
                  // 插入公司Logo
                  if (System.IO.File.Exists(companyLogoPath))
                  {
                      var logoShape = firstHeader.Range.InlineShapes.AddPicture(companyLogoPath);
                      logoShape.Width = 120;
                      logoShape.Height = 40;
                  }
                  
                  // 添加文檔標題占位符
                  var titleRange = firstHeader.Range.Duplicate;
                  titleRange.Collapse(WdCollapseDirection.wdCollapseEnd);
                  titleRange.Text = "\n[文檔標題]\n";
                  titleRange.Font.Name = "微軟雅黑";
                  titleRange.Font.Size = 18;
                  titleRange.Font.Bold = true;
                  titleRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                  
                  // 設置普通頁頁眉(僅包含公司名稱)
                  foreach (IWordSection section in document.Sections)
                  {
                      var header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                      header.Range.Text = "公司名稱\n";
                      header.Range.Font.Name = "微軟雅黑";
                      header.Range.Font.Size = 9;
                      header.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                  }
                  
                  // 設置頁腳(包含日期和頁碼)
                  foreach (IWordSection section in document.Sections)
                  {
                      var footer = section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                      
                      // 左側顯示公司信息
                      footer.Range.Text = "公司名稱 | 地址 | 電話\n";
                      footer.Range.Font.Name = "微軟雅黑";
                      footer.Range.Font.Size = 8;
                      
                      // 右側顯示日期和頁碼
                      var rightFooterRange = footer.Range.Duplicate;
                      rightFooterRange.Collapse(WdCollapseDirection.wdCollapseEnd);
                      rightFooterRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                      
                      // 插入日期
                      rightFooterRange.Text = "打印日期: ";
                      rightFooterRange.Fields.Add(rightFooterRange, WdFieldType.wdFieldDate);
                      rightFooterRange.Text += "     第 ";
                      
                      // 插入頁碼
                      rightFooterRange.Fields.Add(rightFooterRange, WdFieldType.wdFieldPage);
                      rightFooterRange.Text += " 頁";
                      
                      // 設置邊框
                      footer.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
                      footer.Range.Borders[WdBorderType.wdBorderTop].LineWidth = WdLineWidth.wdLineWidth050pt;
                  }
                  
                  // 保存模板
                  document.SaveAs(templatePath, WdSaveFormat.wdFormatXMLTemplate);
                  document.Close();
                  
                  Console.WriteLine($"企業文檔模板已創建: {templatePath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"創建企業文檔模板時發生錯誤: {ex.Message}");
              }
          }
      }
      

      打印文檔

      文檔打印是Word自動化處理的最后一步。通過PrintOut方法,我們可以控制文檔的打印行為,包括打印份數、范圍等。

      當你花費大量時間精心制作了一份文檔,最終的打印輸出卻出現問題,是不是很讓人沮喪?通過掌握精確的打印控制技巧,你可以確保文檔以最佳狀態呈現給讀者,避免因打印設置不當而導致的尷尬。

      使用 Document.PrintOut 方法及其參數控制打印份數、范圍等

      MudTools.OfficeInterop.Word提供了簡潔的PrintOut方法來控制文檔打印。

      using MudTools.OfficeInterop;
      using MudTools.OfficeInterop.Word;
      using System;
      
      // 打開文檔
      using var wordApp = WordFactory.Open(@"C:\Documents\MyDocument.docx");
      var document = wordApp.ActiveDocument;
      
      // 基本打印:打印一份完整文檔
      document.PrintOut();
      
      // 打印兩份完整文檔
      document.PrintOut(copies: 2);
      
      // 打印指定頁面(例如第3到第5頁)
      document.PrintOut(pages: "3-5");
      
      // 打印多個不連續頁面
      document.PrintOut(pages: "1,3,5-7");
      
      // 打印多份指定頁面
      document.PrintOut(copies: 3, pages: "1-2");
      

      應用場景:批量打印文檔

      在企業環境中,經常需要批量打印文檔,并可能需要不同的打印設置。

      // 批量文檔打印管理器
      public class BatchDocumentPrintManager
      {
          /// <summary>
          /// 批量打印文檔
          /// </summary>
          /// <param name="documentPaths">文檔路徑列表</param>
          /// <param name="copies">打印份數</param>
          /// <param name="pageRange">頁碼范圍</param>
          public void BatchPrintDocuments(List<string> documentPaths, int copies = 1, string pageRange = "")
          {
              foreach (var documentPath in documentPaths)
              {
                  try
                  {
                      // 打開文檔
                      using var wordApp = WordFactory.Open(documentPath);
                      var document = wordApp.ActiveDocument;
                      
                      // 隱藏Word應用程序以提高性能
                      wordApp.Visibility = WordAppVisibility.Hidden;
                      wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                      
                      // 打印文檔
                      document.PrintOut(copies: copies, pages: pageRange);
                      
                      // 關閉文檔
                      document.Close(false); // 不保存更改
                      
                      Console.WriteLine($"文檔已打印: {documentPath}");
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine($"打印文檔 {documentPath} 時發生錯誤: {ex.Message}");
                  }
              }
          }
          
          /// <summary>
          /// 根據文檔類型應用不同的打印設置
          /// </summary>
          /// <param name="documentPath">文檔路徑</param>
          /// <param name="documentType">文檔類型</param>
          public void PrintDocumentByType(string documentPath, string documentType)
          {
              try
              {
                  // 打開文檔
                  using var wordApp = WordFactory.Open(documentPath);
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 根據文檔類型應用不同的打印設置
                  switch (documentType.ToLower())
                  {
                      case "report":
                          // 報告類文檔:打印所有頁面,2份
                          document.PrintOut(copies: 2);
                          break;
                          
                      case "contract":
                          // 合同類文檔:打印所有頁面,1份
                          document.PrintOut(copies: 1);
                          break;
                          
                      case "invoice":
                          // 發票類文檔:只打印第一頁,2份
                          document.PrintOut(copies: 2, pages: "1");
                          break;
                          
                      case "manual":
                          // 手冊類文檔:打印指定頁面,1份
                          document.PrintOut(pages: "1-10");
                          break;
                          
                      default:
                          // 默認打印所有頁面,1份
                          document.PrintOut();
                          break;
                  }
                  
                  // 關閉文檔
                  document.Close(false);
                  
                  Console.WriteLine($"文檔已按{documentType}類型打印: {documentPath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"打印文檔時發生錯誤: {ex.Message}");
              }
          }
      }
      

      高級打印控制

      雖然MudTools.OfficeInterop.Word目前只提供了基本的打印參數,但在實際應用中,我們可以通過其他方式實現更精細的打印控制。

      // 文檔打印配置器
      public class DocumentPrintConfigurer
      {
          /// <summary>
          /// 配置并打印文檔
          /// </summary>
          /// <param name="documentPath">文檔路徑</param>
          /// <param name="printerName">打印機名稱</param>
          /// <param name="copies">打印份數</param>
          /// <param name="isCollated">是否逐份打印</param>
          public void ConfigureAndPrintDocument(string documentPath, string printerName, int copies, bool isCollated)
          {
              try
              {
                  // 打開文檔
                  using var wordApp = WordFactory.Open(documentPath);
                  var document = wordApp.ActiveDocument;
                  
                  // 設置打印機(如果指定)
                  if (!string.IsNullOrEmpty(printerName))
                  {
                      wordApp.ActivePrinter = printerName;
                  }
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 注意:當前版本的MudTools.OfficeInterop.Word只支持基本的打印參數
                  // 更高級的打印控制需要通過其他方式實現
                  
                  // 打印文檔
                  document.PrintOut(copies: copies);
                  
                  // 關閉文檔
                  document.Close(false);
                  
                  Console.WriteLine($"文檔已打印: {documentPath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"打印文檔時發生錯誤: {ex.Message}");
              }
          }
      }
      

      實戰:創建一個具有專業格式的文檔模板并演示打印設置

      現在,讓我們綜合運用前面學到的知識,創建一個具有專業格式的文檔模板,并演示如何進行頁面設置和打印控制。

      在實際工作中,我們經常需要創建符合公司標準的文檔模板,并能夠快速生成和打印文檔。通過下面的完整示例,你將學會如何創建一個真正實用的專業文檔模板,以及如何自動化整個文檔生成和打印流程。

      using MudTools.OfficeInterop;
      using MudTools.OfficeInterop.Word;
      using System;
      using System.Collections.Generic;
      
      // 專業文檔模板創建器
      public class ProfessionalDocumentTemplateCreator
      {
          /// <summary>
          /// 創建專業文檔模板
          /// </summary>
          /// <param name="templatePath">模板保存路徑</param>
          public void CreateProfessionalDocumentTemplate(string templatePath)
          {
              try
              {
                  // 創建新文檔
                  using var wordApp = WordFactory.BlankWorkbook();
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 設置頁面格式
                  SetupPageFormat(document);
                  
                  // 設置頁眉頁腳
                  SetupHeadersAndFooters(document);
                  
                  // 添加模板內容示例
                  AddTemplateContent(document);
                  
                  // 保存為模板
                  document.SaveAs(templatePath, WdSaveFormat.wdFormatXMLTemplate);
                  document.Close();
                  
                  Console.WriteLine($"專業文檔模板已創建: {templatePath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"創建專業文檔模板時發生錯誤: {ex.Message}");
              }
          }
          
          /// <summary>
          /// 設置頁面格式
          /// </summary>
          /// <param name="document">Word文檔</param>
          private void SetupPageFormat(IWordDocument document)
          {
              // 為所有節設置頁面格式
              foreach (IWordSection section in document.Sections)
              {
                  var pageSetup = section.PageSetup;
                  
                  // 設置A4紙張,縱向
                  pageSetup.PaperSize = WdPaperSize.wdPaperA4;
                  pageSetup.Orientation = WdOrientation.wdOrientPortrait;
                  
                  // 設置頁邊距(標準商業文檔)
                  pageSetup.TopMargin = 72;      // 1英寸
                  pageSetup.BottomMargin = 72;   // 1英寸
                  pageSetup.LeftMargin = 90;     // 1.25英寸
                  pageSetup.RightMargin = 90;    // 1.25英寸
                  
                  // 啟用首頁不同頁眉頁腳
                  section.PageSetup.DifferentFirstPageHeaderFooter = 1;
              }
          }
          
          /// <summary>
          /// 設置頁眉頁腳
          /// </summary>
          /// <param name="document">Word文檔</param>
          private void SetupHeadersAndFooters(IWordDocument document)
          {
              // 設置首頁頁眉
              var firstSection = document.Sections[1];
              var firstHeader = firstSection.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage];
              firstHeader.Range.Text = "專業文檔模板\n";
              firstHeader.Range.Font.Name = "微軟雅黑";
              firstHeader.Range.Font.Size = 20;
              firstHeader.Range.Font.Bold = true;
              firstHeader.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
              
              // 設置普通頁頁眉
              foreach (IWordSection section in document.Sections)
              {
                  var header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                  header.Range.Text = "文檔標題\n";
                  header.Range.Font.Name = "微軟雅黑";
                  header.Range.Font.Size = 12;
                  header.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
              }
              
              // 設置頁腳
              foreach (IWordSection section in document.Sections)
              {
                  var footer = section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
                  
                  // 左側顯示文檔信息
                  footer.Range.Text = "保密等級: 內部使用\n";
                  footer.Range.Font.Name = "微軟雅黑";
                  footer.Range.Font.Size = 8;
                  
                  // 右側顯示頁碼
                  var pageNumRange = footer.Range.Duplicate;
                  pageNumRange.Collapse(WdCollapseDirection.wdCollapseEnd);
                  pageNumRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                  pageNumRange.Text = "第 ";
                  pageNumRange.Fields.Add(pageNumRange, WdFieldType.wdFieldPage);
                  pageNumRange.Text += " 頁";
              }
          }
          
          /// <summary>
          /// 添加模板內容示例
          /// </summary>
          /// <param name="document">Word文檔</param>
          private void AddTemplateContent(IWordDocument document)
          {
              // 獲取文檔內容范圍
              var contentRange = document.Content;
              
              // 添加文檔標題
              contentRange.Text = "文檔標題\n";
              contentRange.Font.Name = "微軟雅黑";
              contentRange.Font.Size = 16;
              contentRange.Font.Bold = true;
              contentRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
              contentRange.ParagraphFormat.SpaceAfter = 12;
              
              // 添加文檔信息
              var infoRange = contentRange.Duplicate;
              infoRange.Collapse(WdCollapseDirection.wdCollapseEnd);
              infoRange.Text = "文檔編號: [編號]\n創建日期: [日期]\n版本: [版本號]\n\n";
              infoRange.Font.Name = "微軟雅黑";
              infoRange.Font.Size = 10;
              infoRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
              infoRange.ParagraphFormat.SpaceAfter = 6;
              
              // 添加目錄占位符
              var tocRange = infoRange.Duplicate;
              tocRange.Collapse(WdCollapseDirection.wdCollapseEnd);
              tocRange.Text = "目錄\n";
              tocRange.Font.Name = "微軟雅黑";
              tocRange.Font.Size = 14;
              tocRange.Font.Bold = true;
              tocRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
              tocRange.ParagraphFormat.SpaceAfter = 12;
              
              tocRange.Text += "[目錄內容]\n\n";
              tocRange.Font.Bold = false;
              tocRange.Font.Size = 12;
              tocRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
              
              // 添加正文內容示例
              var bodyRange = tocRange.Duplicate;
              bodyRange.Collapse(WdCollapseDirection.wdCollapseEnd);
              bodyRange.Text = "1. 引言\n\n";
              bodyRange.Font.Name = "微軟雅黑";
              bodyRange.Font.Size = 14;
              bodyRange.Font.Bold = true;
              bodyRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
              bodyRange.ParagraphFormat.SpaceAfter = 12;
              
              bodyRange.Text += "這是文檔正文內容的示例。在這里可以添加文檔的主要內容。\n\n";
              bodyRange.Font.Bold = false;
              bodyRange.Font.Size = 12;
              bodyRange.ParagraphFormat.FirstLineIndent = 28; // 首行縮進2字符
              bodyRange.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5; // 1.5倍行距
          }
          
          /// <summary>
          /// 使用模板創建并打印文檔
          /// </summary>
          /// <param name="templatePath">模板路徑</param>
          /// <param name="outputPath">輸出文檔路徑</param>
          /// <param name="printCopies">打印份數</param>
          public void CreateAndPrintDocument(string templatePath, string outputPath, int printCopies = 1)
          {
              try
              {
                  // 基于模板創建新文檔
                  using var wordApp = WordFactory.CreateFrom(templatePath);
                  var document = wordApp.ActiveDocument;
                  
                  // 隱藏Word應用程序以提高性能
                  wordApp.Visibility = WordAppVisibility.Hidden;
                  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                  
                  // 替換模板中的占位符
                  document.FindAndReplace("[編號]", "DOC-2023-001");
                  document.FindAndReplace("[日期]", DateTime.Now.ToString("yyyy-MM-dd"));
                  document.FindAndReplace("[版本號]", "1.0");
                  document.FindAndReplace("[目錄內容]", "1. 引言 .................... 1\n2. 主要內容 ................ 2\n3. 結論 .................... 3");
                  document.FindAndReplace("文檔標題", "2023年度技術報告");
                  
                  // 保存文檔
                  document.SaveAs(outputPath, WdSaveFormat.wdFormatXMLDocument);
                  
                  // 打印文檔
                  if (printCopies > 0)
                  {
                      document.PrintOut(copies: printCopies);
                      Console.WriteLine($"文檔已打印 {printCopies} 份");
                  }
                  
                  // 關閉文檔
                  document.Close();
                  
                  Console.WriteLine($"文檔已創建: {outputPath}");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"創建和打印文檔時發生錯誤: {ex.Message}");
              }
          }
      }
      
      // 使用示例
      class Program
      {
          static void Main(string[] args)
          {
              var creator = new ProfessionalDocumentTemplateCreator();
              
              // 創建專業文檔模板
              string templatePath = @"C:\Templates\ProfessionalDocumentTemplate.dotx";
              creator.CreateProfessionalDocumentTemplate(templatePath);
              
              // 使用模板創建并打印文檔
              string documentPath = @"C:\Documents\TechnicalReport.docx";
              creator.CreateAndPrintDocument(templatePath, documentPath, printCopies: 2);
              
              Console.WriteLine("操作完成!");
          }
      }
      
      posted @ 2025-09-25 14:54  玩泥巴的|mudtools.cn  閱讀(547)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 一区二区三区精品视频免费播放| 视频一区二区三区自拍偷拍| 18禁免费无码无遮挡网站| 最新亚洲人成网站在线影院| 宅男久久精品国产亚洲av麻豆| 宅男噜噜噜66在线观看| 亚欧洲乱码视频在线专区| 人妻精品动漫H无码中字| 拉孜县| 少妇被粗大的猛烈xx动态图| 日夜啪啪一区二区三区| 免费特黄夫妻生活片| 国产精品v欧美精品∨日韩| 色偷偷亚洲女人天堂观看| 国产高清不卡视频| 久久综合亚洲色一区二区三区| 日韩中文字幕免费在线观看| 成人午夜福利免费专区无码| 男人扒女人添高潮视频| 国产精品制服丝袜第一页| 黄色免费在线网址| 亚洲国产精品综合久久20| 亚洲成人四虎在线播放| 国产精品一久久香蕉国产线看观看 | 国产成人一区二区三区影院动漫| 日韩乱码人妻无码中文字幕视频| 国产精品福利午夜久久香蕉| 久久一本人碰碰人碰| 野花韩国高清电影| 黑人猛精品一区二区三区| 亚洲男人成人性天堂网站| 亚洲人成小说网站色在线 | 国产日韩av一区二区在线| XXXXXHD亚洲日本HD| 秋霞人妻无码中文字幕| 老子午夜精品无码| 国精偷拍一区二区三区| 色伦专区97中文字幕| 四虎影视国产精品永久在线| 金堂县| 最近免费中文字幕大全|