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

Wednesday, April 8, 2009

DocX version 1.0.0.2 released

Note: Code samples have been updated to work with DocX version 1.0.0.6.

Lots of people emailed me asking me to add support for Images in DocX. So I did. Version 1.0.0.2 which can be downloaded from here adds basic support for Images and Pictures.

Images and Pictures? Are they not the same thing you might ask? No they are not! You can think of a Picture as a customized view of an Image. Once you have added Images to a docx file.

image

You can then add multiple customized Pictures of those Images into Paragraphs. You can do all sorts of interesting customizations with these Pictures before inserting them into Paragraphs. At the moment docx supports the following Picture customizations;

Rotations and transformations

image image image

Resizing

image image image

Shaping

image image image

image image image

Time for an example using DocX; below is a file entitled Example.docx.

image Figure 1.0 – Example.docx before code execution

The following code adds the Image Donkey.jpg into this file, it then creates two Pictures using this Image and inserts them into the last Paragraph.

// Create a .docx file
using (DocX document = DocX.Create(@"Example.docx"))
{
// Add an Image to the docx file
Novacode.Image img = document.AddImage(@"Donkey.jpg");

// Insert an emptyParagraph into this document.
Paragraph p = document.InsertParagraph("", false);

#region pic1
Picture pic1 = p.InsertPicture(img.Id, "Donkey", "Taken on Omey island");

// Set the Picture pic1’s shape
pic1.SetPictureShape(BasicShapes.cube);

// Rotate the Picture pic1 clockwise by 30 degrees
pic1.Rotation = 30;
#endregion

#region
pic2
// Create a Picture. A Picture is a customized view of an Image
Picture pic2 = p.InsertPicture(img.Id, "Donkey", "Taken on Omey island");

// Set the Picture pic2’s shape
pic2.SetPictureShape(CalloutShapes.cloudCallout);

// Flip the Picture pic2 horizontally
pic2.FlipHorizontal = true;
#endregion

// Save the docx file
document.Save();
}// Release this document from memory.
image

Figure 1.1 – Example.docx after code execution

Another interesting thing that you could do would be to apply an operation to every Picture in a document. The below snippet would rotate every Picture in a document clockwise by 30 degrees.

// Load the document that you want to manipulate
using (DocX document = DocX.Load(@"Test.docx"))
{
// Loop through each Paragraph
foreach (Paragraph p in document.Paragraphs)
{
// Loop through each Picture in this Paragraph
foreach (Picture pi in p.Pictures)
{
// Rotate this picture clockwise by 30 degrees
pi.Rotation = 30;
}
}

// Save the document
document.Save();
}// Release this document from memory
If you would like to send me feedback on DocX, or if you would like to make a suggestion for the next feature I implement, please email me at coffey.cathal@gmail.com.

happy coding,
Cathal

10 comments:

  1. Hi. Compiler throws error: No overload for method 'InsertPicture' takes 3 arguments on: Picture pic1 = p.InsertPicture(img.Id, "Donkey", "Taken on Omey island");

    Since this post is 2 years old I'm guessing the code is out of date. Can you post the correct code? Thanks for your work on this DLL.

    ReplyDelete
  2. This is the code I used instead. It works, but let me know if this is not suggested use. Hope this helps other people out.

    using (DocX document = DocX.Create(@"Example.docx"))
    {
    // Add an Image to the docx file
    Novacode.Image img = document.AddImage(
    @"\images\blurry-cars.jpg");

    // Insert an emptyParagraph into this document.
    Paragraph p = document.InsertParagraph("", false);

    #region pic1
    Picture pic1 = img.CreatePicture();

    // Set the Picture pic1’s shape
    pic1.SetPictureShape(BasicShapes.cube);

    // Rotate the Picture pic1 clockwise by 30 degrees
    pic1.Rotation = 30;
    p.InsertPicture(pic1, 0);

    #endregion

    // Save the docx file
    document.Save();
    }

    ReplyDelete
  3. How do i Word Wrap an image? Plz put some code or something. I am using this dll and i am stuck. Need serious help.

    ReplyDelete
    Replies
    1. @softwaredirectdownloads:

      Hi,

      did you ever find a solution for this problem?
      I'm facing the same problem.

      Regards,
      Stefan

      Delete
    2. how to write a paragraph with images to another file?. Please help solve the problem. my e-mail gsumurfetta@yahoo.com

      Delete
  4. Hi

    Can we add image at first page of word documents using "Docx" Libraray?

    ReplyDelete
  5. Hi

    Is it possible to replace the dummy of image fields by actual ones (I need to merge some images into a template containing image fields).

    Regards
    wvdvegt

    ReplyDelete
  6. HI
    Thanks for this fantastic library.
    When I am using drawing tools or Text Box in my word document then it showing the error on file load.

    Regards,
    Ashish

    ReplyDelete
  7. Hi, how insert a image in header? thanks

    ReplyDelete
  8. Hi, How to insert a image into table cell?

    ReplyDelete