/// <summary> /// 提取word中的图片 /// </summary> /// <param name="filePath">word文件路径</param> /// <param name="savePath">保存文件路径</param> /// <returns></returns> public static IEnumerable <string> ExportImageFromWordFile(string filePath, string savePath = "") { if (!File.Exists(filePath)) { yield return(string.Empty); } if (string.IsNullOrEmpty(savePath)) { savePath = $"{AppDomain.CurrentDomain.BaseDirectory}\\Temp\\"; } //加载word Document doc = new Document(filePath); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; Shape shape; for (int i = 0; i < shapes.Count; i++) { shape = shapes[i] as Shape; if (shape.HasImage) { //扩展名 string ex = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType); //文件名 string fileName = $"{imageIndex + 1}{ex}"; shape.ImageData.Save(savePath + fileName); yield return(fileName); imageIndex++; } } }
public void ExtractImagesToFiles() { //ExStart //ExFor:Shape //ExFor:Shape.ImageData //ExFor:Shape.HasImage //ExFor:ImageData //ExFor:FileFormatUtil.ImageTypeToExtension(ImageType) //ExFor:ImageData.ImageType //ExFor:ImageData.Save(String) //ExFor:CompositeNode.GetChildNodes(NodeType, bool) //ExSummary:Shows how to extract images from a document and save them as files. Document doc = new Document(MyDir + "Images.docx"); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage)); int imageIndex = 0; foreach (Shape shape in shapes.OfType <Shape>()) { if (shape.HasImage) { string imageFileName = $"File.ExtractImagesToFiles.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}"; shape.ImageData.Save(ArtifactsDir + imageFileName); imageIndex++; } } //ExEnd Assert.AreEqual(9, Directory.GetFiles(ArtifactsDir). Count(s => Regex.IsMatch(s, @"^.+\.(jpeg|png|emf|wmf)$") && s.StartsWith(ArtifactsDir + "File.ExtractImagesToFiles"))); }
public void ExtractImageFromWordDocumentFeature() { Document doc = new Document(MyDir + "Extract image.docx"); // Save the document to memory and reload it. using (MemoryStream stream = new MemoryStream()) { doc.Save(stream, SaveFormat.Doc); Document doc2 = new Document(stream); // "Shape" nodes that have the "HasImage" flag set contain and display images. IEnumerable <Shape> shapes = doc2.GetChildNodes(NodeType.Shape, true) .OfType <Shape>().Where(s => s.HasImage); int imageIndex = 0; foreach (Shape shape in shapes) { string imageFileName = $"Image.ExportImages.{imageIndex}_Aspose.Words_{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}"; shape.ImageData.Save(ArtifactsDir + imageFileName); imageIndex++; } } }
static void Main(string[] args) { string FilePath = @"..\..\..\..\Sample Files\"; string File = FilePath + "Extract Image - Aspose.docx"; Document doc = new Document(File); // Save document as DOC in memory MemoryStream stream = new MemoryStream(); doc.Save(stream, SaveFormat.Doc); // Reload document as DOC to extract images. Document doc2 = new Document(stream); NodeCollection shapes = doc2.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes) { if (shape.HasImage) { string imageFileName = string.Format( "Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); shape.ImageData.Save(FilePath + imageFileName); imageIndex++; } } }
static void Main(string[] args) { // Check for license and apply if exists string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic"; if (File.Exists(licenseFile)) { // Apply Aspose.Words API License Aspose.Words.License license = new Aspose.Words.License(); // Place license file in Bin/Debug/ Folder license.SetLicense("Aspose.Words.lic"); } string filePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\data\" + "Extract Images from Word Document.doc"; Document wordDocument = new Document(filePath); NodeCollection pictures = wordDocument.GetChildNodes(NodeType.Shape, true); int imageindex = 0; foreach (Shape shape in pictures) { string imageType = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType); if (shape.HasImage) { string imageFileName = "Aspose_" + (imageindex++).ToString() + "_" + shape.Name + imageType; shape.ImageData.Save(imageFileName); } } }
[Test] //ExSkip public void ExtractImagesToFiles() { Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Image.SampleImages.doc"); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes) { if (shape.HasImage) { string imageFileName = string.Format( "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); shape.ImageData.Save(MyDir + imageFileName); imageIndex++; } } }
[Test] //ExSkip public void ExtractImagesToFiles() { Document doc = new Document(MyDir + "Image.SampleImages.doc"); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes.OfType <Shape>()) { if (shape.HasImage) { string imageFileName = $"Image.ExportImages.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}"; shape.ImageData.Save(ArtifactsDir + imageFileName); imageIndex++; } } }
public void ExtractImages() { //ExStart //ExFor:Shape //ExFor:Shape.ImageData //ExFor:Shape.HasImage //ExFor:ImageData //ExFor:FileFormatUtil.ImageTypeToExtension(ImageType) //ExFor:ImageData.ImageType //ExFor:ImageData.Save(String) //ExFor:CompositeNode.GetChildNodes(NodeType, bool) //ExSummary:Shows how to extract images from a document, and save them to the local file system as individual files. Document doc = new Document(MyDir + "Images.docx"); // Get the collection of shapes from the document, // and save the image data of every shape with an image as a file to the local file system. NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); Assert.AreEqual(9, shapes.Count(s => ((Shape)s).HasImage)); int imageIndex = 0; foreach (Shape shape in shapes.OfType <Shape>()) { if (shape.HasImage) { // The image data of shapes may contain images of many possible image formats. // We can determine a file extension for each image automatically, based on its format. string imageFileName = $"File.ExtractImages.{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}"; shape.ImageData.Save(ArtifactsDir + imageFileName); imageIndex++; } } //ExEnd Assert.AreEqual(9, Directory.GetFiles(ArtifactsDir). Count(s => Regex.IsMatch(s, @"^.+\.(jpeg|png|emf|wmf)$") && s.StartsWith(ArtifactsDir + "File.ExtractImages"))); }
public static void Run() { //ExStart:ExtractImagesToFiles // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithImages(); Document doc = new Document(dataDir + "Image.SampleImages.doc"); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes) { if (shape.HasImage) { string imageFileName = string.Format( "Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); shape.ImageData.Save(dataDir + imageFileName); imageIndex++; } } //ExEnd:ExtractImagesToFiles Console.WriteLine("\nAll images extracted from document."); }
public void test() { string fileName = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "二衬检测.docx"); Document doc = new Document(fileName); var marksList = doc.Range.Bookmarks; var pageHeader = marksList["Image1"]; Node next = pageHeader.BookmarkStart.NextSibling; Console.WriteLine(next.GetType()); Shape shape = next as Shape; var savePath = AppDomain.CurrentDomain.BaseDirectory; string time = DateTime.Now.ToString("HHmmssfff"); //扩展名 string ex = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType); //文件名 string filename = savePath + "\\images"; if (!Directory.Exists(filename)) //如果该文件夹不存在就建立这个新文件夹 { Directory.CreateDirectory(filename); } else { Directory.Delete(filename, true); Directory.CreateDirectory(filename); } string file_Name = string.Format("{0}_{1}{2}", time, 0, ex); shape.ImageData.Save(Path.Combine(filename + "\\", file_Name)); //Console.WriteLine(pageHeader.GetType()); //var text = pageHeader.Text; //Console.WriteLine(pageHeader.Text); var table0 = doc.GetChildNodes(NodeType.Table, true)[5] as Aspose.Words.Tables.Table; Cell cell = table0.Rows[0].Cells[2]; Console.WriteLine(cell.GetText()); Console.WriteLine(table0.FirstRow.Cells[2].GetText()); //List<string> list = new List<string>(); //var savePath = AppDomain.CurrentDomain.BaseDirectory; //NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); //int imageIndex = 0; //foreach (Shape shape in shapes) //{ // if (shape.HasImage) // { // string time = DateTime.Now.ToString("HHmmssfff"); // //扩展名 // string ex = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType); // //文件名 // string filename = savePath + "\\images"; // if (!Directory.Exists(filename)) //如果该文件夹不存在就建立这个新文件夹 // { // Directory.CreateDirectory(filename); // }else // { // Directory.Delete(filename, true); // Directory.CreateDirectory(filename); // } // string file_Name = string.Format("{0}_{1}{2}", time, imageIndex, ex); // shape.ImageData.Save(Path.Combine(filename + "\\", file_Name)); // //添加文件到集合 // list.Add(file_Name); // imageIndex++; // } //} //NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); //foreach (Shape shape in shapes) //{ // Console.WriteLine(shape.Name); // if(shape.Name == "Image1") // { // Console.WriteLine(shape.Name); // } //} //odc.Save(fileName, SaveFormat.Docx); }