About Me

My photo
Ireland
Hello, my name is Cathal Coffey. I am best described as a hybrid between a developer and an adventurer. When I am not behind a keyboard coding, I am hiking and climbing the beautiful mountains of my home country Ireland. I am a full time student studying Computer Science & Software Engineering at the National University of Ireland Maynooth. I am finishing the final year of a 4 year degree in September 2009. I am the creator of an open source project on codeplex.com called DocX. At the moment I spend a lot of my free time advancing DocX and I enjoy this very much. My aim is to build a community around DocX and add features based on requests from this community. I really enjoy hearing about how people are using DocX in their work\personal projects. So if you are one of these people, please send me an email. Cathal coffey.cathal@gmail.com

Thursday, December 23, 2010

Programmatically manipulate an Image imbedded inside a DocX document

This example uses DocX to write the text “Hello World” into an Image embedded inside a .docx document.

Note: Make sure the document Input.docx exists and that it contains an Image.

Below is an example Input.docx and Output.docx

image

image

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Novacode;
  7. using System.Drawing;
  8. using System.Threading.Tasks;
  9. using System.IO;
  10. using System.Diagnostics;
  11. using System.Drawing.Imaging;
  12. namespace testDocX
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             // Open the document Input.docx.
  19.             using (DocX document = DocX.Load("Input.docx"))
  20.             {
  21.                 // Make sure this document has at least one Image.
  22.                 if (document.Images.Count() > 0)
  23.                 {
  24.                     Novacode.Image img = document.Images[0];
  25.                     CoolExample(img, "Hello World");
  26.                 }
  27.  
  28.                 else
  29.                     Console.WriteLine("The provided document contains no Images.");
  30.  
  31.                 // Save this document as Output.docx.
  32.                 document.SaveAs("Output.docx");
  33.             }
  34.         }
  35.  
  36.         // Write the given string into this Image.
  37.         private static void CoolExample(Novacode.Image img, string str)
  38.         {
  39.             // Write "Hello World" into this Image.
  40.             Bitmap b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite));
  41.  
  42.             /*
  43.              * Get the Graphics object for this Bitmap.
  44.              * The Graphics object provides functions for drawing.
  45.              */
  46.             Graphics g = Graphics.FromImage(b);
  47.  
  48.             // Draw the string "Hello World".
  49.             g.DrawString
  50.             (
  51.                 str,
  52.                 new Font("Tahoma", 20),
  53.                 Brushes.Blue,
  54.                 new PointF(0, 0)
  55.             );
  56.  
  57.             // Save this Bitmap back into the document using a Create\Write stream.
  58.             b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
  59.         }
  60.     }
  61. }

6 comments:

  1. Hi, i appreciate DocX library, it saved my ... day many times. Your samples has given me very good directions many times, and this is one of those days.
    I see the blog is 2 years silent and i though... no more ? You left docx/dotnet/programming ?
    Or you transferred to another link i don't know ?

    However, thanks for your posts, even 2 year later they're very very useful

    ReplyDelete
  2. I simlpy love you right now..

    ReplyDelete
  3. Hi,
    How to merge several files into one?
    Or how to start recording Paragraph with a new list?

    ReplyDelete
  4. Hi,
    How to save many word template into one?

    ReplyDelete
  5. Hi, my requirement is little different. I want to replace {placeholder} with Image(s). How can I achieve this using DocX library?

    ReplyDelete