asp.net - 如何在使用Docx dll生成word文档时在标题中添加图像和文本?

标签 asp.net asp.net-mvc ms-word docx

我正在使用 Docx dll 生成 pdf 文件,但无法在标题中添加图像,尽管我可以在 word 文件的内容部分中添加图像。这是我的代码:

using (Novacode.DocX document = Novacode.DocX.Create(savePath))
    {

        // Add Header and Footer support to this document.
        document.AddHeaders();
        document.AddFooters();

        // Get the default Header for this document.
        Novacode.Header header_default = document.Headers.odd;

        // Add an Image to the docx file
        string imageName = "LOGO.png";
        string url = Request.MapPath("/PDFFolder/" + imageName);

        Novacode.Image img = document.AddImage(url);

        // Insert a Paragraph into the default Header.
        Novacode.Picture pic1 = img.CreatePicture();           
        Novacode.Paragraph p1 = header_default.InsertParagraph();
        header_default.Pictures.Add(pic1);           

        p1.Append("Some more text").Bold();


        // Add a new Paragraph to the document.           
        Novacode.Paragraph p = document.InsertParagraph();

        // Append some text.
        p.Append(textword).Font(new myDrawing.FontFamily("Arial"));

        // Get the default Footer for this document.
        Novacode.Footer footer_default = document.Footers.odd;


        // Insert a Paragraph into the default Footer.
        Novacode.Paragraph p3 = footer_default.InsertParagraph();
        p3.Append("Hello Footer.").Bold();


        // Save the document.
        document.Save();
    }

任何帮助都会很棒!!

最佳答案

我得到了问题的答案。实际上,Docx dll 似乎不支持在标题下的表格中显示图像。尽管他们已经给出了在表格中显示图像的方法,并且也在他们的博客中显示,但我仍然无法使用标题部分中的表格显示图像,我可以通过使用下面给出的代码中的段落轻松完成:

// Insert pic and text into the default Header.
Novacode.Paragraph p1 = header_default.InsertParagraph();
p1.Direction = Novacode.Direction.LeftToRight;
p1.AppendPicture(pic1);

它工作正常,但当您必须显示带有某些标题文本的图像时,就会出现问题。因为我们不使用表格来显示图像,所以很难正确对齐图像和标题文本。经过尝试了很多解决方案和努力工作,因为我没有找到任何支持和解决方案,终于找到了我的问题的解决方案。我们可以将标题图像和标题文本对齐在一行中,而无需使用下面给出的一行表格:

p1.Append("Headertext").Bold().Position(30);

借助 Position() 方法,您可以将标题文本和标题图像对齐在一行中。希望这也会对某人有所帮助:) 。

关于asp.net - 如何在使用Docx dll生成word文档时在标题中添加图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14619517/

相关文章:

asp.net-mvc - 如何获取Controller中ActionResult返回的html

c# - 将word文档另存为图片

asp.net - 圆角按钮 ASP.NET

c# - ASP.NET、HttpWebRequest 和 "Could not create SSL/TLS secure channel"

javascript - 在新窗口中从 javascript 打开 MVC 4 View

c# - 什么时候应该在 Controller 和模型中编写代码?

c# - 文件恢复

php - 如何使用 php code-igniter 读取 docx 文件数学方程式

asp.net - 使用进程外 session 状态的 ASP.NET 应用程序中的 SQL Server 连接问题

c# - asp.net mvc 模型绑定(bind)不绑定(bind)集合