In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to extract multimedia files inserted in Word", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to extract multimedia files inserted in Word".
In Word, files can be inserted into a document by embedding OLE objects, including Word, excel, PDF, PPT, pictures, macro files, file packages, and so on. The document objects that have been inserted in the document can also be extracted and saved to the specified path by the method in this article.
Dll file installation (3 methods)
1. Install dll via NuGet (2 ways)
You can open "solution Explorer" in Visual Studio, right-click "references", "manage NuGet packages", then search for "Free Spire.Doc" and click "install". Wait for the program to be installed.
1.2 copy the following to the PM console installation.
Install-Package FreeSpire.Doc-Version 9.9.7
two。 Manually add a dll reference
You can download the package to your local location manually, and then extract it to find the Spire.Doc.dll under the BIN folder. Then open solution Explorer in Visual Studio, right-click "reference" and "add reference" to add a reference to the dll file under the local path BIN folder.
Extract Fil
When extracting a file, it is mainly done through the following steps:
Create an object of the Document class and load the Word document through the Document.LoadFromFile () method.
Traverses all the sub-objects in the Section in the Word document to determine whether the object is of type Paragraph.
Iterate through the sub-objects in the paragraph to determine whether the object is an OLE object of type DocumentObjectType.
Converts eligible OLE objects to DocOleObject types through the as operator.
The object is extracted by the File.WriteAllBytes () method and saved to the local path.
Note: the Word test file and the extracted multimedia file path used in the following code is the Debug path of the VS program, such as: F:\ VS2017Project\ ExtractMediaFile\ bin\ Debug.
Complete code
Here is an example of the complete code:
C#using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.IO;namespace ExtractMediaFile {class Program {static void Main (string [] args) {/ / load Word document Document doc = new Document (); doc.LoadFromFile (@ "OLE.docx") / / traverse all sub-objects foreach (Section section in doc.Sections) {foreach (DocumentObject obj in section.Body.ChildObjects) {if (obj is Paragraph) {Paragraph paragraph = obj as Paragraph Foreach (DocumentObject obj2 in paragraph.ChildObjects) {if (obj2.DocumentObjectType = = DocumentObjectType.OleObject) {DocOleObject ole = obj2 as DocOleObject / / extract file File.WriteAllBytes (ole.PackageFileName, ole.NativeData) } VB.NETImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.IONamespace ExtractMediaFile Class Program Private Shared Sub Main (args As String ()) 'load Word document Dim Doc As New Document () doc.LoadFromFile ("test.docx") 'traverses all child objects For Each section As Section In doc.Sections For Each obj As DocumentObject In section.Body.ChildObjects If TypeOf obj Is Paragraph Then Dim paragraph As Paragraph = TryCast (obj Paragraph) For Each obj2 As DocumentObject In paragraph.ChildObjects If obj2.DocumentObjectType = DocumentObjectType.OleObject Then Dim ole As DocOleObject = TryCast (obj2, DocOleObject) 'extract file File.WriteAllBytes (ole.PackageFileName Ole.NativeData) End If Next End If Next Next End Sub End ClassEnd Namespace
The reading result of the file is shown in the figure:
The above is about the content of this article on "how to extract multimedia files inserted in Word by C#". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.