การทำงานกับ OLE Objects และวิดีโอออนไลน์

OLE (การเชื่อมโยงและการฝังวัตถุ) เป็นเทคโนโลยีที่ผู้ใช้สามารถทำงานกับเอกสารที่มี “วัตถุ” ที่สร้างหรือแก้ไขโดยแอปพลิเคชันบุคคลที่สาม นั่นคือ OLE อนุญาตให้แอปพลิเคชันแก้ไขส่งออก “ออบเจ็กต์” เหล่านี้ไปยังแอปพลิเคชันแก้ไขอื่น จากนั้นนำเข้าพร้อมเนื้อหาเพิ่มเติม

ในบทความนี้ เราจะพูดถึงการแทรกวัตถุ OLE และการตั้งค่าคุณสมบัติ รวมถึงการแทรกวิดีโอออนไลน์ลงในเอกสาร

แทรกวัตถุ OLE

ถ้าคุณต้องการ OLE Object ให้เรียกใช้เมธอด InsertOleObject และส่งผ่าน ProgId อย่างชัดเจนพร้อมกับพารามิเตอร์อื่นๆ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกวัตถุ OLE ลงในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertOleObject("http://www.aspose.com", "htmlfile", true, true, null);
dataDir = dataDir + "DocumentBuilderInsertOleObject_out.doc";
doc.Save(dataDir);

ตั้งชื่อไฟล์และนามสกุลเมื่อแทรกวัตถุ OLE

แพคเกจ OLE เป็นวิธีดั้งเดิมและ “ไม่มีเอกสาร” ในการจัดเก็บวัตถุฝังตัวหากไม่รู้จักตัวจัดการ OLE

Windows เวอร์ชันก่อนหน้า เช่น Windows 3.1, 95 และ 98 มีแอปพลิเคชัน Packager.exe ที่สามารถใช้เพื่อฝังข้อมูลประเภทใดก็ได้ลงในเอกสาร ขณะนี้แอปพลิเคชันนี้แยกออกจาก Windows แล้ว แต่ Microsoft Word และแอปพลิเคชันอื่นๆ ยังคงใช้เพื่อฝังข้อมูล หากตัวจัดการ OLE หายไปหรือไม่ทราบ คลาส OlePackage อนุญาตให้ผู้ใช้เข้าถึงคุณสมบัติ OLE Package

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งชื่อไฟล์ นามสกุล และชื่อที่แสดงสำหรับ OLE Package:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
byte[] bs = File.ReadAllBytes(dataDir + @"input.zip");
using (Stream stream = new MemoryStream(bs))
{
Shape shape = builder.InsertOleObject(stream, "Package", true, null);
OlePackage olePackage = shape.OleFormat.OlePackage;
olePackage.FileName = "filename.zip";
olePackage.DisplayName = "displayname.zip";
dataDir = dataDir + "DocumentBuilderInsertOleObjectOlePackage_out.doc";
doc.Save(dataDir);
}

เข้าถึงข้อมูลดิบของวัตถุ OLE

ผู้ใช้สามารถเข้าถึงข้อมูลวัตถุ OLE โดยใช้คุณสมบัติและวิธีการต่างๆ ของคลาส OleFormat ตัวอย่างเช่น คุณสามารถรับข้อมูลดิบของวัตถุ OLE หรือเส้นทางและชื่อของไฟล์ต้นฉบับสำหรับวัตถุ OLE ที่เชื่อมโยงได้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการรับข้อมูลดิบของวัตถุ OLE โดยใช้วิธี GetRawData:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load document with OLE object.
Document doc = new Document(dataDir + "DocumentBuilderInsertTextInputFormField_out.doc");
Shape oleShape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
byte[] oleRawData = oleShape.OleFormat.GetRawData();

แทรกวัตถุ OLE เป็นไอคอน

วัตถุ OLE ยังสามารถแทรกลงในเอกสารเป็นรูปภาพได้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกวัตถุ OLE เป็นไอคอน เพื่อจุดประสงค์นี้ คลาส DocumentBuilder เปิดเผยวิธีการ InsertOleObjectAsIcon:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file");
doc.Save(dataDir + "EmbeddeWithIcon_out.docx");
Console.WriteLine("The document has been saved with OLE Object as an Icon.");

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกวัตถุ OLE ที่ฝังตัวเป็นไอคอนจากกระแสข้อมูลลงในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file");
using (MemoryStream stream = new MemoryStream(File.ReadAllBytes(dataDir + "embedded.xlsx")))
builder.InsertOleObjectAsIcon(stream, "Package", dataDir + "icon.ico", "My embedded file");
doc.Save(dataDir + "EmbeddeWithIconUsingStream_out.docx");
Console.WriteLine("The document has been saved with OLE Object as an Icon.");

แทรกวิดีโอออนไลน์

สามารถแทรกวิดีโอออนไลน์ลงในเอกสาร Word ได้จากแท็บ “แทรก”> “วิดีโอออนไลน์” คุณสามารถแทรกวิดีโอออนไลน์ลงในเอกสารที่ตำแหน่งปัจจุบันได้โดยการเรียกวิธี InsertOnlineVideo

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

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแทรกวิดีโอออนไลน์จาก Vimeo ลงในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
//The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Pass direct url from youtu.be.
string url = "https://youtu.be/t_1LYZ102RA";
double width = 360;
double height = 270;
Shape shape = builder.InsertOnlineVideo(url, width, height);
dataDir = dataDir + "Insert.OnlineVideo_out_.docx";
doc.Save(dataDir);

การโอเวอร์โหลดครั้งที่สองทำงานร่วมกับทรัพยากรวิดีโออื่นๆ ทั้งหมด และใช้โค้ด HTML ที่ฝังไว้เป็นพารามิเตอร์ รหัส HTML สำหรับการฝังวิดีโออาจแตกต่างกันไปขึ้นอยู่กับผู้ให้บริการ ดังนั้นโปรดติดต่อผู้ให้บริการที่เกี่ยวข้องเพื่อขอรายละเอียด

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแทรกวิดีโอออนไลน์ลงในเอกสารโดยใช้โค้ด HTML ดังกล่าว:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
//The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithOnlineVideo();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Shape width/height.
double width = 360;
double height = 270;
// Poster frame image.
byte[] imageBytes = File.ReadAllBytes("TestImage.jpg");
// Visible url
string vimeoVideoUrl = @"https://vimeo.com/52477838";
// Embed Html code.
string vimeoEmbedCode = "";
builder.InsertOnlineVideo(vimeoVideoUrl, vimeoEmbedCode, imageBytes, width, height);
dataDir = dataDir + "Insert.OnlineVideo_out_.docx";
doc.Save(dataDir);