OpenXML插入图片,文件大小增加近150%

LSSQXT 2016-02-19 11:45:18
VS2008 OpenXML2.0
按MSDN上的代码在一个大小不到2KB的docx文档中插入一张图片(大小607KB),发现文档大小变为(929KB),也就是说增加了所插入图片的差不多1.5倍,
但是
1、把扩展名改为.zip解压缩,解出来的文件夹总大小才608KB
2、用Word 2007打开并另存该文档为别的文件名,文件大小变为619KB(解压缩发现包里自动增加了一些页眉页脚样式之类的小xml文件)
有没有人知道问题出在哪里,请赐教!

代码如下:
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging
Imports System.IO
Imports A = DocumentFormat.OpenXml.Drawing
Imports DW = DocumentFormat.OpenXml.Drawing.Wordprocessing
Imports PIC = DocumentFormat.OpenXml.Drawing.Pictures

Public Class Form1
Dim docx_fn As String = "d:\TEMP\test2.docx"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建WordprocessingDocument实例doc,对应于TEST.docx文件
Dim docx As WordprocessingDocument = WordprocessingDocument.Create(docx_fn, WordprocessingDocumentType.Document)

Using docx
'为doc添加MainDocumentPart部分
Dim mp As MainDocumentPart = docx.AddMainDocumentPart()

'为mainPart添加Document,对应于Word里的文档内容部分
mp.Document = New Document

'为Document添加Body,之后所有于内容相关的均在此body中
Dim body1 As Body = mp.Document.AppendChild(New Body)

'添加段落P,P中包含一个文本
Dim p As Paragraph = mp.Document.Body.AppendChild(New Paragraph)
p.AppendChild(New Run(New Text("This is a Docx for test!")))

End Using

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim document As String = docx_fn
Dim fileName As String = "Tulips.jpg"
InsertAPicture(document, fileName)
End Sub


Public Sub InsertAPicture(ByVal document As String, ByVal fileName As String)
Using wordprocessingDocument As WordprocessingDocument = wordprocessingDocument.Open(document, True)
Dim mainPart As MainDocumentPart = wordprocessingDocument.MainDocumentPart

Dim imagePart As ImagePart = mainPart.AddImagePart(ImagePartType.Jpeg)

Using stream As New FileStream(fileName, FileMode.Open)
imagePart.FeedData(stream)
End Using

AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart))
End Using
End Sub

Private Sub AddImageToBody(ByVal wordDoc As WordprocessingDocument, ByVal relationshipId As String)
' Define the reference of the image.
Dim element = New Drawing( _
New DW.Inline( _
New DW.Extent() With {.Cx = 990000L, .Cy = 792000L}, _
New DW.EffectExtent() With {.LeftEdge = 0L, .TopEdge = 0L, .RightEdge = 0L, .BottomEdge = 0L}, _
New DW.DocProperties() With {.Id = CType(1UI, UInt32Value), .Name = "Picture1"}, _
New DW.NonVisualGraphicFrameDrawingProperties( _
New A.GraphicFrameLocks() With {.NoChangeAspect = True} _
), _
New A.Graphic(New A.GraphicData( _
New PIC.Picture( _
New PIC.NonVisualPictureProperties( _
New PIC.NonVisualDrawingProperties() With {.Id = 0UI, .Name = "Koala.jpg"}, _
New PIC.NonVisualPictureDrawingProperties() _
), _
New PIC.BlipFill( _
New A.Blip( _
New A.BlipExtensionList( _
New A.BlipExtension() With {.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"}) _
) With {.Embed = relationshipId, .CompressionState = A.BlipCompressionValues.Print}, _
New A.Stretch( _
New A.FillRectangle() _
) _
), _
New PIC.ShapeProperties( _
New A.Transform2D( _
New A.Offset() With {.X = 0L, .Y = 0L}, _
New A.Extents() With {.Cx = 990000L, .Cy = 792000L}), _
New A.PresetGeometry( _
New A.AdjustValueList() _
) With {.Preset = A.ShapeTypeValues.Rectangle} _
) _
) _
) With {.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"} _
) _
) With {.DistanceFromTop = 0UI, _
.DistanceFromBottom = 0UI, _
.DistanceFromLeft = 0UI, _
.DistanceFromRight = 0UI} _
)

' Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(New Paragraph(New Run(element)))
End Sub


End Class

PS:插入图片的代码来自https://msdn.microsoft.com/EN-US/library/office/bb497430(v=office.15).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-5
...全文
260 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
LSSQXT 2016-02-23
  • 打赏
  • 举报
回复
都没人遇到过吗???

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧