当前位置: 首页 > 编程语言 > C#

Convert-Embedtoaw-drawingshare

时间:2023-04-10 17:40:38 C#

Convert/Embedtoaw:drawing我有一个V:Shape我假设V:Shape是从一个.Doc文件转换成一个.Docx文件,但是当我写在代码中,我想要一个DocumentFormat.OpenXml.Drawing.Pictures.Picture。有没有办法以编程方式转换为图片?s**t代码风暴...{p.Append(item.CloneNode(true));}else{p.Append(CreateImageRun(source,item,target,f));}}publicRunCreateImageRun(WordprocessingDocumentsourceDoc,RunsourceRun,WordprocessingDocumenttarget,FooterPartfooterPart){ImagePartnewPart=footerPart.AddImagePart(ImagePartType.Png);ImageDatashape=sourceRun.Descendants().FirstOrDefault();ImagePartp=sourceDoc.MainDocumentPart.GetPartById(shape.RelationshipId)asImagePart;位图图像=newBitmap(p.GetStream());使用(Streams=p.GetStream()){s.Position=0;newPart.FeedData(s);}stringpartId=footerPart.GetIdOfPart(newPart);绘图newImage=CreateImage(partId);返回新运行(新图像);}privateDrawingCreateImage(stringrelationshipId){//定义图像的引用。返回新博士awing(newDW.Inline(newDW.Extent(){Cx=990000L,Cy=792000L},newDW.EffectExtent(){LeftEdge=0L,TopEdge=0L,RightEdge=0L,BottomEdge=0L},newDW.DocProperties(){Id=(UInt32Value)1U,Name="图片1"},newDW.NonVisualGraphicFrameDrawingProperties(newA.GraphicFrameLocks(){NoChangeAspect=true}),newA.Graphic(newA.GraphicData(newPIC.图片(newPIC.NonVisualPictureProperties(newPIC.NonVisualDrawingProperties(){Id=(UInt32Value)0U,Name="NewBitmapImage.jpg"},newPIC.NonVisualPictureDrawingProperties(),newPIC.BlipFill(newA.Blip(newA.BlipExtensionList(newA.BlipExtension(){Uri="{28A0092B-C50C-407E-A947-70E740481C1C}"})){Embed=relationshipId,CompressionState=A.BlipCompressionValues.Print},newA.Stretch(新A.FillRectangle())),newPIC.ShapeProperties(newA.Transform2D(newA.Offset(){X=0L,Y=0L},newA.Extents(){Cx=990000L,Cy=792000L}),新A.PresetGeometry(新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,EditId="50D07946"});完整的代码上下文这里有一个不同的问题:创建一个ImagePart不会保存OpenXML中的关系我发现找到了一个快速/简单的方法-我应该早点修复它!Shape对象有一个ImageData对象作为后代,所以我检查了它并通过一个额外的方法运行它来获取ImagePart:A.Blippic=item.Descendants().FirstOrDefault();ImageDataimageData=item.Descendants().FirstOrDefault();if(pic==null&&imageData==null)//图片的处理方式不同——它们绝对是代码风暴...{runToAmend.InsertAfterSelf(item.CloneNode(true));}else{if(pic!=null){runToAmend.InsertAfterSelf(CreateImageFromBlip(source,item,footerHeaderPart,pic));}elseif(imageData!=null){runToAmend.InsertAfterSelf(CreateImageFromShape(source,item,footerHeaderaderPart,imageData));然后创建了一个新方法-CreateImageFromShape(..)然后调用原始的CreateImageRun私有运行CreateImageFromShape(WordprocessingDocumentsourceDoc,RunsourceRun,OpenXmlPartheaderFooterPart,ImageDataimageData){GetPartById(imageData.RelationshipId)作为ImagePart;返回CreateImageRun(sourceDoc,sourceRun,headerFooterPart,p);}任务完成。以上就是《C#学习教程:converting/Embedtoaw:drawing》的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处: