Formatting font and paragraph style of document using java

Hi,

I have a default word document, that I need to print in different font and paragraph setting as per customer suggestion. So can you please help me to change the entire document style without any change in content. I am attaching the sample default document sample.docx (103 KB)
.

@Gptrnt

You can use following code example to change the font name and size of text in Word document. Hope this helps you.

If you still face problem, please share some more detail about your requirement along with expected output document. We will then answer your query accordingly.

public class FontChanger extends DocumentVisitor
{
	  ///
	  /// Called when a FieldEnd node is encountered in the document.
	  ///
	  public int VisitFieldEnd(final FieldEnd fieldEnd)
	  {
	  	//Simply change font name
	  	ResetFont(fieldEnd.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a FieldSeparator node is encountered in the document.
	  ///
	  public int VisitFieldSeparator(final FieldSeparator fieldSeparator)
	  {
	  	ResetFont(fieldSeparator.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a FieldStart node is encountered in the document.
	  ///
	  public int VisitFieldStart(final FieldStart fieldStart)
	  {
	  	ResetFont(fieldStart.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a Footnote end is encountered in the document.
	  ///
	  public int VisitFootnoteEnd(final Footnote footnote)
	  {
	  	ResetFont(footnote.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a FormField node is encountered in the document.
	  ///
	  public int VisitFormField(final FormField formField)
	  {
	  	ResetFont(formField.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a Paragraph end is encountered in the document.
	  ///
	  public int VisitParagraphEnd(final Paragraph paragraph)
	  {
	  	ResetFont(paragraph.getParagraphBreakFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a Run node is encountered in the document.
	  ///
	  public int visitRun(final Run run)
	  {
	  	ResetFont(run.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  ///
	  /// Called when a SpecialChar is encountered in the document.
	  ///
	  public int VisitSpecialChar(final SpecialChar specialChar)
	  {
	  	ResetFont(specialChar.getFont());
	  	return VisitorAction.CONTINUE;
	  }
	
	  private void ResetFont(Font font)
	  {
	  	font.setName(mNewFontName);
	  	font.setSize(mNewFontSize);
	  }


	  private String mNewFontName = "Arial";
	  private double mNewFontSize = 19.0;
}
Document doc = new Document(MyDir + "input.docx");
FontChanger changer = new FontChanger();
doc.accept(changer);

doc.save(MyDir + "output.docx");

Hi,

I have also need to change paragraph format and margin of the document. Please me for the same

Thank you,

@Gptrnt Margins are defined per section, so you can update them by implementing visitSectionStart or visitSectionEnd. Please see Section.getPageSetup().
To change paragraph format implement visitParagraphStart or visitParagraphEnd. See Paragraph.getParagraphFormat() property.