将URL图像添加到Word文件中会导致C#出现问题

问题描述 投票:0回答:2

我想用以下代码传输单词image,但是我认为由于图像加载太晚,出现错误:

System.Runtime.InteropServices.COMException

我的错误在哪里?

// Word uygulamasını oluşturuyoruz.
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
// Yeni doküman oluşturuyoruz.
WordApp.Documents.Add();
// word açılıyor.
WordApp.Visible = true;

Microsoft.Office.Interop.Word.Document doc = WordApp.ActiveDocument;

var link = "https://zeminprogram-debb.restdb.io/media/uydu.png";

doc.InlineShapes.AddPicture(link, Type.Missing, Type.Missing, Type.Missing);  
c#
2个回答
1
投票

您可以使用DocX库实现此功能

这是示例代码:

var myImageFullPath = "C:\tmp\sample.png";
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
{
  // Add an image into the document.    
  Image image = document.AddImage(myImageFullPath);

  // Create a picture (A custom view of an Image).
  Picture picture = image.CreatePicture();

  // Insert a new Paragraph into the document.
  Paragraph title = document.InsertParagraph().Append("This is a test for a 
  picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
  title.Alignment = Alignment.center;

  // Insert a new Paragraph into the document.
  Paragraph p1 = document.InsertParagraph();

 // Append content to the Paragraph
 p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky 
 don't you think?");
 p1.AppendLine();

 p1.AppendPicture(picture);

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

0
投票

不幸的是,同样是同样的问题,

从文件夹检索没有问题但从url检索时仍然给出相同的错误

  var myImageFullPath = "https://zeminprogram-debb.restdb.io/media/uydu.png";
        using (DocX document = DocX.Create(@Application.StartupPath + "\\dene.doc"))
        {
            // Add an image into the document.    
            Xceed.Document.NET.Image image = document.AddImage(myImageFullPath);

            // Create a picture (A custom view of an Image).
            Picture picture = image.CreatePicture();

            // Insert a new Paragraph into the document.


            // Insert a new Paragraph into the document.
            Paragraph p1 = document.InsertParagraph();

            // Append content to the Paragraph
            p1.AppendLine("LOGO").AppendPicture(picture).Append(" its funky  don't you think?");



            p1.AppendLine();

            p1.AppendPicture(picture);
            document.Save();
            // Save this document.

        }
© www.soinside.com 2019 - 2024. All rights reserved.