Я не могу поместить изображение в документ docx, сгенерированный программно - PullRequest
0 голосов
/ 09 октября 2019

Я создаю настольное приложение, которое генерирует документ .docx с данными, которые я извлекаю из базы данных SQLite. Я использую пакет Xceed DocX Nuget для его сборки, и я могу писать текст без каких-либо сложностей.

Однако мое приложение должно разместить изображение на заголовке. Я с легкостью извлекаю изображение из базы данных, но мне не удается отправить его в файл .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();
}

Я ожидаю увидеть файл с изображением в заголовке и следующимабзац в теле документа:

"Это простой отформатированный красный жирный абзац, содержащий синий курсивный текст.

Но в моем файле есть только текст, без изображения.

1 Ответ

0 голосов
/ 10 октября 2019

Я сделал это! Вот решение

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