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.

Mon Feb 05, 2024 8:24 am

hello,
i'm trying to append picture to first page of a document.
how can I place the image on the entire page width? with no margin on the right..

Screenshot 2024-02-05 at 10.14.42.png


please assist to remove the right margin from the picture, but NOT from the entire page
(notice that the text below to the picture should have margins).


Code: Select all
DocPicture image = section.addParagraph().appendPicture(imageSection.getImage());
image.setTextWrappingStyle(TextWrappingStyle.Square);
image.setHorizontalPosition(0f);
image.setHorizontalOrigin(HorizontalOrigin.Page);
image.setHorizontalAlignment(ShapeHorizontalAlignment.Left);

image.setVerticalPosition(0f);
image.setVerticalOrigin(VerticalOrigin.Page);
image.setVerticalAlignment(ShapeVerticalAlignment.Top);
image.setVerticalAlignment(ShapeVerticalAlignment.Bottom);

float boundWidth = section.getPageSetup().getMargins().getLeft()
       + section.getPageSetup().getMargins().getRight()
       + section.getPageSetup().getClientWidth();
float boundHeight = (float) section.getPageSetup().getPageSize().getHeight();

if (picture.getWidth() != boundWidth) {
      newWidth = boundWidth;
      newHeight = (newWidth * picture.getHeight()) / picture.getWidth();
}

image.setWidth(newWidth);
image.setHeight(newHeight);



thank you
Adi

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Mon Feb 05, 2024 9:27 am

Hello,

Thank you for your inquiry.
Please refer to the code below to fulfill your requirement:
Code: Select all
Document document = new Document();
document.loadFromFile("adiWord.docx");
Section section = document.getSections().get(0);
float boundWidth = section.getPageSetup().getMargins().getLeft()
        + section.getPageSetup().getMargins().getRight()
        + section.getPageSetup().getClientWidth();
float newWidth = 0;
float newHeight = 0;
for (int i = 0; i < section.getParagraphs().getCount(); i++) {
    Paragraph paragraph = section.getParagraphs().get(i);
    for (int j = 0; j < paragraph.getChildObjects().getCount(); j++) {
        DocumentObject object = paragraph.getChildObjects().get(j);
        if (object.getDocumentObjectType().equals(DocumentObjectType.Picture)){
            DocPicture picture = (DocPicture) object;
            float pictureWidth = picture.getWidth();
            float pictureHeight = picture.getHeight();
            if (pictureWidth != boundWidth) {
                newWidth = boundWidth;
                newHeight = newWidth / pictureWidth * pictureHeight;
            }
            picture.setWidth(newWidth);
            picture.setHeight(newHeight);
        }
    }
}
document.saveToFile("result.docx", FileFormat.Docx_2013);

If you have any further questions or need assistance with anything else, please don't hesitate to let me know.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Mon Feb 05, 2024 12:33 pm

Hello,

I done what you suggest. but there is still a gap on the right margin between the image to the page.

Code: Select all
    @Override
    public void createImage(Section section, ImageSection imageSection, boolean isImageFirst) {
        if (imageSection == NO_SECTION || ArrayUtils.isEmpty(imageSection.getImage())) {
            return;
        }
        DocPicture image = section.addParagraph().appendPicture(imageSection.getImage());
        image.setTextWrappingStyle(TextWrappingStyle.Square);

        image.setHorizontalPosition(DEFAULT_POSITION);
        image.setHorizontalOrigin(HorizontalOrigin.Page);
        image.setHorizontalAlignment(ShapeHorizontalAlignment.Left);

        image.setVerticalPosition(DEFAULT_POSITION);
        image.setVerticalOrigin(VerticalOrigin.Page);
        if (isImageFirst) {
            image.setVerticalAlignment(ShapeVerticalAlignment.Top);
        } else {
            image.setVerticalAlignment(ShapeVerticalAlignment.Bottom);
        }

        float boundWidth = section.getPageSetup().getMargins().getLeft()
                + section.getPageSetup().getMargins().getRight()
                + section.getPageSetup().getClientWidth();
        float newWidth = 0;
        float newHeight = 0;

        for (int i = 0; i < section.getParagraphs().getCount(); i++) {
            Paragraph paragraph = section.getParagraphs().get(i);
            for (int j = 0; j < paragraph.getChildObjects().getCount(); j++) {
                DocumentObject object = paragraph.getChildObjects().get(j);
                if (object.getDocumentObjectType().equals(DocumentObjectType.Picture)){
                    DocPicture picture = (DocPicture) object;
                    float pictureWidth = picture.getWidth();
                    float pictureHeight = picture.getHeight();
                    if (pictureWidth != boundWidth) {
                        newWidth = boundWidth;
                        newHeight = newWidth / pictureWidth * pictureHeight;
                    }
                    picture.setWidth(newWidth);
                    picture.setHeight(newHeight);
                }
            }
        }
    }


please assist with placing the image on the entire page width with no margins.
my result document attached.
thank you

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Tue Feb 06, 2024 1:31 am

Hello,

Thank you for your feedback.
Based on the code you provided, I tested your Word document again, but it still didn't reproduce the issue you mentioned. I am attaching my complete test code and result file for your reference. If the issue still exists after you test my code, please provide the following information for further investigation. Thanks in advance.
1) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).
2) The version of the JDK you are using, e.g. jdk1.8.
In addition, the version of Spire.Doc I am using is Spire.Doc for Java Version:12.1.16. Is this the version you are using? If not, I suggest you upgrade to this version before testing.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Tue Feb 06, 2024 9:59 am

hello,
thank you for your feedback.
currently i'm working with Spire.Doc version:11.7.0.

I cannot upgrade my spire doc into Spire.Doc version:12.1.16 due to this open issue: sipedoc-12-1-0-convertion-to-pdf-font-problems-t12748.html?sid=6e0cc446951cdc4de68b35022d38e3bb

do you have any other suggestion for me in the mean while?
relay on the following information:
1) my test OS info: MacBook Pro Core i7, 16-GB , region: Israel
2) my JDK version:17.0.7

thanks again for your assist.

adi.zahi
 
Posts: 7
Joined: Tue Jan 30, 2024 10:12 am

Wed Feb 07, 2024 3:29 am

Hello,

Thank you for the detailed information.
I tested your Word document on macOS using Spire.Doc version:11.7.0 and Spire.Doc version:12.1.16, and the results were fine, the width of the image was the same as the width of the page. Attached here is the jar package I typed for the project I tested on mac. Please test again on your side.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Return to Spire.Doc