将图像的图像和标题从文件夹插入到文字中 [英] Insert images and title of the image from folder to word
问题描述
我无法正确插入图像和图像标题。
第一个标题正在更新,接下来所有图像都插入文档。
如何添加标题所有图片。
我尝试过:
< 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屋!