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

17 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
    Replies
    1. Hi, my code worked for me:

      using (DocX document = DocX.Create(txtRutaArchivo.Text))
      {
      //Agrega Encabezado
      document.AddHeaders();
      Novacode.Header encabezado = document.Headers.odd;
      //Agregar imagen al documento
      Novacode.Image img = document.AddImage(@"Logo.png");
      Novacode.Picture pic = img.CreatePicture();
      pic.Height = 40; //Alto imagen
      pic.Width = 100; //Ancho imagen
      //Insertar párrafo vació
      Novacode.Paragraph phimg = encabezado.InsertParagraph("", false).InsertPicture(pic,0).Append("\tDocumento de Ejemplo, usando DocX");

      //Alineación a la izquierda
      phimg.Alignment = Novacode.Alignment.left;
      phimg.Font(new System.Drawing.FontFamily("Courier New")).FontSize(14);

      ////Agrega texto en negritas
      //ph.Append("Documento de Ejemplo, usando DocX").Bold();
      ////Alineación al Centro
      //ph.Alignment = Novacode.Alignment.center;
      ////Tamaño y tipo de fuente
      //ph.Font(new System.Drawing.FontFamily("Courier New")).FontSize(14);

      //Agrega Pie de Página
      document.AddFooters();
      Novacode.Footer pie = document.Footers.odd;
      //Agrega un párrafo al pie de página
      Novacode.Paragraph pf = pie.InsertParagraph();
      //Agrega texto en itálica
      pf.Append(DateTime.Now.ToString("dddd, dd/MMMM/yyyy", CultureInfo.CurrentCulture));
      //Alinea a la derecha.
      pf.Alignment = Alignment.right;
      //Tamaño y tipo de fuente
      pf.Font(new System.Drawing.FontFamily("Arial Black")).FontSize(6);

      Novacode.Paragraph empty = document.InsertParagraph("",false);
      //Agregando párrafo nuevo
      Novacode.Paragraph p = document.InsertParagraph();
      //Añadir texto
      p.Append("Documento de Ejemplo, usando DocX").Font(new System.Drawing.FontFamily("Arial")).FontSize(12);
      //Guarda Archivo
      document.Save();
      }

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

    ReplyDelete
    Replies
    1. Novacode.Image img = document.AddImage(test.png");
      Paragraph p = document.Tables[0].Rows[0].Cells[0].InsertParagraph("", false);

      Picture pic = img.CreatePicture();
      p.InsertPicture(pic, 0);

      Delete
    2. This comment has been removed by the author.

      Delete
  9. Hi, how i replace image with another image

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete

  11. I have a list and need to disply it like this.Please suggest me some ways.Is it possible to do like this?
    foreach (var s in qItems[i].qImages)
    {
    Paragraph p = document.InsertParagraph();
    Picture pic1 = p.InsertPicture(s.image);
    }

    ReplyDelete
  12. Thank you for bringing more information to this topic for me. I’m truly grateful and really impressed.

    xml data conversion

    ReplyDelete