Advertisement
Pavle_nis

C# extract images from word document

Dec 2nd, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. private void getTitle(string path, Document document, DateTime startTime)
  2.         {
  3.             int index = 0;
  4.  
  5.             foreach (Spire.Doc.Section section in document.Sections)
  6.             {
  7.                 foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
  8.                 {
  9.                     if (paragraph.Text.StartsWith("Figure"))
  10.                     {
  11.                         string[] split = paragraph.Text.Split(' ');
  12.                         string title = split[0] + " " + split[1];
  13.  
  14.                         titles.Add(title);
  15.  
  16.                         index++;
  17.                         timeRemaining = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks * (section.Paragraphs.Count - (index + 1)) / (index + 1));
  18.                         estimatedTime = String.Format("{0}h:{1:D2}m:{2:D2}s", (int)timeRemaining.TotalHours, timeRemaining.Minutes, timeRemaining.Seconds);
  19.                         lblRemainingTime.Text = estimatedTime;
  20.                         lblRemainingTime.Update();
  21.  
  22.                         percentComplete = (int)Math.Round((double)(100 * section.Paragraphs.Count - index) / section.Paragraphs.Count);
  23.                         prgTimeRemaining.Value = percentComplete;
  24.                         lblProgress.Text = percentComplete.ToString() + " %";
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.  
  30.         private void extractImagesFromDocument(string path, Document document, DateTime startTime)
  31.         {
  32.             int index = 0;
  33.  
  34.             Queue containers = new Queue();
  35.             containers.Enqueue(document);
  36.  
  37.             while (containers.Count > 0)
  38.             {
  39.                 ICompositeObject container = (ICompositeObject)containers.Dequeue();
  40.                 DocumentObjectCollection docObjects = container.ChildObjects;
  41.                 foreach (DocumentObject docObject in docObjects)
  42.                 {
  43.                     if (docObject.DocumentObjectType == DocumentObjectType.Picture)
  44.                     {
  45.                         timeRemaining = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks * (titles.Count - (index + 1)) / (index + 1));
  46.                         estimatedTime = String.Format("{0}h:{1:D2}m:{2:D2}s", (int)timeRemaining.TotalHours, timeRemaining.Minutes, timeRemaining.Seconds);
  47.                         lblRemainingTime.Text = estimatedTime;
  48.                         lblRemainingTime.Update();
  49.  
  50.                         percentComplete = (int)Math.Round((double)(100 * titles.Count - index) / titles.Count);
  51.                         prgTimeRemaining.Value = percentComplete;
  52.                         lblProgress.Text = percentComplete.ToString() + " %";
  53.  
  54.                         DocPicture picture = docObject as DocPicture;
  55.  
  56.                         string imageTitle= titles[index];
  57.                         picture.Image.Save(imageTitle + ".png", ImageFormat.Png);
  58.                         index++;
  59.                     }
  60.                     else
  61.                     {
  62.                         if (docObject is ICompositeObject)
  63.                         {
  64.                             containers.Enqueue(docObject as ICompositeObject);
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.             MessageBox.Show("Titles list:" + titles.Count);
  70.             MessageBox.Show("Images count:" + index);
  71.         }
  72.  
  73.         private void btnSelectDocument1_Click(object sender, EventArgs e)
  74.         {
  75.             startTime = DateTime.Now;
  76.  
  77.             OpenFileDialog openFileDialog = new OpenFileDialog();
  78.             DialogResult result = openFileDialog.ShowDialog();
  79.  
  80.             if (result == DialogResult.OK)
  81.             {
  82.                 path = txtDocument1.Text = openFileDialog.FileName;
  83.                 extension = Path.GetExtension(path);
  84.  
  85.                 newPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
  86.                 fileName = newPath + " - Copy" + extension;
  87.  
  88.                 File.Copy(path, fileName);
  89.  
  90.                 Document document = new Document();
  91.                 document.LoadFromFile(path);
  92.  
  93.                 getTitle(fileName, document, startTime);
  94.                 extractImagesFromDocument(fileName, document, startTime);
  95.                 MessageBox.Show("Done");
  96.             }
  97.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement