การทำงานกับภาพ
Aspose.Wordsช่วยให้ผู้ใช้สามารถทำงานกับภาพในทางที่ยืดหยุ่นมาก ในบทความนี้คุณสามารถสำรวจเพียงบางส่วนของความเป็นไปได้ของการทำงานกับภาพ.
วิธีการแยกภาพจากเอกสาร
รูปภาพทั้งหมดจะถูกเก็บไว้ภายในShapeโหนดในDocument เมื่อต้องการแยกภาพหรือรูปภาพทั้งหมดที่มีชนิดเฉพาะจากเอกสารให้ทำตามขั้นตอนเหล่านี้:
- ใช้วิธีการGetChildNodesเพื่อเลือกโหนดShapeทั้งหมด.
- ย้ำผ่านคอลเลกชันโหนดที่เกิด.
- ตรวจสอบคุณสมบัติบูลีนHasImage.
- แยกข้อมูลภาพโดยใช้คุณสมบัติImageData.
- บันทึกข้อมูลภาพไปยังแฟ้ม.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแยกรูปภาพจากเอกสารและบันทึกเป็นแฟ้ม:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithImages(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithImages(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Image.SampleImages.doc"); | |
System::SharedPtr<NodeCollection> shapes = doc->GetChildNodes(NodeType::Shape, true); | |
int32_t imageIndex = 0; | |
for (System::SharedPtr<Shape> shape : System::IterateOver<System::SharedPtr<Shape>>(shapes)) | |
{ | |
if (shape->get_HasImage()) | |
{ | |
System::String imageFileName = System::String::Format(u"Image.ExportImages.{0}.{1}", imageIndex, FileFormatUtil::ImageTypeToExtension(shape->get_ImageData()->get_ImageType())); | |
System::String imagePath = outputDataDir + imageFileName; | |
shape->get_ImageData()->Save(imagePath); | |
std::cout << "Image saved at " << imagePath.ToUtf8String() << std::endl; | |
imageIndex++; | |
} | |
} |
บันทึกภาพเป็นWMF
Aspose.Wordsให้ฟังก์ชั่นเพื่อบันทึกรูปภาพทั้งหมดที่มีอยู่ในเอกสารไปที่ WMF รูปแบบขณะแปลงDOCXเป็นRTF.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการบันทึกภาพเป็นWMFด้วยRTFตัวเลือกการบันทึก:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc"); | |
System::SharedPtr<RtfSaveOptions> options = System::MakeObject<RtfSaveOptions>(); | |
options->set_SaveImagesAsWmf(true); | |
doc->Save(outputDataDir + u"WorkingWithRtfSaveOptions.SavingImagesAsWmf.rtf", options); |