output screenshot:
original file :
FV.docx (111.2 KB)
code snippet:
com.aspose.words.Document document = new com.aspose.words.Document(inputStream);
FontSettings fontSettings = FontSettings.getDefaultInstance();
com.aspose.words.FolderFontSource folderFontSource = new com.aspose.words.FolderFontSource(getFontDirectory(), true);
fontSettings.setFontsSources(new FontSourceBase[] { new SystemFontSource(), folderFontSource });
document.setFontSettings(fontSettings);
// document.updateTableLayout();
if (!StringUtils.isEmpty(runningHead))
{
HeaderFooter primaryHeader = document.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if (primaryHeader != null)
{
String primaryHeaderText = primaryHeader.getText();
if (!StringUtils.isEmpty(primaryHeaderText) && primaryHeaderText.toLowerCase().contains(runningHead.toLowerCase()))
primaryHeader.remove();
}
}
//justify the paragaraoh alignment
Iterable<Paragraph> paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph p : paragraphs)
{
if (p.getParagraphFormat().getAlignment() == ParagraphAlignment.DISTRIBUTED)
p.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
}
// as per https://forum.aspose.com/t/blank-pages-are-added-in-file/270804?u=tandfsp to avoid new page added by section break
for (Section s : document.getSections())
{
int start = s.getPageSetup().getSectionStart();
if (start == SectionStart.EVEN_PAGE || start == SectionStart.ODD_PAGE)
s.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
}
if (removeWaterMark)
{
Watermark watermark = document.getWatermark();
if (watermark.getType() == WatermarkType.TEXT)
{
watermark.remove();
}
}
if (removeLineNumbers)
{
PageSetup pageSetup = document.getFirstSection().getPageSetup();
pageSetup.setLineNumberCountBy(0);
}
com.aspose.words.PdfSaveOptions pdfSaveOptions = new com.aspose.words.PdfSaveOptions();
pdfSaveOptions.setUpdateFields(false);
document.save(directoryPath + PATH_SEPARATOR + outputFileName, pdfSaveOptions);
LOGGER.info(MessageConstants.CONVERTED_WORD_TO_PDF);
aspose version:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-total</artifactId>
<version>23.8</version>
<type>pom</type>
</dependency>