การทำงานกับวัตถุOle

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

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

แทรกOleวัตถุ

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObject("http://www.aspose.com", "htmlfile", true, true, null);
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObject.docx");

ตั้งชื่อไฟล์และนามสกุลเมื่อใส่วัตถุ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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
byte[] bs = FileUtils.readFileToByteArray(new File(getMyDir() + "Zip file.zip"));
try (ByteArrayInputStream stream = new ByteArrayInputStream(bs))
{
Shape shape = builder.insertOleObject(stream, "Package", true, null);
OlePackage olePackage = shape.getOleFormat().getOlePackage();
olePackage.setFileName("filename.zip");
olePackage.setDisplayName("displayname.zip");
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectWithOlePackage.docx");
}

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Shape oleShape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
byte[] oleRawData = oleShape.getOleFormat().getRawData();

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObjectAsIcon(getMyDir() + "Presentation.pptx", false, getImagesDir() + "Logo icon.ico",
"My embedded file");
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIcon.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
try(ByteArrayInputStream stream = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(getMyDir() + "Presentation.pptx"))))
{
builder.insertOleObjectAsIcon(stream, "Package", getImagesDir() + "Logo icon.ico", "My embedded file");
}
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectAsIconUsingStream.docx");

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOnlineVideo("https://youtu.be/t_1LYZ102RA", 360.0, 270.0);
// We can watch the video from Microsoft Word by clicking on the shape.
doc.save(getArtifactsDir() + "DocumentBuilder.InsertVideoWithUrl.docx");

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
String videoUrl = "https://vimeo.com/52477838";
String videoEmbedCode = "<iframe src=\"https://player.vimeo.com/video/52477838\" width=\"640\" height=\"360\" frameborder=\"0\" " +
"title=\"Aspose\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
byte[] thumbnailImageBytes = IOUtils.toByteArray(getAsposelogoUri().toURL().openStream());
BufferedImage image = ImageIO.read(new ByteArrayInputStream(thumbnailImageBytes));
// Below are two ways of creating a shape with a custom thumbnail, which links to an online video
// that will play when we click on the shape in Microsoft Word.
// 1 - Insert an inline shape at the builder's node insertion cursor:
builder.insertOnlineVideo(videoUrl, videoEmbedCode, thumbnailImageBytes, image.getWidth(), image.getHeight());
doc.save(getArtifactsDir() + "DocumentBuilder.InsertOnlineVideoCustomThumbnail.docx");