무료 C++ API를 통한 동적 Word DOCX 파일 생성

오픈 소스 C++ DOCX 라이브러리를 사용하면 C++ 코드에서 직접 Word 문서(.docx 형식)를 생성할 수 있습니다. 표와 이미지 추가, 텍스트에 형식 및 스타일 적용 등을 지원합니다.

MiniDocx란?

C++ 개발자에게 Microsoft Word 문서를 프로그래밍 방식으로 다루는 것은 전통적으로 무거운 의존성이나 독점 소프트웨어 설치가 필요해 어려운 작업이었습니다. MiniDocx는 C++ 애플리케이션에서 직접 Word 문서를 생성할 수 있는 가볍고 최신적이며 사용자 친화적인 솔루션을 제공함으로써 이러한 상황을 변화시킵니다. 이 오픈 소스 라이브러리는 Microsoft Office나 WPS Office 설치 없이도 강력한 문서 조작 기능을 제공합니다. MiniDocx는 Word 문서 생성의 기본 요소를 포괄하는 필수 기능들을 포함하고 있습니다. 섹션, 단락, 풍부한 텍스트 서식, 표, 그림, 스타일 및 목록을 지원합니다. 이 포괄적인 기능 세트는 개발자가 전문적인 기준에 부합하는 복잡하고 잘 포맷된 문서를 만들 수 있게 합니다.

MiniDocx는 현대적인 오픈 소스 C++ 라이브러리로, 개발자가 Microsoft Word .docx 문서를 프로그래밍 방식으로 생성 및 조작할 수 있게 하며 Microsoft Word나 WPS Office 설치가 필요 없습니다. 라이브러리 구조는 최신 C++20 표준을 기반으로 하여 최신 언어 기능을 활용해 성능, 타입 안전성 및 코드 명료성을 향상시킵니다. MiniDocx의 눈에 띄는 특징 중 하나는 크로스 플랫폼 호환성입니다. 이 라이브러리는 Windows, Linux, macOS 운영 체제에서 원활하게 작동하여 여러 플랫폼에서 실행되는 애플리케이션을 개발하는 개발자에게 뛰어난 선택이 됩니다.

Previous Next

MiniDocx 시작하기

MiniDocx를 설치하는 권장 방법은 GitHub를 통한 설치입니다. 원활한 설치를 위해 다음 명령을 사용하십시오.

GitHub를 통해 MiniDocx 설치

git clone git@github.com:totravel/minidocx.git 
cd minidocx  
You can also download it directly from Aspose product page.

C++를 사용한 Word Docx 문서 만들기

오픈 소스 MiniDocx 라이브러리는 C++ 애플리케이션 내에서 Word Docx 문서를 쉽게 생성하고 조작할 수 있게 합니다. 이 라이브러리는 서식 적용, 텍스트, 표, 이미지 추가를 지원합니다. MiniDocx를 이해하는 가장 간단한 방법은 실용적인 예제를 통해서입니다. 서식이 적용된 텍스트가 포함된 기본 Word 문서를 만드는 것은 라이브러리의 직관적인 API 설계와 간단한 워크플로우를 보여줍니다.

C++ 라이브러리를 사용해 Word 문서를 만드는 방법은?

#include "minidocx/minidocx.hpp"
#include 

int main()
{
  using namespace md;
  try {
    Document doc;
    SectionPointer sect = doc.addSection();

    ParagraphPointer para = sect->addParagraph();
    para->prop_.align_ = Alignment::Centered;

    RichTextPointer rich = para->addRichText("Happy Chinese New Year!");
    rich->prop_.fontSize_ = 32;
    rich->prop_.color_ = "FF0000";

    doc.saveAs("a.docx");
  }
  catch (const Exception& ex) {
    std::cerr << ex.what() << std::endl;
  }
  return 0;
}

C++ 라이브러리를 통한 문서에 표 추가

표는 Word 문서에서 구조화된 데이터를 제시하는 데 필수적이며, MiniDocx는 포괄적인 표 생성 및 서식 기능을 제공합니다. 표는 행과 셀로 구성되며 각 셀은 텍스트, 서식 및 중첩 콘텐츠를 포함할 수 있습니다. 표를 만들기 위해서는 표 구조 정의, 행 추가, 셀에 내용 채우기, 서식 적용 등의 여러 단계가 필요합니다. 아래는 C++ 애플리케이션 내에서 서식이 적용된 표를 생성하는 방법을 보여주는 자세한 예제입니다.

C++ 라이브러리를 사용해 Word 문서에 서식 있는 표를 만드는 방법은?



#include "minidocx/minidocx.hpp"

int main()
{
  using namespace md;
  try {
    Document doc;
    SectionPointer sect = doc.addSection();

    // Create a table with 3 rows and 2 columns
    TablePointer table = sect->addTable(3, 2);

    // Access and populate cells
    // First row - header
    auto cell00 = table->cell(0, 0);
    auto para00 = cell00->addParagraph();
    auto text00 = para00->addRichText("Name");
    text00->prop_.bold_ = true;

    auto cell01 = table->cell(0, 1);
    auto para01 = cell01->addParagraph();
    auto text01 = para01->addRichText("Age");
    text01->prop_.bold_ = true;

    // Second row
    auto cell10 = table->cell(1, 0);
    auto para10 = cell10->addParagraph();
    para10->addRichText("Alice");

    auto cell11 = table->cell(1, 1);
    auto para11 = cell11->addParagraph();
    para11->addRichText("25");

    // Third row
    auto cell20 = table->cell(2, 0);
    auto para20 = cell20->addParagraph();
    para20->addRichText("Bob");

    auto cell21 = table->cell(2, 1);
    auto para21 = cell21->addParagraph();
    para21->addRichText("30");

    doc.saveAs("table_document.docx");
  }
  catch (const Exception& ex) {
    std::cerr << ex.what() << std::endl;
  }
  return 0;
}

Word DOCX 파일에 이미지와 사진 삽입

시각적 콘텐츠는 문서의 가독성 및 몰입도를 향상시켜 이미지 지원을 모든 문서 생성 라이브러리의 핵심 기능으로 만듭니다. MiniDocx는 개발자가 이미지 크기와 위치를 제어하면서 문서에 이미지를 삽입할 수 있게 합니다. 이미지 삽입 시 이미지 파일 경로를 지정하고 필요에 따라 크기를 설정합니다. 아래는 C++ 라이브러리를 사용해 Word 문서에 이미지를 삽입하는 예제입니다.

C++ 라이브러리를 사용해 Word 문서에 이미지를 삽입하는 방법은?

#include "minidocx/minidocx.hpp"

int main()
{
  using namespace md;
  try {
    Document doc;
    SectionPointer sect = doc.addSection();

    // Add a paragraph before the image
    ParagraphPointer para1 = sect->addParagraph();
    para1->addRichText("Below is an important diagram:");

    // Add a paragraph containing the image
    ParagraphPointer para2 = sect->addParagraph();
    para2->prop_.align_ = Alignment::Centered;

    // Insert the picture
    PicturePointer pic = para2->addPicture("path/to/image.png");
    pic->prop_.width_ = 400;
    pic->prop_.height_ = 300;

    // Add a paragraph after the image
    ParagraphPointer para3 = sect->addParagraph();
    para3->addRichText("Figure 1: Important visualization");

    doc.saveAs("document_with_image.docx");
  }
  catch (const Exception& ex) {
    std::cerr << ex.what() << std::endl;
  }
  return 0;
}

문서 섹션 및 페이지 레이아웃

섹션은 Word 문서 내 페이지 레이아웃 속성을 제어하는 프레임워크를 제공합니다. 각 섹션은 페이지 크기, 방향, 여백, 헤더, 푸터 등 서로 다른 페이지 설정을 가질 수 있습니다. 이러한 섹션 기반 구조는 다양한 페이지 구성을 가진 복잡한 문서 레이아웃을 가능하게 합니다. 여러 섹션을 사용하는 일반적인 사례는 가로 및 세로 페이지가 모두 포함된 문서를 만드는 것으로, 넓은 표나 차트가 가로 방향을 필요로 하는 보고서가 예시입니다. 아래는 C++ 애플리케이션 내에서 Word 문서의 여러 섹션을 다루는 예제입니다.

C++ 라이브러리를 사용해 Word 문서의 여러 섹션을 다루는 방법은?


#include "minidocx/minidocx.hpp"

int main()
{
  using namespace md;
  try {
    Document doc;

    // First section with portrait orientation
    SectionPointer sect1 = doc.addSection();
    sect1->prop_.orientation_ = Orientation::Portrait;
    sect1->prop_.pageWidth_ = 8.5 * 1440; // Letter size in twips
    sect1->prop_.pageHeight_ = 11 * 1440;

    ParagraphPointer para1 = sect1->addParagraph();
    para1->addRichText("This is content in portrait orientation.");

    // Second section with landscape orientation
    SectionPointer sect2 = doc.addSection();
    sect2->prop_.orientation_ = Orientation::Landscape;
    sect2->prop_.pageWidth_ = 11 * 1440;
    sect2->prop_.pageHeight_ = 8.5 * 1440;

    ParagraphPointer para2 = sect2->addParagraph();
    para2->addRichText("This content appears in landscape orientation.");

    doc.saveAs("multi_section_document.docx");
  }
  catch (const Exception& ex) {
    std::cerr << ex.what() << std::endl;
  }
  return 0;
}

 한국인