Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Thu Nov 11, 2021 3:51 pm

Hello!

We're trying to insert a picture in a Word file. I've tried to first insert a textbox and then set the textbox's backround image to this image we want to insert, and I've also tried to just insert it as an image (without the textbox). In both ways, you could specify the width and the height. The picture is 130 x 130 px. When we pre open the picture and when we insert it directly in Word, the size remains the same. BUT! When we insert it in the document in code, the image size gets a little bit larger for somehow..

Here's what I do:

Public Sub drawBlueAGLogo(document As Document, premie As String)
Dim pImgAGCirkel = document.Sections(0).AddParagraph.AppendTextBox(130, 130)

Dim pPremieStor = pImgAGCirkel.Body.AddParagraph
Dim pPremieLiten = pImgAGCirkel.Body.AddParagraph()
formatText(pPremieStor, vbNewLine & vbNewLine & Math.Round(premie / 12), "Calibri", 30, False, Color.White) <-- this is just a function that formats the text
pPremieStor.Format.HorizontalAlignment = HorizontalAlignment.Center
formatText(pPremieLiten, "kr/månaden" & vbNewLine & "med autogiro", "Calibri", 11, False, Color.White)
pPremieLiten.Format.HorizontalAlignment = HorizontalAlignment.Center

pImgAGCirkel.Format.HorizontalOrigin = HorizontalOrigin.Page
pImgAGCirkel.Format.HorizontalPosition = 215
pImgAGCirkel.Format.VerticalOrigin = VerticalOrigin.Page
pImgAGCirkel.Format.VerticalPosition = 40
pImgAGCirkel.Format.FillEfects.Type = BackgroundType.Picture
pImgAGCirkel.Format.FillEfects.Picture = Image.FromFile("test.png")
pImgAGCirkel.Format.TextWrappingStyle = TextWrappingStyle.Through
pImgAGCirkel.Format.NoLine = True
End Sub

I've also tried to set the size as a float (130.0F x 130.0F). Am I doing somehting wrong here?

As you can see, I've attached the two images before and after inserting it into our code.

Best,

Elin
You do not have the required permissions to view the files attached to this post.

elinarctic
 
Posts: 25
Joined: Thu Oct 21, 2021 11:39 am

Fri Nov 12, 2021 8:21 am

Hello,

Thank you for your inquiry.
Kindly note the unit of picture is Pixel, while the unit in our Spire.Doc is Point. In your case, the size of the TextBox you defined is actually larger than the size of the picture,which causes the picture added to the Word file to become larger. To solve this problem, please refer to the following code to convert Pixel unit to Point, and then define the size of the TextBox.
Code: Select all
Public Sub drawBlueAGLogo(document As Document, premie As String)
Dim section As Section = doc. AddSection
Dim paragraph As Paragraph = section. AddParagraph
Dim image As Image = Image.FromFile("test.png")
'Convert unit
Dim convertorUnit As PdfUnitConvertor = New PdfUnitConvertor
'Convert Pixel unit to Point
Dim widthInPoint As Single = convertorUnit.ConvertUnits(image. Width, PdfGraphicsUnit. Pixel, PdfGraphicsUnit.Point)
Dim heightInPoint As Single = convertorUnit.ConvertUnits(image. Height, PdfGraphicsUnit. Pixel, PdfGraphicsUnit.Point)
Dim pImgAGCirkel = paragraph. AppendTextBox(widthInPoint, heightInPoint)

Dim pPremieStor = pImgAGCirkel.Body.AddParagraph
Dim pPremieLiten = pImgAGCirkel.Body.AddParagraph()
formatText(pPremieStor, vbNewLine & vbNewLine & Math. Round(premie / 12), "Calibri", 30, False, Color.White) <-- this is just a function that formats the text
pPremieStor.Format.HorizontalAlignment = HorizontalAlignment. Center
formatText(pPremieLiten, "kr/månaden" & vbNewLine & "med autogiro", "Calibri", 11, False, Color.White)
pPremieLiten.Format.HorizontalAlignment = HorizontalAlignment. Center

pImgAGCirkel.Format.HorizontalOrigin = HorizontalOrigin.Page
pImgAGCirkel.Format.HorizontalPosition = 215
pImgAGCirkel.Format.VerticalOrigin = VerticalOrigin.Page
pImgAGCirkel.Format.VerticalPosition = 40
pImgAGCirkel.Format.FillEfects.Type = BackgroundType.Picture
pImgAGCirkel.Format.FillEfects.Picture = image
pImgAGCirkel.Format.TextWrappingStyle = TextWrappingStyle.Through
pImgAGCirkel.Format.NoLine = True
End Sub

In addition, I did a test about inserting the picture into the Word file, but did not reproduce your issue. The test code is as follows:
Code: Select all
Dim document As Document = New Document
Dim section As Section = document.AddSection
Dim paragraph As Paragraph = section.AddParagraph
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left
Dim ima As Image = Image.FromFile("test.png")
'Add a image
paragraph.AppendPicture(ima)

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 712
Joined: Wed Apr 07, 2021 2:50 am

Fri Nov 12, 2021 8:48 am

That seemed to do the trick! Thank you so much :D

/ Elin

elinarctic
 
Posts: 25
Joined: Thu Oct 21, 2021 11:39 am

Fri Nov 12, 2021 9:47 am

Hi,

You're welcome.
If you have other questions about using our products in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 712
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc