C#WordprocessingDocument-在单元格中插入图像
我有一个方法可以替换文档特定文本中的标记。如何替换标签图片?C#WordprocessingDocument-在单元格中插入图像,c#,image,openxml,wordprocessingml,C#,Image,Openxml,Wordprocessingml,我有一个方法可以替换文档特定文本中的标记。如何替换标签图片? 下面是一段代码,用于查找表中包含文本“PersonMainPhoto”的单元格。选项卡单元格被清除,并插入图像。希望这能引导你朝着正确的方向前进 插入图像的过程分为两部分: 将图像部分添加到文档中 在正文中插入对图像的引用-包括有关缩放、定位等的各种详细信息 插入引用的代码取自brilliant OpenXML SDK文档: 使用System.IO; 使用System.Linq; 使用DocumentFormat.OpenXml;
下面是一段代码,用于查找表中包含文本“PersonMainPhoto”的单元格。选项卡单元格被清除,并插入图像。希望这能引导你朝着正确的方向前进 插入图像的过程分为两部分:
- 将图像部分添加到文档中
- 在正文中插入对图像的引用-包括有关缩放、定位等的各种详细信息
使用System.IO;
使用System.Linq;
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Wordprocessing;
使用A=DocumentFormat.OpenXml.Drawing;
使用DW=DocumentFormat.OpenXml.Drawing.Wordprocessing;
使用PIC=DocumentFormat.OpenXml.Drawing.Pictures;
命名空间堆栈溢出
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串文件=@“c:\temp\mydoc.docx”;
字符串imageFile=@“c:\temp\myimage.jpg”;
字符串labelText=“PersonMainPhoto”;
使用(var document=WordprocessingDocument.Open(文件,isEditable:true))
{
var mainPart=document.MainDocumentPart;
var table=mainPart.Document.Body.subjects().First();
var pictureCell=table.subjects().First(c=>c.InnerText==labelText);
ImagePart ImagePart=mainPart.AddImagePart(ImagePartType.Jpeg);
使用(FileStream-stream=newfilestream(imageFile,FileMode.Open))
{
imagePart.FeedData(流);
}
pictureCell.RemoveAllChildren();
AddImageToCell(pictureCell,mainPart.GetIdOfPart(imagePart));
mainPart.Document.Save();
}
}
私有静态void AddImageToCell(TableCell单元格,字符串关系ID)
{
变量元素=
新图纸(
新的DW.Inline(
新的DW.Extent(){Cx=99000L,Cy=792000L},
新的DW.EffectExtent()
{
LeftEdge=0升,
TopEdge=0升,
RightEdge=0升,
底边=0升
},
新的DW.DocProperties()
{
Id=(UINT32值)1U,
Name=“图片1”
},
新DW.NonVisualGraphicFrameDrawingProperties(
新的A.GraphicFrameLocks(){NoChangeAspect=true}),
新A.图形(
新A.GraphicData(
新图片(
新图片。非可视图片属性(
新图片。非可视化绘图属性()
{
Id=(UINT32值)0U,
Name=“New Bitmap Image.jpg”
},
新图片:NonVisualPictureDrawingProperties()),
新图片。布利普菲尔(
新光点(
新的A.BlipExtensionList(
新的A.BlipExtension()
{
Uri=“{28A0092B-C50C-407E-A947-70E740481C}”
})
)
{
嵌入=关系ID,
压缩状态=
A.BlipCompressionValues.Print
},
新的A.伸展运动(
新的A.FillRectangle()),
新PIC.ShapeProperties(
新A.2D(
新的A.Offset(){X=0L,Y=0L},
新的A.extensts(){Cx=99000L,Cy=792000L}),
新A.几何(
新的A.调整值列表()
)
{Preset=A.ShapeTypeValues.Rectangle})
)
{Uri=”http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
距离顶部=(UINT32值)0U,
距离底部=(UINT32值)0U,
距离左=(UINT32值)0U,
DistanceFromRight=(UINT32值)0U
});
追加(新段落(新运行(元素));
}
}
}
下面是一段代码,用于查找表中包含文本“PersonMainPhoto”的单元格。选项卡单元格被清除,并插入图像。希望这能引导你朝着正确的方向前进
插入图像的过程分为两部分:
- 将图像部分添加到文档中
- 在正文中插入对图像的引用-包括有关缩放、定位等的各种详细信息
使用System.IO;
使用System.Linq;
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Wordprocessing;
使用A=DocumentFormat.OpenXml.Drawing;
使用DW=DocumentFormat.OpenXml.Drawing.Wordprocessing;
使用PIC=DocumentFormat.OpenXml.Drawing.Pictures;
命名空间堆栈溢出
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串文件=@“c:\temp\mydoc.docx”;
字符串imageFile=@“c:\temp\myimage.jpg”;
字符串labelText=“PersonMainPhoto”;
使用(var document=WordprocessingDocument.Open(文件,isEditable:true))
{
var mainPart=document.MainDocumentPart;
var table=mainPart.Document.Body.subjects().First();
var pictureCell=table.subjects().First(c=>c.InnerText==labelText);
ImagePart ImagePart=mainPart.AddImagePart(ImagePartType.Jpeg);
使用(FileStream-stream=newfilestream(imageFile,FileMode.Open))
{
imagePart.FeedData(流);
}
pictureCell.RemoveAllChildren();
AddImageToCell(pictureCell,mainPart.GetIdO
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
string file = @"c:\temp\mydoc.docx";
string imageFile = @"c:\temp\myimage.jpg";
string labelText = "PersonMainPhoto";
using (var document = WordprocessingDocument.Open(file, isEditable: true))
{
var mainPart = document.MainDocumentPart;
var table = mainPart.Document.Body.Descendants<Table>().First();
var pictureCell = table.Descendants<TableCell>().First(c => c.InnerText == labelText);
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(imageFile, FileMode.Open))
{
imagePart.FeedData(stream);
}
pictureCell.RemoveAllChildren();
AddImageToCell(pictureCell, mainPart.GetIdOfPart(imagePart));
mainPart.Document.Save();
}
}
private static void AddImageToCell(TableCell cell, string relationshipId)
{
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U
});
cell.Append(new Paragraph(new Run(element)));
}
}
}
TableCellProperties tableCellProperties1 = VARIABLE.GetFirstChild<TableCellProperties>();
TableCellWidth tableCellWidth1 = tableCellProperties1.GetFirstChild<TableCellWidth>();
int _width = Int32.Parse(tableCellWidth1.Width);
TableRowProperties tableRowProperties1 =
VARIABLE.Parent.GetFirstChild<TableRowProperties>();
TableRowHeight tableRowHeight1 =
tableRowProperties1.GetFirstChild<TableRowHeight>();
int _height = Int32.Parse(tableRowHeight1.Val);
document.MainDocumentPart.Document.Save();