1

I have created a docX using the docX library and have added an image in the header. However when I convert the docX to PDF using the Spire.doc library, the image is lost. Any idea why? Below is my code:

var doc = DocX.Create(fileName);
string url = @"C:\Users\Desktop\Capture.JPG";
Novacode.Image img = doc.AddImage(url);
Picture pic = img.CreatePicture();
doc.AddHeaders();
Header header_default = doc.Headers.odd;
Paragraph p1 = header_default.InsertParagraph();
p1.Append(headerText).Bold().Color(System.Drawing.Color.LightGray).FontSize(20);
p1.AppendPicture(pic);

doc.Save();
Document docS = new Document();
docS.LoadFromFile(fileName);
string pdfPath = @"C:\Users\Documents\toPDF.PDF";
docS.SaveToFile(pdfPath, FileFormat.PDF);
refresh
  • 1,319
  • 2
  • 20
  • 71

1 Answers1

0

As you already use Spire.Doc in your code, why not use spire to create header in Word directly and then save the file to PDF file format. I tried following code and it works fine.

using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace Doc2Pdf
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            Section section = doc.AddSection();

            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph p1 = header.AddParagraph();
            Image image = Image.FromFile("pic.png");
            p1.AppendPicture(image);

            doc.SaveToFile("Header.pdf", FileFormat.PDF);
        }
    }
}
Kumar
  • 17
  • 1