Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

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

elinarctic
 
Posts: 27
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: 1643
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: 27
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: 1643
Joined: Wed Apr 07, 2021 2:50 am

Wed Feb 09, 2022 7:14 am

Hello again!

I've used the converter from pixels to points on an other place in our code. We're trying to add a picture to a document, and the picture may vary (it may be different logos depending on a certain input). This seems to work fine, since you get the original image's widht and height when loading it. When the document then is saved from .dotx to .pdf, the image's quality gets worse than the original image. When comparing the PDF with the original image, it seems like the image on the pdf gets a tiny bit larger (like if you would drag it from the corner and make it larger in a document). We've debugged the function and it seems to convert pixels to points correctly (for example 166 px --> 124.5 points). So I don't know why the image quality gets worse this time :-(

I've attached an example here with one of the logos that we use. The left one is the one in the PDF and the right one is the original image. As you can see, the left one has worse quality.

This is our function:

Public Sub drawVeckobrevLogo(document As Document, imgPath As String, horPos As Single, verPos As Single)
Dim image As Image = Image.FromFile(imgPath)
Dim convertorUnit As PdfUnitConvertor = New PdfUnitConvertor
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 pImgLogo = document.Sections(0).AddParagraph.AppendTextBox(widthInPoint, heightInPoint)

pImgLogo.Format.HorizontalOrigin = HorizontalOrigin.Page
pImgLogo.Format.HorizontalPosition = horPos
pImgLogo.Format.VerticalOrigin = VerticalOrigin.Page
pImgLogo.Format.VerticalPosition = verPos
pImgLogo.Format.FillEfects.Type = BackgroundType.Picture
pImgLogo.Format.FillEfects.Picture = image


pImgLogo.Format.TextWrappingStyle = TextWrappingStyle.Through
pImgLogo.Format.NoLine = True

End Sub

Do you have any suggestions that we could do?

Best,

Elin

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

Wed Feb 09, 2022 8:21 am

Hello,

Thank you for your inquiry.
I did an initial test, but didn't reproduce the issue you mentioned. The version I used is the latest version (Spire.Office Platinum Version:7.2). If you were not using the latest version, please first give it a try. If the issue still exists after trying, please provide the following information. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
1) Your source logo image.
2) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).
3) Your application type, such as Console app (. Net Framework 4.5).

Sincerely,
Annika
E-iceblue support team
User avatar

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

Wed Feb 09, 2022 8:41 am

Okay, super. I will check which version we're running first, and see if we have the latest one. Otherwise I will get back to you with the information that you need.

Best,

Elin

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

Wed Feb 09, 2022 10:20 am

Hello,

Thanks for sharing the information via email.
I tested the logo images you provided and found that using the PS method to convert word to PDF, the image in the result file is clearer, please refer to the code below.
Code: Select all
    …
    drawVeckobrevLogo(document, imagePath, horPos, verPos)
    Dim ps As ToPdfParameterList = New ToPdfParameterList()
    ps.UsePSCoversion = True
document.SaveToFile("result.pdf", ps)

In addition, you mentioned the issue that the picture in the PDF result file becomes larger. I found that if you use Microsoft Word to convert word to PDF, the picture size in the result file is the same as the result of using our product. So we will not fix this issue, I hope you can understand.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Return to Spire.Doc