C#을 사용하여 Word 문서를 만드는 방법

이 자습서에서는 **C#**을 사용하여 Word 문서를 만들고, 다른 글꼴 형식으로 텍스트를 작성하고, 하이퍼링크를 삽입하고, 문서를 DOCX 파일 형식으로 저장하는 방법을 배웁니다.

다음 단계에서는 C#이 Word 문서 파일을 만드는 방법을 설명합니다.

C#을 사용하여 Word 문서를 만드는 단계

  1. NuGet에서 Aspose.Words for .NET 패키지 설치
  2. 참조 Aspose.Words 및 Aspose.Words.Saving 네임스페이스 추가
  3. DocumentDocumentBuilder 클래스의 인스턴스 생성
  4. 일부 텍스트를 작성하고 문서에 하이퍼링크 삽입
  5. 문서를 DOCX 파일 형식으로 저장

이전에 C#을 사용하여 DOCX에 머리글과 바닥글을 삽입하는 방법에 대해 살펴보았습니다. C#의 다음 코드 예제는 잘 알려진 DOCX 파일 형식으로 Word 문서를 생성합니다. Document.Save 메소드를 사용하여 DOC, RTF 등과 같은 다양한 파일 형식으로 Word 문서를 생성하는 데 동일한 접근 방식을 사용할 수 있습니다. 이 코드 예제는 .NET이 설치된 곳에서 사용할 수 있습니다.

C#을 사용하여 Word 문서를 생성하는 코드

using System.Drawing;
using Aspose.Words;
using Aspose.Words.Saving;
namespace KBCodeExamples
{
class how_to_create_word_document_using_c_sharp
{
public static void Main(string[] args)
{
//Set Aspose license before creating blank Word document
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License();
AsposeWordsLicense.SetLicense(@"Aspose.Words.lic");
// Create a blank Word document
Document doc = new Document();
// Initialize a new instance of DocumentBuilder class
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a string surrounded by a border
builder.Font.Border.Color = Color.Green;
builder.Font.Border.LineWidth = 2.5d;
builder.Font.Border.LineStyle = LineStyle.DashDotStroker;
builder.Write("Text surrounded by green border.");
// Remove all font formatting specified explicitly
builder.Font.ClearFormatting();
builder.InsertBreak(BreakType.ParagraphBreak);
builder.Write("For more information, please visit the ");
// Insert a hyperlink and emphasize it with custom formatting
// The hyperlink will be a clickable piece of text which will take us to the location specified in the URL
builder.Font.Color = Color.Blue;
builder.Font.Underline = Underline.Single;
builder.InsertHyperlink("Aspose Knowledge Base", "https://kb.aspose.com/", false);
builder.Font.ClearFormatting();
builder.Writeln(".");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions
{
Compliance = OoxmlCompliance.Iso29500_2008_Strict,
SaveFormat = SaveFormat.Docx
};
// Save the document with strict compliance level
doc.Save("create word document using C#.docx", saveOptions);
}
}
}

c#의 위 코드 예제는 Word 문서를 처음부터 만듭니다. 문서 클래스는 빈 Word 문서를 나타냅니다. DocumentBuilder를 Document와 연결해야 합니다. DocumentBuilder를 사용하여 표, 이미지, 텍스트 등과 같은 다양한 유형의 콘텐츠를 Word 문서에 삽입할 수 있습니다.

 한국인