Hello Aspose,
We change the headers in the documents that are uploaded. Because sometime the uploaded document has some weird settings, we try to clear as much formatting as possible.
In the document that’s attached, has some indentation settings that isn’t cleared. Is there something we can add to also clear this ?
Regards,
Inception
Aspose-example-header-indent.docx (23.8 KB)
Resulting.pdf (60.0 KB)
Code:
Document aAsposeDoc = new Document(lFileName);
for ( Section lSection : aAsposeDoc.getSections() ) {
// Remove all existing Headers/Footers
for ( HeaderFooter lHeaderFooter : lSection.getHeadersFooters() ) {
if ( lHeaderFooter != null ) {
lHeaderFooter.remove();
}
}
}
DocumentBuilder aDocumentBuilder = new DocumentBuilder( aAsposeDoc );
aDocumentBuilder.moveToHeaderFooter( HeaderFooterType.HEADER_PRIMARY );
aDocumentBuilder.getCurrentParagraph().getParagraphFormat().setAlignment( ParagraphAlignment.CENTER );
aDocumentBuilder.getCurrentParagraph().getParagraphFormat().setLeftIndent( 0.0d );
aDocumentBuilder.getCurrentParagraph().getParagraphFormat().setRightIndent( 0.0d );
com.aspose.words.Table lTable = aDocumentBuilder.startTable();
com.aspose.words.Cell lAsposeCell = aDocumentBuilder.insertCell();
lAsposeCell.getCellFormat().clearFormatting();
lAsposeCell.getCellFormat().getShading().clearFormatting();
lAsposeCell.getCellFormat().getBorders().clearFormatting();
lAsposeCell.getCellFormat().getBorders().getTop().setLineStyle( LineStyle.SINGLE );
lAsposeCell.getCellFormat().getBorders().getTop().setLineWidth( 1d );
lAsposeCell.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE );
lAsposeCell.getCellFormat().getBorders().getBottom().setLineWidth( 1d );
lAsposeCell.getCellFormat().getBorders().getLeft().setLineStyle( LineStyle.SINGLE );
lAsposeCell.getCellFormat().getBorders().getLeft().setLineWidth( 1d );
lAsposeCell.getCellFormat().getBorders().getRight().setLineStyle( LineStyle.SINGLE );
lAsposeCell.getCellFormat().getBorders().getRight().setLineWidth( 1d );
lAsposeCell.getCellFormat().setPreferredWidth( PreferredWidth.fromPercent( 50 ) );
aDocumentBuilder.getParagraphFormat().clearFormatting();
aDocumentBuilder.getParagraphFormat().setSpaceBefore( 0d );
aDocumentBuilder.getParagraphFormat().setSpaceAfter( 0d );
aDocumentBuilder.getParagraphFormat().setLeftIndent( 0d );
aDocumentBuilder.getParagraphFormat().setRightIndent( 0d );
aDocumentBuilder.getParagraphFormat().setCharacterUnitFirstLineIndent( 0d ); // ParagraphFormat.FirstLineIndent will be updated
aDocumentBuilder.getParagraphFormat().setCharacterUnitLeftIndent( 0d ); // ParagraphFormat.LeftIndent will be updated
aDocumentBuilder.getParagraphFormat().setCharacterUnitRightIndent( 0d );
aDocumentBuilder.getParagraphFormat().setLineUnitBefore( 0d ); // ParagraphFormat.SpaceBefore will be updated
aDocumentBuilder.getParagraphFormat().setLineUnitAfter( 0d );
aDocumentBuilder.write( "TEST LEFT" );
lAsposeCell = aDocumentBuilder.insertCell();
aDocumentBuilder.write( "TEST RIGHT" );
aDocumentBuilder.endRow();
aDocumentBuilder.getCurrentSection();
aDocumentBuilder.endTable();
aAsposeDoc.updateFields();
aAsposeDoc.updatePageLayout();
aAsposeDoc.save(lSaveName);