이 빠른 자습서는 DOCX, DOC 등과 같은 **C#**을 사용하여 Word에서 빈 페이지를 제거하는 방법을 설명하기 위해 설계되었습니다. 작업 및 C#을 사용하여 Word의 빈 페이지를 지우는 전체 논리를 구현하는 샘플 코드. Windows, Linux 또는 macOS와 같은 운영 체제를 사용하여 타사 소프트웨어를 설치하지 않고도 이 코드를 실행할 수 있습니다.
C#을 사용하여 Word에서 빈 페이지를 삭제하는 단계
- 프로젝트에 Aspose.Words 라이브러리를 추가하도록 프로젝트 구성
- 소스 Word 파일을 Document 클래스 개체에 로드하여 빈 페이지 제거
- 소스 문서의 모든 페이지를 구문 분석하고 각 페이지를 개별적으로 가져옵니다.
- 원본 문서에서만 모든 단일 페이지로 별도의 문서 만들기
- 이 단일 페이지 문서를 분석하고 section에 일부 텍스트/모양 노드가 포함되어 있는지 확인하십시오.
- 원본 문서에서 비어 있지 않은 모든 페이지 목록 유지
- 비어 있지 않은 모든 페이지를 새 Word 파일에 추가
- 일부 텍스트 또는 모양이 있는 페이지만 있는 이 파일을 저장합니다.
이 단계에서는 C#*을 사용하여 Word에서 *빈 페이지를 삭제하는 세부 정보를 제공합니다. 먼저 원본 Word 파일을 로드한 다음 각 페이지에 대해 새 Word 문서가 만들어지도록 각 페이지를 구문 분석합니다. 각 페이지를 개별적으로 분석해야 하고 Document 클래스의 기능이 비어 있지 않은 페이지를 식별하는 데 도움이 되기 때문에 필요합니다. 구문 분석 중에 비어 있지 않은 각 페이지는 해당 페이지를 모두 포함하는 새 Word 파일에 추가됩니다.
C#을 사용하여 Word에서 빈 페이지를 삭제하는 코드
using System.Collections; | |
using Aspose.Words; | |
namespace DeleteABlankPageInWordUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to delete a blank page in Word using CSharp | |
{ | |
// Instantiate the license to avoid any trial version limitations | |
// and watermark in the output Word file | |
Aspose.Words.License licDeleteBlankPages= new Aspose.Words.License(); | |
licDeleteBlankPages.SetLicense("Aspose.Words.lic"); | |
// Load the Word file having blank pages in it | |
Document originalDocWithFewBlankPages = new Document("WordFileWithBlankPages.docx"); | |
// Declare an array to store page numbers of the empty or blank pages | |
ArrayList listOfBlankPageNumbers = new ArrayList(); | |
listOfBlankPageNumbers.Add(-1); | |
// Extract each page of the loaded document as a separate Word document | |
int totalPagesInOriginalDoc = originalDocWithFewBlankPages.PageCount; | |
for (int iCount = 0; iCount < totalPagesInOriginalDoc; iCount++) | |
{ | |
// Create a new Word document containing the single page | |
Document DocWithOnePage = originalDocWithFewBlankPages.ExtractPages(iCount, 1); | |
// Parse through all the sections of the newly created document | |
// to count text segments and shapes if any | |
int shapesCounter = 0; | |
string pageText = ""; | |
foreach (Section docSection in DocWithOnePage.Sections) | |
{ | |
// Extract text from the section and also get shapes count in the newly created document | |
pageText = pageText + docSection.Body.ToString(SaveFormat.Text); | |
shapesCounter += docSection.Body.GetChildNodes(NodeType.Shape, true).Count; | |
} | |
// Check if the extracted page is null or not and also check if shapes count is 0 or not | |
// If text is blank and shapes count is 0, it means the page is blank | |
if (string.IsNullOrEmpty(pageText.Trim()) && shapesCounter == 0) | |
listOfBlankPageNumbers.Add(iCount); | |
} | |
listOfBlankPageNumbers.Add(totalPagesInOriginalDoc); | |
// Add all the non-empty pages to the final document | |
Document nonEmptyDocument = (Document)originalDocWithFewBlankPages.Clone(false); | |
nonEmptyDocument.RemoveAllChildren(); | |
for (int iCount = 1; iCount < listOfBlankPageNumbers.Count; iCount++) | |
{ | |
int index = (int)listOfBlankPageNumbers[iCount - 1] + 1; | |
int count = (int)listOfBlankPageNumbers[iCount] - index; | |
if (count > 0) | |
nonEmptyDocument.AppendDocument(originalDocWithFewBlankPages.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting); | |
} | |
// Save the output file having all the non-empty pages | |
nonEmptyDocument.Save(@"NonEmptyPages.docx"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
*C#*을 사용하여 Word 문서에서 빈 페이지를 제거하는 이 코드는 원본 Word 파일의 각 페이지가 새 Word 파일에 저장된 다음 그 안에 있는 내용이 분석되는 전체 논리를 설명합니다. 이 과정에서 머리글과 바닥글은 고려되지 않지만 포함할 수 있습니다. 모든 페이지가 분석되고 해당 번호가 배열에 저장되면 이 배열을 사용하여 해당 페이지를 새 Word 파일에 추가합니다.
이 자습서에서는 Word 파일에서 모든 빈 페이지를 제거했습니다. Word 문서를 HTML로 변환하는 것과 같은 다른 기능에 대해 알아보려면 C#을 사용하여 Word 문서를 HTML로 변환하는 방법에 대한 문서를 참조하세요.