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.

Sat Dec 09, 2023 8:43 pm

Hi,
We have document with custom page size.
In order to align objects to left upper corner we tried to remove headers and margins in document, but without success.
result.png

Please help to remove any margins from document.
Code we tried (SpireDoc for Java 11.7.0):
Code: Select all
        Document document = new Document("test.docx");
        SectionCollection sections = document.getSections();
        for (int i = 0; i < sections.getCount(); i++) {
            sections.get(i).getHeadersFooters().getHeader().getChildObjects().clear();
            sections.get(i).getPageSetup().getMargins().setTop(0f);
            sections.get(i).getHeadersFooters().getFooter().getChildObjects().clear();
        }
        document.saveToFile("test_changed.docx");
    }


The source and generated documents are in attachments.

Thanks,
Andrei Chorin

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Mon Dec 11, 2023 5:56 am

Hello,

Thank you for your inquiry.
Regarding the Word document you provided, upon investigation, I found that if you wish to start adding content from the top left corner, it is necessary to remove the artistic text watermark that was added through the header.
Below is an example code snippet that demonstrates how to achieve this:
Code: Select all
Document document = new Document("test.docx");
SectionCollection sections = document.getSections();
// Set the watermark as null to remove the watermark.
document.setWatermark(null);
for (int i = 0; i < sections.getCount(); i++) {
    sections.get(i).getHeadersFooters().getHeader().getChildObjects().clear();
    sections.get(i).getHeadersFooters().getFooter().getChildObjects().clear();
    sections.get(i).getPageSetup().setHeaderDistance(0);
    sections.get(i).getPageSetup().setFooterDistance(0);
    sections.get(i).getPageSetup().getMargins().setAll(0);
}
document.saveToFile("test_changed.docx");

If you have any further questions or need additional assistance, please feel free to let us know. We are here to help.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Mon Dec 11, 2023 7:25 am

Thanks a lot.

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Mon Dec 11, 2023 7:43 am

Hello,

You're welcome.
If you encounter other issues related to our products in the future, please feel free to contact us.
Have a nice day.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Mon Dec 18, 2023 3:24 pm

Thanks a lot for solution.

andrei.chorin
 
Posts: 68
Joined: Sun Jan 08, 2023 2:36 pm

Return to Spire.Doc