英文:
C# Interop.Word insert image into paragraph
问题
使用Visual Studio/C#和Microsoft.Office.Interop.Word,我有以下代码可以将文本正确地放置在Word文档中:
private void WriteBookLine(string theLine, int al, string font, int fontSize, int fontBold, int spaceAfter)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
paraTop.Range.Text = theLine;
if (al == 0)
paraTop.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
else if (al == 1)
paraTop.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
paraTop.Range.Font.Name = font;
paraTop.Range.Font.Size = fontSize;
paraTop.Range.Font.Bold = fontBold;
paraTop.Format.SpaceAfter = spaceAfter;
paraTop.Range.InsertParagraphAfter();
}
但是,当我调用以下代码来将图片放置在Word文档的下一个段落中时:
private void WriteBookImage(int im)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
if (im == 0)
{
doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
}
paraTop.Range.InsertParagraphAfter();
}
这会将图像放置在文档的第一页,第一行的顶部,而不是作为下一个段落。请问您有什么建议,我错在哪里?
英文:
So with Visual Studio/C# and Microsoft.Office.Interop.Word;
I have this code which places text correctly to the word doc
private void WriteBookLine(string theLine, int al, string font, int fontSize, int fontBold, int spaceAfter)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
paraTop.Range.Text = theLine;
if (al == 0)
paraTop.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
else if (al == 1)
paraTop.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
paraTop.Range.Font.Name = font;
paraTop.Range.Font.Size = fontSize;
paraTop.Range.Font.Bold = fontBold;
paraTop.Format.SpaceAfter = spaceAfter;
paraTop.Range.InsertParagraphAfter();
}
But when I call this code to place a picture as the next paragraph in the word doc
private void WriteBookImage(int im)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
if (im == 0)
{
doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
}
paraTop.Range.InsertParagraphAfter();
}
This places the image at the very top of the document on page 1, line 1 instead as the next paragraph, any ideas where I am going wrong please
Cheers
Kev
答案1
得分: 1
请尝试以下代码:
private void WriteBookImage(int im)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
if (im == 0)
{
// 在此处指定要添加Inlineshpae的范围[where]
// 在这种情况下,doc将是[where],而doc中的最后一个InlineShape将正好是[where]
//doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
// 在这种情况下,paraTop(刚刚添加的最后一个段落)将是[where]
// [where]参数类似于对象Hyperlinks Bookmark的Range参数以及对象Shape的Anchor参数,在它们的Add方法中使用。
//paraTop.Range.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png", Range: paraTop.Range.Next()); // 使用参数Range指定,使用Next方法获取刚刚添加的新段落。
// 在我的测试中,我发现官方文档中有一个错误,并已提交更正:
// 当调用方不是文档中的最后一个段落时,它将返回一个表示已添加到文档中的新空白段落的Paragraph对象,否则,它将是调用段落本身。
//https://github.com/MicrosoftDocs/VBA-Docs/pull/1727/commits/ddd0bbe1b29e90a64012dd8d1e9ec9a1a3c86e4f
//https://github.com/MicrosoftDocs/office-developer-word-pia-ref-dotnet/pull/22#issuecomment-1637070970
}
paraTop.Range.InsertParagraphAfter();
// 如果您要在此处插入新段落,然后:
//paraTop.Range.Next().InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
}
英文:
Try this first:
private void WriteBookImage(int im)
{
paraTop = doc.Content.Paragraphs.Add(ref oMissing);
if (im == 0)
{
//you should specify the range [where] to add inlineshpae
//in this case, doc will be the [where] and the last InlineShape in the doc will be the exactly [where]
//doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
//in this case, paraTop(the last paragrahp you just added it in before) will be the [where]
// the [where] argument just like the parameter Range of the object Hyperlinks Bookmark, etc, and Anchor of the object Shape, in their method Add.
//paraTop.Range.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
doc.InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png", Range:paraTop.Range.Next()); // specify by argument Range, use Next methot to get the new paragraph just be added before.
//In my testing yesterday, I found an error in the official documentation, and have already submitted a correction:
//Returns a **Paragraph** object When the caller is not the last paragraph in a document, that represents a new, blank paragraph added to a document, otherwise, that will be the calling paragraph itself.
//https://github.com/MicrosoftDocs/VBA-Docs/pull/1727/commits/ddd0bbe1b29e90a64012dd8d1e9ec9a1a3c86e4f
//https://github.com/MicrosoftDocs/office-developer-word-pia-ref-dotnet/pull/22#issuecomment-1637070970
}
paraTop.Range.InsertParagraphAfter();
// if you want to insert the new paragraph here then :
//paraTop.Range.Next().InlineShapes.AddPicture(@"C:\Users\fred\source\repos\xxx\wwwroot\images\yyy.png");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论