สร้างเอกสาร PDF หรือ Word ใน C++

สร้างเอกสารใหม่ในเกือบทุกรูปแบบโดยทางโปรแกรมโดยใช้ไลบรารี C++ ที่มีความเที่ยงตรงสูงของเรา

เมื่อใช้ API การเขียนโปรแกรมของเรา นักพัฒนา C++ สามารถสร้างเอกสารในรูปแบบ PDF, DOC, DOCX, HTML, EPUB และรูปแบบอื่นๆ อีกมากมายด้วยโค้ดเพียงไม่กี่บรรทัด

ดูข้อมูลโค้ด

สร้างเอกสารโดยใช้ C++

ด้วย API อันทรงพลังที่ให้มา นักพัฒนา C++ สามารถสร้างเอกสารในเกือบทุกรูปแบบ ในการดำเนินการนี้ คุณต้องทำตามขั้นตอนสองสามขั้นตอนโดยใช้ไลบรารี C++ ของเรา:

  1. เพิ่มชื่อไฟล์
  2. เริ่มสร้างเอกสารโดยใช้ C++
  3. บันทึกเอกสารที่สร้างในรูปแบบที่เลือก

เป็นที่น่าสังเกตว่าเอกสารเปล่าควรจะมีหนึ่งย่อหน้าในทางเทคนิค ดังนั้นเมื่อคุณสร้างเอกสารโดยทางโปรแกรม คุณจะได้โครงสร้างเอกสารพื้นฐานที่แน่นอน

โปรดทราบว่าคุณสามารถเพิ่มเนื้อหาลงในเอกสารที่สร้างขึ้นใหม่ได้ทันที ดังนั้น คุณจะได้ไม่เพียงแค่เอกสารเปล่า แต่ได้เอกสารที่มีเนื้อหาที่จำเป็น สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการแก้ไขเอกสาร โปรดดูที่หน้าการแก้ไข

สร้างเอกสารใน C++ โดยทางโปรแกรม

ไลบรารี C++ ที่ให้มาช่วยให้คุณสร้างเอกสารในรูปแบบที่สนับสนุนโดยทางโปรแกรม เช่น PDF, DOCX, DOC, RTF, ODT, EPUB, HTML และอื่นๆ

ลองใช้ฟังก์ชันอันทรงพลังของเรา และดูวิธีสร้างเอกสารในบางรูปแบบโดยใช้ตัวอย่างต่อไปนี้:

สร้างเอกสารใหม่โดยใช้ C++
เลือกรูปแบบเป้าหมายจากรายการ
รันโค้ด
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto font = builder->get_Font();
font->set_Name(u"Courier New");
font->set_Color(System::Drawing::Color::get_Blue());
font->set_Size(36);
font->set_HighlightColor(System::Drawing::Color::get_Yellow());

builder->Write(u"Morbi enim nunc faucibus a.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();

auto run = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed.");
auto para = doc->get_FirstSection()->get_Body()->get_FirstParagraph();
para->AppendChild(run);

auto comment = MakeObject<Comment>(doc, u"John Doe", u"JD", System::DateTime::get_Today());
comment->SetText(u"Quisque fringilla leo.");

auto commentRangeStart = MakeObject<CommentRangeStart>(doc, comment->get_Id());
auto commentRangeEnd = MakeObject<CommentRangeEnd>(doc, comment->get_Id());

run->get_ParentNode()->InsertBefore(commentRangeStart, run);
run->get_ParentNode()->InsertAfter(commentRangeEnd, run);
commentRangeEnd->get_ParentNode()->InsertAfter(comment, commentRangeEnd);

comment->AddReply(u"Jane Doe", u"JD", System::DateTime::get_Now(),
    u"Pellentesque vel sapien justo.");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. ");
auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a.");
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun);
doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun);

builder->MoveTo(secondRun);
builder->StartBookmark(u"Aspose bookmark");
// ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า
if (secondRun->get_NextSibling() != NULL)
    builder->MoveTo(secondRun->get_NextSibling());
else
    builder->MoveTo(secondRun->get_ParentParagraph());
builder->EndBookmark(u"Aspose bookmark");

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
using namespace Aspose::Words;

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

auto shape = builder->InsertChart(ChartType::Pie, 432, 252);
auto chart = shape->get_Chart();
chart->get_Title()->set_Text(u"Demo Chart");

chart->get_Series()->Clear();
chart->get_Series()->Add(u"Series 1",
    MakeArray<String>({ u"Category1", u"Category2", u"Category3" }),
    MakeArray<double>({ 2.7, 3.2, 0.8 }));

doc->Save(u"Output.docx");
using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); doc->Save(u"Output.docx"); using namespace Aspose::Words; auto doc = MakeObject<Document>(); auto builder = MakeObject<DocumentBuilder>(doc); auto firstRun = MakeObject<Run>(doc, u"Proin eros metus, sagittis sed. "); auto secondRun = MakeObject<Run>(doc, u"Morbi enim nunc faucibus a."); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(firstRun); doc->get_FirstSection()->get_Body()->get_FirstParagraph()->AppendChild(secondRun); builder->MoveTo(secondRun); builder->StartBookmark(u"Aspose bookmark"); // ถ้า NextSibling เป็นโมฆะ เป็นไปได้มากว่านี่คือ Run สุดท้ายในย่อหน้า if (secondRun->get_NextSibling() != NULL) builder->MoveTo(secondRun->get_NextSibling()); else builder->MoveTo(secondRun->get_ParentParagraph()); builder->EndBookmark(u"Aspose bookmark"); auto saveOptions = MakeObject<PdfSaveOptions>(); saveOptions->get_OutlineOptions()->get_BookmarksOutlineLevels()->Add(u"Aspose bookmark", 1); doc->Save(u"Output.docx", saveOptions);
รันโค้ด

วิธีทำเอกสารใน C++

  1. ติดตั้ง Aspose.Words for C++
  2. เพิ่มการอ้างอิงไลบรารี (นำเข้าไลบรารี) ไปยัง C++ โครงการของคุณ
  3. สร้างเอกสารใหม่
  4. เรียกเมธอด Save() โดยส่งชื่อไฟล์
  5. รับผลลัพธ์เป็นไฟล์แยกต่างหาก

C++ ห้องสมุดเพื่อสร้างเอกสาร

มีสามตัวเลือกในการติดตั้ง Aspose.Words for C++ ในสภาพแวดล้อมของนักพัฒนาซอฟต์แวร์ของคุณ โปรดเลือกหนึ่งรายการที่ตรงกับความต้องการของคุณและทำตามคำแนะนำทีละขั้นตอน:

ความต้องการของระบบ

คุณสามารถใช้ไลบรารี C++ นี้เพื่อพัฒนาซอฟต์แวร์บน Microsoft Windows, Linux และ macOS:

  • GCC >= 6.3.0 และ Clang >= 3.9.1 จำเป็นสำหรับ Linux
  • Xcode >= 12.5.1 Clang และ libc++ สำหรับ macOS

หากคุณพัฒนาซอฟต์แวร์สำหรับ Linux หรือ macOS โปรดตรวจสอบข้อมูลเกี่ยวกับการพึ่งพาไลบรารีเพิ่มเติม (แพ็คเกจโอเพ่นซอร์ส fontconfig และ mesa-glu) ในเอกสารประกอบผลิตภัณฑ์

รูปแบบไฟล์ยอดนิยม

5%

สมัครสมาชิก Aspose Product Updates

รับจดหมายข่าวและข้อเสนอรายเดือนที่ส่งตรงถึงกล่องจดหมายของคุณ