将图像的图像和标题从文件夹插入到文字中 [英] Insert images and title of the image from folder to word

查看:84
本文介绍了将图像的图像和标题从文件夹插入到文字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确插入图像和图像标题。

第一个标题正在更新,接下来所有图像都插入文档。

如何添加标题所有图片。



我尝试过:



< pre lang =c#> public static void Wordtest1()
{
Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application( ); // 为word app创建实例
wordApplication.Visible = false ; // 设置文字应用的动画状态
Microsoft.Office.Interop.Word.Document document = wordApplication.Documents.Add();
string dirPath = @ ;

foreach string filePath in Directory.GetFiles(dirPath, *。jpg)。Reverse())
{
// 插入文字
string p = Path.GetFileNameWithoutExtension(filePath);

var pText = document.Paragraphs.Add();
pText.Format.SpaceAfter = 10f;
pText.Range.Text = String .Format( 这是#{0}行,p);
pText.Range.InsertParagraphAfter();


// 插入图片
var pPicture = document.Paragraphs.Add();
pPicture.Format.SpaceAfter = 10f;
document.InlineShapes.AddPicture(filePath);
}

// 保存设置
文件.SaveAs( @ + @ \ Report.doc);

// 关闭字
wordApplication.Quit();
}

解决方案

试试这个



 foreach(Directory.GetFiles中的字符串filePath(dirPath,*)。Reverse())
{
string p = Path.GetFileNameWithoutExtension(filePath);
var paragraph = document.Paragraphs.Add();
paragraph.Range.Text = String.Format(This is line#{0},p);
paragraph.Range.InlineShapes.AddPicture(filePath);
paragraph.Range.InsertParagraphAfter();
}


解决如何将图像插入Word文档的最简单方法是创建或运行宏 [ ^ ]



我按照上面的说明记录下列内容:

  Sub  Macro1()
'
' Macro1 Macro
'
'
Selection.InlineShapes.AddPicture FileName:= _
C:\\blacksheep.png,LinkToFile:= False,_
SaveWithDocument:= True
结束 Sub



现在更新代码应该很容易。


I am not able to Insert Images and title of the image correctly.
First title is updating and next all images are inserting to document.
How to add title for All the images.

What I have tried:

public static void Wordtest1()
{
   Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();//Create an instance for word app
   wordApplication.Visible = false;//Set animation status for word application
   Microsoft.Office.Interop.Word.Document document = wordApplication.Documents.Add();
   string dirPath = @"";

   foreach (string filePath in Directory.GetFiles(dirPath, "*.jpg").Reverse())
    {
        // Insert text
        string p = Path.GetFileNameWithoutExtension(filePath);

        var pText = document.Paragraphs.Add();
        pText.Format.SpaceAfter = 10f;
        pText.Range.Text = String.Format("This is line #{0}", p);
        pText.Range.InsertParagraphAfter();


        // Insert picture
        var pPicture = document.Paragraphs.Add();
        pPicture.Format.SpaceAfter = 10f;
        document.InlineShapes.AddPicture(filePath);
    }

    // Save settings
    document.SaveAs(@"" + @"\Report.doc");

    // Close word
    wordApplication.Quit();
}

解决方案

try this

foreach (string filePath in Directory.GetFiles(dirPath, "*").Reverse())
          {
              string p = Path.GetFileNameWithoutExtension(filePath);
              var paragraph = document.Paragraphs.Add();
              paragraph.Range.Text = String.Format("This is line #{0}", p);
              paragraph.Range.InlineShapes.AddPicture(filePath);
              paragraph.Range.InsertParagraphAfter();
          }


The simplest way to work out how to insert an image into a Word document is to Create or run a macro[^]

I followed the instructions above and the following was recorded:

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.InlineShapes.AddPicture FileName:= _
        "C:\\blacksheep.png", LinkToFile:=False, _
        SaveWithDocument:=True
End Sub


Now it should be easy to update your code.


这篇关于将图像的图像和标题从文件夹插入到文字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆