How to add image and text as in the header when generating a dictionary document using Docx dll?

I use docx dll to generate a pdf file, but I can not add the image to the header, although I can add the image to part of the contents of the word file. Here is my code for this:

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

Any help would be great!

+4
source share
1 answer

I got an answer to my problem. In fact, it seems that Docx dll does not support displaying images in a table under the heading. Although they gave a method for displaying the image in the table, and also displayed on their blogs, but still I could not display the image using the table in the header section, which I can easily execute using the paragraph, as in the code below:

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

And it works fine, but the problem arises when you have to display an image with some header text. Because we do not use a table to display the image, so it is difficult for you to correctly align both the image and the title text. After trying so many solutions and hard work, as I did not find support and solutions, in the end I found a solution to my problem. We can align both the header image and the header text in one row without using a table with data below one row:

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

Using the Position() method, you can align both the header text and the header image on one line. Hope this helps someone too :).

+4
source

All Articles