我无法将图像放在以编程方式生成的 docx 文档上

I can't place an image on a docx document programmatically generated

我正在构建一个桌面应用程序,它生成一个 .docx 文档,其中包含我从 SQLite 数据库中提取的数据。我正在使用 Xceed 的 DocX Nuget 包来构建它,我可以毫不费力地编写文本。

但是,我的应用程序需要将图像放在 HEADER 上。我很容易从数据库中提取图像,但是当我尝试将它发送到 .docx 文件时失败了。

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();
}

我希望看到一个文件,该文件在 header 中有图像,在文档的 body 中有以下段落:

“这是一个包含蓝色斜体文本的简单格式红色粗体段落。

但是我的文件只有文字,没有图片。

我做到了!这是解决方案

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();
}