1

我使用 docX 库创建了一个 docX,并在标题中添加了一个图像。但是,当我使用 Spire.doc 库将 docX 转换为 PDF 时,图像会丢失。知道为什么吗?下面是我的代码:

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);
4

1 回答 1

0

由于您已经在代码中使用了 Spire.Doc,为什么不使用 spire 直接在 Word 中创建标题,然后将文件保存为 PDF 文件格式。我尝试了以下代码,它工作正常。

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);
        }
    }
}
于 2017-04-25T02:05:41.753 回答