如何用VB.NET将Image/Byte[]格式图片并排插入Word文档?
VB.NET中直接在Word文档并排放置Image/Byte[]格式图片的解决方案
我需要在Word文档中并排放置两张图片,这些图片在VB.NET中以Image或**Byte[]**数组形式存在。试过两种插入方案,但都存在问题:
- Inline Shapes方案:该方法需从本地硬盘文件加载图片,未找到直接加载Image或Byte[]的方式(不想先保存到本地再读取),因此暂时搁置;
- 剪贴板粘贴法:无法实现图片并排,粘贴第二张图片时第一张图片(及附带文本)仍处于选中状态,尝试使用InsertAfter、MoveEnd等语句取消选中无效。
尝试的剪贴板粘贴代码
sparePicture = ByteArrayToImage(lstOrderedSpares(1)) Clipboard.SetDataObject(sparePicture) Dim oPara4 = oDoc.Content.Paragraphs.Add(oMissing) oPara4.Range.Paste() oPara4.Range.InsertAfter("hoi") oPara4.Range.InsertAfter("hoi2") oPara4.Range.MoveEnd() sparePicture = ByteArrayToImage(lstOrderedSpares(2)) Clipboard.SetDataObject(sparePicture) oPara4.Range.Paste()
临时表格解决方案代码
目前已采用表格作为临时解决方案,代码如下:
Dim oPara6 = oDoc.Content.Paragraphs.Add(oMissing) oPara6.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft Dim tableLocation1 As Word.Range = oPara6.Range() oDoc.Tables.Add(Range:=tableLocation1, NumRows:=1, NumColumns:=3) sparePicture = ByteArrayToImage(lstOrderedSpares(1)) Clipboard.SetDataObject(sparePicture) With oDoc.Tables.Item(3).Cell(1, 1).Range .Paste() .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight End With sparePicture = ByteArrayToImage(lstOrderedSpares(2)) Clipboard.SetDataObject(sparePicture) With oDoc.Tables.Item(3).Cell(1, 2).Range .Paste() .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight End With sparePicture = ByteArrayToImage(lstOrderedSpares(3)) Clipboard.SetDataObject(sparePicture) With oDoc.Tables.Item(3).Cell(1, 3).Range .Paste() .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight End With
但仍希望找到直接实现图片并排的方法,求解决方案。
内容的提问来源于stack exchange,提问作者Ukkie
相关产品推荐
相关产品推荐

