Inserimento di un’immagine in un file Word

' Il seguente frammento di codice mostra come si può inserire un'immagine in un file Word in formato DOCX o DOCM
' Può essere usato in un form associato ad una categoria documentale di tipo file per apporre una firma e il nome
' di chi ha firmato su un file Word
' L'immagine è contenuta in un campo del form chiamato "FIRMA_SAVE", e sul documento deve essere presente un segnalibro chiamato "QW_FIRMA_WACOM" nella posizione in cui andrà inserita l'immagine.

dim QWSess as QWSession = form.Session("QWSess")

dim path_doc as string

path_doc = QWSess.Q95_NOME_DIR_DOCUM + "\" + form.form_state.dmd_ref.TIPI_DOC.rowset.fields("DIRECTORY").value + "\" + iif(form.form_state.dmd_ref.DOCUMENT.rowset.fields("PUBBLICATO").value, QWSess.DESCRITT.rowset.fields("DIR_IN_VIG").value, QWSess.DESCRITT.rowset.fields("DIR_ATTESA").value) + "\" + form.form_state.dmd_ref.DOCUMENT.rowset.fields("NOMEFILE").value

if not dbdollar(".DOCX", upper(path_doc)) and not dbdollar(".DOCM", upper(path_doc)) then
	form.Alert("Il Documento deve essere in formato DOCX o DOCM !", "ERRORE")
else
	dim nomefilejpg as string
	
	nomefilejpg = funique(Q95_PATH_TEMP + "\??????????.png")
	'Base64ToFile(form.findcontrol2("FIRMA_SAVE").Text, nomefilejpg)
	Base64ToFile(replace(unEscape(form.findcontrol2("FIRMA_SAVE").Text), "data:image/png;base64,", ""), nomefilejpg)
	
	if FileLen(nomefilejpg) > 0 then
		Dim doc As DocumentFormat.OpenXml.Packaging.WordprocessingDocument = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(path_doc, True)
		
        For Each bookmarkStart As DocumentFormat.OpenXml.Wordprocessing.BookmarkStart In doc.MainDocumentPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.BookmarkStart)()
            If bookmarkStart.Name = "QW_FIRMA_WACOM" Then
				Dim elem As DocumentFormat.OpenXml.OpenXmlElement = bookmarkStart.NextSibling()
				
				While elem IsNot Nothing AndAlso Not (TypeOf elem Is DocumentFormat.OpenXml.Wordprocessing.BookmarkEnd)
					Dim nextElem As DocumentFormat.OpenXml.OpenXmlElement = elem.NextSibling()
					elem.Remove()
					elem = nextElem
				End While
				
				Dim mainPart As DocumentFormat.OpenXml.Packaging.MainDocumentPart
				Dim imagePart As DocumentFormat.OpenXml.Packaging.ImagePart
				
				mainPart = doc.MainDocumentPart
				imagePart = mainPart.AddImagePart(DocumentFormat.OpenXml.Packaging.ImagePartType.Png)
				
				Using stream As System.IO.FileStream = New System.IO.FileStream(nomefilejpg, System.IO.FileMode.Open)
					imagePart.FeedData(stream)
				End Using
				
				Dim relationshipId As String = doc.MainDocumentPart.GetIdOfPart(imagePart)
				
				Dim element as DocumentFormat.OpenXml.Wordprocessing.Drawing = New DocumentFormat.OpenXml.Wordprocessing.Drawing(New DocumentFormat.OpenXml.Drawing.Wordprocessing.Inline(New DocumentFormat.OpenXml.Drawing.Wordprocessing.Extent() With {
					.Cx = 990000L,
					.Cy = 792000L
				}, New DocumentFormat.OpenXml.Drawing.Wordprocessing.EffectExtent() With {
					.LeftEdge = 0L,
					.TopEdge = 0L,
					.RightEdge = 0L,
					.BottomEdge = 0L
				}, New DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties() With {
					.Id = CType(1UI, DocumentFormat.OpenXml.UInt32Value),
					.Name = "Picture 1"
				}, New DocumentFormat.OpenXml.Drawing.Wordprocessing.NonVisualGraphicFrameDrawingProperties(New DocumentFormat.OpenXml.Drawing.GraphicFrameLocks() With {
					.NoChangeAspect = True
				}), New DocumentFormat.OpenXml.Drawing.Graphic(New DocumentFormat.OpenXml.Drawing.GraphicData(New DocumentFormat.OpenXml.Drawing.Pictures.Picture(New DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties(New DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties() With {
					.Id = CType(0UI, DocumentFormat.OpenXml.UInt32Value),
					.Name = "New Bitmap Image.jpg"
				}, New DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties()), New DocumentFormat.OpenXml.Drawing.Pictures.BlipFill(New DocumentFormat.OpenXml.Drawing.Blip(New DocumentFormat.OpenXml.Drawing.BlipExtensionList(New DocumentFormat.OpenXml.Drawing.BlipExtension() With {
					.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
				})) With {
					.Embed = relationshipId,
					.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print
				}, New DocumentFormat.OpenXml.Drawing.Stretch(New DocumentFormat.OpenXml.Drawing.FillRectangle())), New DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties(New DocumentFormat.OpenXml.Drawing.Transform2D(New DocumentFormat.OpenXml.Drawing.Offset() With {
					.X = 0L,
					.Y = 0L
				}, New DocumentFormat.OpenXml.Drawing.Extents() With {
					.Cx = 990000L,
					.Cy = 792000L
				}), New DocumentFormat.OpenXml.Drawing.PresetGeometry(New DocumentFormat.OpenXml.Drawing.AdjustValueList()) With {
					.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle
				}))) With {
					.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
				})) With {
					.DistanceFromTop = CType(0UI, DocumentFormat.OpenXml.UInt32Value),
					.DistanceFromBottom = CType(0UI, DocumentFormat.OpenXml.UInt32Value),
					.DistanceFromLeft = CType(0UI, DocumentFormat.OpenXml.UInt32Value),
					.DistanceFromRight = CType(0UI, DocumentFormat.OpenXml.UInt32Value),
					.EditId = "50D07946"
				})
				
				bookmarkStart.Parent.InsertAfter(Of DocumentFormat.OpenXml.Wordprocessing.Run)(New DocumentFormat.OpenXml.Wordprocessing.Run(element), bookmarkStart)				
				
                bookmarkStart.Remove()
                Exit For
            End If
        Next
		
        doc.Close()
	end if
end if