C# Document Element Tutorial
IronWord 는 .NET C# 개발자가 Word 및 DOCX 문서를 생성, 읽기 및 편집하는 기능을 애플리케이션에 통합할 수 있도록 지원하는 강력한 Word 문서 라이브러리입니다. 워드 문서에서 문서 요소는 콘텐츠를 구성하는 기본 구성 요소입니다.
빠른 시작: 스타일이 적용된 텍스트런을 만들고 이미지를 삽입하세요
IronWord 사용하여 스타일이 적용된 텍스트와 삽입된 이미지를 문서에 결합하는 풍부한 콘텐츠를 추가하는 방법을 소개합니다. 이 예시는 스타일이 적용된 텍스트에 대한 Run 래퍼 패턴을 보여줍니다.
-
NuGet 패키지 관리자를 사용하여 https://www.nuget.org/packages/IronWord 설치하기
PM > Install-Package IronWord -
다음 코드 조각을 복사하여 실행하세요.
using IronWord; using IronWord.Models; // Create document WordDocument doc = new WordDocument(); // Create styled Run Run textRun = new Run(new TextContent("Hello IronWord!")); textRun.Style = new TextStyle() { IsBold = true, Color = Color.Blue }; // Create paragraph and add Run Paragraph paragraph = new Paragraph(); paragraph.AddChild(textRun); paragraph.AddImage(new ImageContent("pic.png")); // Add to document and save doc.AddParagraph(paragraph); doc.SaveAs("output.docx"); -
실제 운영 환경에서 테스트할 수 있도록 배포하세요.
무료 체험판으로 오늘 프로젝트에서 IronWord 사용 시작하기
목차
- 텍스트 추가
- 이미지 추가
- 이미지 불러오기 (파일 및 파일 스트림)
- 텍스트 줄 바꿈 설정
- 치수 설정 (가로 및 세로)
- 위치 오프셋 설정
- 모서리와의 거리 설정
핵심 개념
문서 계층 구조
IronWord 문서 → 단락 → 실행 → 텍스트 콘텐츠 와 같은 구조화된 문서 계층 구조를 따릅니다. 스타일이 적용된 텍스트를 다루려면 이러한 계층 구조를 이해하는 것이 필수적입니다.
WordDocument: 전체 문서를 나타내는 최상위 컨테이너Paragraph: 문서 내 콘텐츠의 블록Run:TextContent를 포함하고 스타일을 적용할 수 있는 래퍼 객체TextContent: 실제 텍스트 문자열
단락에 내용 추가하기
IronWord 단락에 내용을 추가하는 두 가지 방법을 제공합니다.
AddText(TextContent): 스타일이 적용되지 않은 일반 텍스트에 사용하세요. 단락에 직접TextContent를 추가합니다.AddChild(Run): 스타일이 적용된 텍스트에 사용하세요. 스타일을 포함하고 있는TextContent를 감싸는Run객체를 단락에 추가합니다.
폰트 크기, 색상, 굵은 글씨 서식 같은 스타일을 적용하려면, Run 객체를 생성하고 TextStyle을 Run에 할당한 후 AddChild을 사용하여 단락에 추가하세요.
TextRuns를 추가하세요
텍스트 내용
Split 메서드는 지정된 구분자를 기준으로 텍스트 런을 더 작은 텍스트 런들의 목록으로 나누기 위해 활용됩니다. 이를 통해 문서 내 텍스트 정보를 구성하고 조작할 수 있습니다.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
// Add text
TextContent addText = new TextContent("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
TextContent appendText = new TextContent("The first text.");
appendText.Append(new TextContent("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
TextContent splitText = new TextContent("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));
// Export docx
doc.SaveAs("textrun.docx");
Imports IronWord
Imports IronWord.Models
Private doc As New WordDocument()
' Add text
Private addText As New TextContent("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))
' Append text
Dim appendText As New TextContent("The first text.")
appendText.Append(New TextContent("The second text."))
doc.AddParagraph(New Paragraph(appendText))
' Split text
Dim splitText As New TextContent("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))
' Export docx
doc.SaveAs("textrun.docx")
스타일링 설정
텍스트에 스타일을 설정하기 위해서는 Run 래퍼 패턴을 사용해야 합니다. Run 객체를 생성하여 TextContent을 포함시키고, 그런 다음 TextStyle을 Run에 할당하세요 (TextContent에는 아님). TextStyle을 사용하면 FontSize, 색상, 굵게, 기울임꼴, 취소선, 밑줄, 위 첨자, 아래 첨자와 같은 시각적 속성을 정의할 수 있습니다.
FontSize는 TextStyle 수준에서 구성되는 반면, FontFamily은 TextFont 속성 내부에서 설정된다는 점을 유의하세요. 스타일이 적용된 후에는 AddChild을 사용하여 Run을 단락에 추가하세요. 이는 문서 계층 구조(문서 → 단락 → 실행 → 텍스트 콘텐츠)를 따릅니다.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Run textRun = new Run(new TextContent("Add text using IronWord"));
textRun.Style = new TextStyle()
{
FontSize = 72,
TextFont = new Font()
{
FontFamily = "Caveat"
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddChild(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
' Load docx
Dim doc As New WordDocument("document.docx")
' Configure text
Dim textRun As New Run(New TextContent("Add text using IronWord"))
textRun.Style = New TextStyle() With {
.FontSize = 72,
.TextFont = New Font() With {
.FontFamily = "Caveat"
},
.Color = Color.Red,
.IsBold = True,
.IsItalic = True,
.Underline = New Underline(),
.Strike = StrikeValue.Strike
}
Dim paragraph As New Paragraph()
' Add text
paragraph.AddChild(textRun)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
텍스트 채우기 색상 가져오기
IronWord 스타일 설정 외에도 기존 문서에서 RGBA 색상 값을 가져와 스타일의 일관성을 유지할 수 있는 방법을 제공합니다.
:path=/static-assets/word/content-code-examples/tutorials/rgba-color-value.cs
using IronWord;
using IronWord.Models;
using System;
// Open existing Word
WordDocument doc = new WordDocument("Accent1TextThemcolor.docx");
TextContent content = doc.Paragraphs[0].Texts[0];
// This will show the R G B A of the themecolor
var filledColor = content.Color;
// Print the filled color variable to the console
Console.WriteLine(filledColor);
Imports IronWord
Imports IronWord.Models
Imports System
' Open existing Word
Dim doc As New WordDocument("Accent1TextThemcolor.docx")
Dim content As TextContent = doc.Paragraphs(0).Texts(0)
' This will show the R G B A of the themecolor
Dim filledColor = content.Color
' Print the filled color variable to the console
Console.WriteLine(filledColor)
색상 값을 검색하기 위해, WordDocument을 사용하여 문서를 로드한 후 Paragraphs 컬렉션과 Texts 배열에 접근하여 TextContent 객체를 얻으세요. TextContent의 Color 속성은 텍스트의 기존 색상의 RGBA 값을 반환하여 문서 전체에서 일관된 스타일을 유지할 수 있습니다.
이미지 삽입
이 기능을 사용하면 콘텐츠 내에 이미지를 매끄럽게 삽입할 수 있어 문서의 전반적인 시각적 매력과 전달력을 향상시킬 수 있습니다.
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
TextContent textRun = new TextContent();
// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);
// Add paragraph
doc.AddParagraph(new Paragraph(textRun));
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
' Configure image
Private image As New ImageContent("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextContent()
' Add image
Dim para As New Paragraph(textRun)
para.AddImage(image)
' Add paragraph
doc.AddParagraph(New Paragraph(textRun))
' Export docx
doc.SaveAs("save_document.docx")
이미지 추가
이미지 불러오기
이미지 로딩은 매우 중요한 과정입니다. 이 과정에는 외부 이미지 파일을 문서 안으로 가져오는 작업이 포함됩니다. 이미지 업로드 기능은 관련 시각 자료를 쉽게 포함할 수 있도록 해주어 더욱 매력적이고 유익한 문서를 만드는 데 기여합니다.
:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage("image.jpg");
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument()
Private paragraph As New Paragraph()
' Add image
paragraph.AddImage("image.jpg")
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
이미지 구성
설정 가능한 옵션을 사용하여 이미지를 최적화하세요. 여기에는 텍스트 줄 바꿈, 크기, 위치 및 모서리와의 거리와 같은 속성 설정이 포함됩니다. 적절한 설정을 통해 이미지가 시각적으로 보기 좋고 맥락에 맞는 방식으로 표시될 수 있습니다.
:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
using IronSoftware.Abstractions.Word;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
ImageContent image = new ImageContent("image.jpg");
image.TextWrapBehavior = new TextWrapSquare();
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;
var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Imports IronSoftware.Abstractions.Word
' Load docx
Dim doc As New WordDocument()
' Configure image
Dim image As New ImageContent("image.jpg")
image.TextWrapBehavior = New TextWrapSquare()
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50
Dim position As New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("document.docx")
자주 묻는 질문
C#을 사용하여 Word 문서에 텍스트를 추가하려면 어떻게 해야 하나요?
IronWord를 사용하여 Word 문서에 텍스트 런 인스턴스를 생성하여 문서 내의 단락에 추가할 수 있습니다.
Word 문서에서 텍스트를 분할하는 방법은 무엇인가요?
IronWord는 Split 메서드를 제공하여 지정된 구분자를 기준으로 텍스트 런을 더 작은 세그먼트로 나눌 수 있어 텍스트 조작을 더욱 쉽게 합니다.
IronWord를 사용하여 Word 문서에서 텍스트를 스타일링하려면 어떻게 해야 하나요?
IronWord를 사용하여 글꼴 크기, 색상 및 볼드, 기울임꼴, 취소선, 밑줄, 위첨자, 아래첨자와 같은 스타일을 설정하여 텍스트를 스타일링할 수 있습니다.
C#을 사용하여 Word 문서에 이미지를 포함하려면 어떻게 해야 하나요?
IronWord를 사용하여 Word 문서에 이미지를 포함하는 경우 파일에서 이미지를 로드하고 문서 내의 단락에 인라인 이미지로 추가할 수 있습니다.
Word 문서에 이미지를 로드하는 단계는 무엇인가요?
IronWord를 사용하면 파일이나 파일 스트림에서 이미지를 로드할 수 있으며, 이를 통해 Word 문서에 시각적 콘텐츠를 통합할 수 있습니다.
Word 문서에서 이미지 속성을 어떻게 구성할 수 있나요?
IronWord를 사용하여 이미지 속성(text wrapping, dimensions, position offset, corners와의 거리)을 설정하여 문서 내에서 적절한 이미지 표시를 보장할 수 있습니다.
Word 문서의 텍스트에 대한 RGBA 색상 값을 검색할 수 있나요?
네, IronWord를 사용하여 문서 내의 기존 텍스트의 RGBA 색상 값을 얻을 수 있으며, 이를 통해 일정한 스타일을 유지할 수 있습니다.
IronWord를 사용하여 Word 문서 조작을 어떻게 시작하나요?
IronWord를 통해 .NET C# 애플리케이션에 통합하여 Word 문서를 생성, 읽기 및 편집할 수 있으며, 철저한 라이브러리 기능을 활용할 수 있습니다.

