Reputation: 23
I am building a desktop application that generates a .docx document with data that I pull from an SQLite database. I am using Xceed's DocX Nuget package to build it, and I can write text without any complication.
However, my application needs to place an image ON THE HEADER. I pull the image with ease from the database, but I fail when I try to send it to the .docx file.
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Documento Word (*.docx)|*.docx",
FileName = "Documento " + obj_eObra.ProcesoDeSeleccion + ".docx",
DefaultExt = ".docx"
};
if (saveFileDialog.ShowDialog() == true)
{
DocX document = DocX.Create(saveFileDialog.FileName);
Stream Logo = new MemoryStream(obj_eEmpresa.Logo);
Xceed.Document.NET.Image image = document.AddImage(Logo);
document.AddHeaders();
document.AddFooters();
// Force the first page to have a different Header and Footer.
document.DifferentFirstPage = true;
// Force odd & even pages to have different Headers and Footers.
document.DifferentOddAndEvenPages = true;
// Insert a Paragraph into the first Header.
document.Headers.First.Images.Add(image);
// Insert a Paragraph into this document.
var p = document.InsertParagraph();
// Append some text and add formatting.
p.Append("This is a simple formatted red bold paragraph")
.Font(new Font("Arial"))
.FontSize(25)
.Color(System.Drawing.Color.Red)
.Bold()
.Append(" containing a blue italic text.").Font(new Font("Times New Roman")).Color(System.Drawing.Color.Blue).Italic()
.SpacingAfter(40);
document.Save();
}
I expect to see a file that has an image in the header and the following paragraph in the body of the document:
"This is a simple formatted red bold paragraph containing a blue italic text.
But my file only has text, no image.
Upvotes: 0
Views: 2488
Reputation: 23
I did it! Here's the solution
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Documento Word (*.docx)|*.docx",
FileName = "Documento " + obj_eObra.ProcesoDeSeleccion + ".docx",
DefaultExt = ".docx"
};
if (saveFileDialog.ShowDialog() == true)
{
DocX document = DocX.Create(saveFileDialog.FileName);
Stream Logo = new MemoryStream(obj_eEmpresa.Logo);
Xceed.Document.NET.Image image = document.AddImage(Logo);
document.AddHeaders();
document.AddFooters();
// Force the first page to have a different Header and Footer.
document.DifferentFirstPage = true;
// Force odd & even pages to have different Headers and Footers.
document.DifferentOddAndEvenPages = true;
// Insert a Paragraph & image into the first Header.
var picture = image.CreatePicture();
var p = document.Headers.First.InsertParagraph("");
p.AppendPicture(picture);
p.SpacingAfter(30);
// Insert a Paragraph into this document.
Paragraph CiudadYFecha = document.InsertParagraph();
// Append some text and add formatting.
CiudadYFecha.Append("\n\n\n" + obj_eObra.Ciudad + ", " + obj_eObra.FechaActual + ".\n\nSeñores: " + "\n" + obj_eObra.Cliente + "\n\nAtt.: Comité de Selección " + "\nRef.: " + "<Insertar adjudicacion> " + "N° " + obj_eObra.ProcesoDeSeleccion)
.Font(new Font("Times New Roman"))
.FontSize(12)
.SpacingAfter(40);
Paragraph Cuerpo = document.InsertParagraph("De nuestra consideración: \n\n" + "Es grato dirigirnos a ustedes en atención al proceso de selección de la referencia, para alcanzarles nuestra oferta técnico – económica.\n\n" + "Sin otro particular, nos suscribimos de ustedes,\n\n" + "Atentamente,\n");
Cuerpo.Font(new Font("Times New Roman"))
.FontSize(12)
.SpacingAfter(40);
document.Save();
}
Upvotes: 1