VS 2022에서 프로그램으로 새 Word 문서 만들기(튜토리얼)
디지털 시대에 문서들은 점점 디지털화되고 있습니다. 비즈니스 환경에서 문서는 종종 Microsoft Word와 그 문서 기반의 Word 파일과 연관됩니다. Microsoft Word 문서를 작성하고 편집하는 것은 대규모 조직에서 정보를 전달하는 기초가 되었습니다. 그러나 문서를 수동으로 작성하고 편집하는 것은 힘든 과정이 될 수 있습니다; 프로그래밍적으로 Word 생성 자동화 역시 쉬운 일이 아닙니다. 많은 오픈 소스 라이브러리가 Microsoft Office Word에 의존하기 때문입니다.
그러나 Word 문서를 관리하고 생성하는 것이 이렇게 어려울 필요는 없습니다. IronWord는 Microsoft Office Word에 의존하지 않으면서 사용자가 전체 문서를 커스터마이즈할 수 있도록 하며, 프로그래밍 방식으로 문서 생성 및 문서 템플릿 생성을 허용하는 C# Word 라이브러리입니다.
오늘 튜토리얼에서는 IronWord 사용하여 Microsoft Word 문서를 프로그래밍 방식으로 생성하는 방법을 간략하게 설명하고 몇 가지 예제를 제공하겠습니다.
IronWord: C# Word 라이브러리
IronWord는 매우 신뢰할 수 있고 사용자 친화적인 C# Docx 라이브러리로, 개발자들이 C#을 사용하여 전통적인 종속성, 예를 들어 Microsoft Office나 Word Interop 없이 Word 문서를 구성하고 수정할 수 있게 해줍니다. 또한 광범위한 문서를 갖추고 있으며 .NET 8, 7, 6, Framework, Core, Azure를 완벽히 지원하여 대부분의 애플리케이션과 보편적으로 호환됩니다. 이러한 유연성 수준은 당신이 처리 중인 모든 애플리케이션에 적합한 선택이 됩니다.
환경 설정
이 예시에서는 Visual Studio를 사용하여 콘솔 앱을 생성하고 IronWord 라이브러리를 사용하여 빈 Word 문서를 생성하고 조작하는 방법을 시연합니다. 다음 단계 이전에, Visual Studio가 설치되어 있는지 확인하세요.
먼저 새 콘솔 앱을 만들어 보겠습니다.
그런 다음, 아래와 같이 프로젝트 이름을 제공하고 위치를 저장하세요.
마지막으로 원하는 프레임워크를 선택하세요.
C# Word 라이브러리 설치
빈 콘솔 앱을 만든 후에, NuGet 패키지 관리자를 통해 IronWord를 다운로드 받을 것입니다.
"NuGet 패키지 관리"를 클릭한 다음 아래에 보이는 것처럼 'Browse' 탭에서 IronWord를 검색하세요.
그 후, 새로 만든 프로젝트에 설치하세요.
또한, 명령줄에 다음 명령어를 입력하여 IronWord를 설치할 수 있습니다.
Install-Package IronWord
이제 모든 설정이 완료되었으니, Word 문서를 제작하는 예제로 넘어가겠습니다.
라이선스 키
IronWord는 작동을 위해 라이선스 키가 필요하다는 점을 기억하세요. 링크를 방문하여 무료 체험판의 일환으로 키를 얻을 수 있습니다.
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
체험 키를 받은 후에는 프로젝트에 이 변수를 설정하세요.
새로운 Word 문서 만들기
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Create a textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Create a textrun
Dim textRun As New Text("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)
' Create a new Word document with the paragraph
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
End Sub
End Class

설명
- 먼저, 샘플 코드에서 IronWord 라이브러리와 그 모델들을 가져옵니다.
- 우리는 새로운
Text변수를 생성합니다. 이 변수는 워드 문서에 텍스트를 추가할 수 있게 해줍니다. - 그런 다음 우리는
textRun을Paragraph에 추가합니다. 이로 인해 둘을 구분하여 텍스트를 쉽게 조작할 수 있게 됩니다. - 우리는 새로운 문서 객체를 생성하기 위해
paragraph매개변수를 새로운WordDocument클래스에 전달합니다. - 마지막으로, 우리는 문서를 'document.docx'로 워드 파일로 저장합니다.
이전 예제에서는 기본 텍스트로 워드 문서를 만들었습니다. 더 고급 기능으로 텍스트를 사용자 지정하고 텍스트 효과를 추가하는 예제를 해보겠습니다.
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
using IronWord;
using IronWord.Models;
class Program
{
static void Main()
{
// Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY";
// Load an existing docx
WordDocument doc = new WordDocument("document.docx");
// Create sample texts with styles
Text introText = new Text("This is an example paragraph with italic and bold styling.");
TextStyle italicStyle = new TextStyle()
{
IsItalic = true
};
Text italicText = new Text("Italic example sentence.");
italicText.Style = italicStyle;
TextStyle boldStyle = new TextStyle()
{
IsBold = true
};
Text boldText = new Text("Bold example sentence.");
boldText.Style = boldStyle;
// Create a paragraph and add texts
Paragraph paragraph = new Paragraph();
paragraph.AddText(introText);
paragraph.AddText(italicText);
paragraph.AddText(boldText);
// Add paragraph to the document
doc.AddParagraph(paragraph);
// Export the document
doc.SaveAs("save_document.docx");
}
}
Imports IronWord
Imports IronWord.Models
Friend Class Program
Shared Sub Main()
' Ensure to set up the license key
IronWord.License.LicenseKey = "YOUR-KEY"
' Load an existing docx
Dim doc As New WordDocument("document.docx")
' Create sample texts with styles
Dim introText As New Text("This is an example paragraph with italic and bold styling.")
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New Text("Italic example sentence.")
italicText.Style = italicStyle
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New Text("Bold example sentence.")
boldText.Style = boldStyle
' Create a paragraph and add texts
Dim paragraph As New Paragraph()
paragraph.AddText(introText)
paragraph.AddText(italicText)
paragraph.AddText(boldText)
' Add paragraph to the document
doc.AddParagraph(paragraph)
' Export the document
doc.SaveAs("save_document.docx")
End Sub
End Class

코드 설명
- 위의 예제와 같이, 우리는
Text객체를 생성하고 문서의 샘플 텍스트를 추가합니다. - 그런 다음 우리는 새로운
TextStyle객체를 생성하고 속성IsItalic를true로 할당하여 텍스트가 이탤릭체로 표시되도록 합니다. - 우리는
boldText변수에 대해서도 동일하게 수행하여 속성IsBold에true를 할당합니다. - 그런 다음 우리는 각
TextStyle변수를 각각의Text변수에 할당합니다. - 우리는
Paragraph변수를 생성하고 이탤릭체와 굵은 텍스트를 추가하기 위해AddText메소드를 호출합니다. - 우리는
AddParagraph메소드를 사용하여 문서에 단락을 추가합니다. - 마지막으로 문서를 'save_document.docx'로 저장합니다.
위의 예제는 개발자가 IronWord를 사용하여 사용할 수 있는 글꼴 및 스타일을 보여줍니다. 이탤릭체 및 볼드체 텍스트 효과 외에도, IronWord는 개발자가 다양한 시나리오에서 독특한 워드 문서를 생성할 수 있는 모든 옵션을 제공하도록 다른 기능도 제공합니다.
결론

우리의 시연은 IronWord 라이브러리를 사용하여 C#에서 프로그래밍으로 워드 문서를 만드는 것이 얼마나 쉬운지를 보여주었습니다. 라이브러리의 유연성과 확장성은 실제 사례 시나리오에서 텍스트, 글꼴 및 스타일을 사용자 정의하는 것과 같은 작업을 위해 개발자에게 유용한 도구로 만듭니다. 워드가 다른 애플리케이션과 통합되는 방법을 이해하면 개발자에게 그들의 도전에 대한 추가 솔루션을 제공합니다.
IronWord는 무료 체험 라이센스를 제공합니다.
자주 묻는 질문
C#에서 Microsoft Word 문서를 프로그램적으로 생성하는 방법은 무엇인가요?
IronWord 라이브러리를 사용하면 C#에서 Microsoft Word 문서를 프로그래밍 방식으로 생성할 수 있습니다. WordDocument 객체를 초기화하고 필요한 내용을 추가한 다음 SaveAs 메서드를 사용하여 .docx 파일로 내보낼 수 있습니다.
Word 문서 작성 시 Microsoft Office Interop 대신 IronWord 사용하는 것의 장점은 무엇입니까?
IronWord Microsoft Office Interop에 비해 여러 가지 이점을 제공합니다. Microsoft Office 설치가 필요하지 않으므로 Office에 의존하지 않고 다양한 플랫폼에서 더 큰 유연성과 손쉬운 통합을 제공합니다.
Visual Studio에 IronWord 어떻게 설치하나요?
Visual Studio에 IronWord 설치하려면 NuGet 패키지 관리자를 열고 IronWord 검색하여 프로젝트에 설치하세요. 또는 명령줄에서 Install-Package IronWord 사용할 수도 있습니다.
IronWord 사용하여 Word 문서의 텍스트 서식을 지정할 수 있습니까?
네, IronWord 사용하면 TextStyle 개체를 활용하여 텍스트 요소에 기울임꼴이나 굵게와 같은 다양한 스타일을 적용함으로써 Word 문서의 텍스트 서식을 지정할 수 있습니다.
IronWord .NET 및 Azure와 호환됩니까?
네, IronWord 는 .NET 8, 7, 6, Framework, Core 및 Azure와 완벽하게 호환되므로 다양한 애플리케이션에 활용할 수 있습니다.
IronWord 설치 문제를 어떻게 해결할 수 있나요?
IronWord 설치에 문제가 발생하는 경우, Visual Studio 및 .NET 의 호환 버전을 사용하고 있는지, 그리고 NuGet 패키지 관리자가 최신 버전인지 확인하십시오. 인터넷 연결 상태를 확인하고 설치를 다시 시도하십시오.
IronWord 사용하여 Word 문서를 만들기 시작하려면 무엇이 필요합니까?
IronWord 사용하여 Word 문서를 생성하려면 먼저 IronWord 라이브러리와 해당 모델을 C# 프로젝트로 가져옵니다. 그런 다음 WordDocument 객체를 초기화하여 문서 생성을 시작합니다.
IronWord 사용하여 Word 문서에 단락을 프로그램적으로 추가하는 방법은 무엇입니까?
IronWord 사용하면 Paragraph 개체를 만들고, 텍스트를 추가하고, WordDocument 에 포함시켜 Word 문서에 단락을 프로그래밍 방식으로 추가할 수 있습니다.



