我需求是将源文档的内容只要是文本的就将字号调大,最终生成一个新的文档。这种方式是不是逐步遍历这个文档对象模型树,将每个节点序列化后写入输出流。举个例子:我的节点有 图片1:300M 段落1:50M 图片2:300M 段落2:50M 当程序遍历到段落2的时候,内存中是不是只会加载图片1,段落1,图片2的部分基本信息,而不是全部加载微信图片_20230919151702.png (100.8 KB)
@SalesDhorde, 您可以使用 DocumentVisitor 更改字体大小,如下例所示:
InputStream srcStream = new FileInputStream("aa.docx");
OutputStream destStream = new FileOutputStream("ccccc.docx");
Document srcDoc = new Document(srcStream);
srcDoc.accept(new FontChanger(2));
srcDoc.save(destStream, SaveFormat.DOCX);
srcStream.close();
destStream.close();
public static class FontChanger extends DocumentVisitor {
private double mFontSizeFactor = 1;
private void changeFont(Font font)
{
double newFontSize = font.getSize() * mFontSizeFactor;
font.setSize(newFontSize);
}
public FontChanger(double fontSizefactor)
{
mFontSizeFactor = fontSizefactor;
}
/// <summary>
/// Called when a FieldEnd node is encountered in the document.
/// </summary>
@Override
public int visitFieldEnd(FieldEnd fieldEnd)
{
//Simply change font name
changeFont(fieldEnd.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldSeparator node is encountered in the document.
/// </summary>
@Override
public int visitFieldSeparator(FieldSeparator fieldSeparator)
{
changeFont(fieldSeparator.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FieldStart node is encountered in the document.
/// </summary>
@Override
public int visitFieldStart(FieldStart fieldStart)
{
changeFont(fieldStart.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Footnote end is encountered in the document.
/// </summary>
@Override
public int visitFootnoteEnd(Footnote footnote)
{
changeFont(footnote.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a FormField node is encountered in the document.
/// </summary>
@Override
public int visitFormField(FormField formField)
{
changeFont(formField.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Paragraph end is encountered in the document.
/// </summary>
@Override
public int visitParagraphEnd(Paragraph paragraph)
{
changeFont(paragraph.getParagraphBreakFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
@Override
public int visitRun(Run run)
{
changeFont(run.getFont());
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a SpecialChar is encountered in the document.
/// </summary>
@Override
public int visitSpecialChar(SpecialChar specialChar)
{
changeFont(specialChar.getFont());
return VisitorAction.CONTINUE;
}
}
请参考我们的文档: