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.
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
Resizing
Shaping
Time for an example using DocX; below is a file entitled Example.docx.
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.
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 manipulateIf 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.
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
happy coding,
Cathal
Hi. Compiler throws error: No overload for method 'InsertPicture' takes 3 arguments on: Picture pic1 = p.InsertPicture(img.Id, "Donkey", "Taken on Omey island");
ReplyDeleteSince 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.
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.
ReplyDeleteusing (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();
}
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@softwaredirectdownloads:
DeleteHi,
did you ever find a solution for this problem?
I'm facing the same problem.
Regards,
Stefan
how to write a paragraph with images to another file?. Please help solve the problem. my e-mail gsumurfetta@yahoo.com
DeleteHi
ReplyDeleteCan we add image at first page of word documents using "Docx" Libraray?
Hi
ReplyDeleteIs 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
HI
ReplyDeleteThanks 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
Hi, how insert a image in header? thanks
ReplyDeleteHi, my code worked for me:
Deleteusing (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();
}
Hi, How to insert a image into table cell?
ReplyDeleteNovacode.Image img = document.AddImage(test.png");
DeleteParagraph p = document.Tables[0].Rows[0].Cells[0].InsertParagraph("", false);
Picture pic = img.CreatePicture();
p.InsertPicture(pic, 0);
This comment has been removed by the author.
DeleteHi, how i replace image with another image
ReplyDeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteI 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);
}
Thank you for bringing more information to this topic for me. I’m truly grateful and really impressed.
ReplyDeletexml data conversion