我可以知道我的文字再word里是否换行吗?他有几行?一段话他在word里有几行,这个aspose是否有相关api?
@xl1,
下面的代码将打印Word文档中每个段落的行数。
Document document = new Document("C:\\Temp\\word.docx");
var collector = new LayoutCollector(document);
var it = new LayoutEnumerator(document);
foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
{
var paraBreak = collector.GetEntity(paragraph);
object stop = null;
var prevItem = paragraph.PreviousSibling;
if (prevItem != null)
{
var prevBreak = collector.GetEntity(prevItem);
if (prevItem is Paragraph)
{
it.Current = collector.GetEntity(prevItem); // para break
it.MoveParent(); // last line
stop = it.Current;
}
else if (prevItem is Table)
{
var table = (Table)prevItem;
it.Current = collector.GetEntity(table.LastRow.LastCell.LastParagraph); // cell break
it.MoveParent(); // cell
it.MoveParent(); // row
stop = it.Current;
}
else
{
throw new Exception();
}
}
it.Current = paraBreak;
it.MoveParent();
// We move from line to line in a paragraph.
// When paragraph spans multiple pages the we will follow across them.
var count = 1;
while (it.Current != stop)
{
if (!it.MovePreviousLogical())
break;
count++;
}
const int MAX_CHARS = 16;
var paraText = paragraph.GetText();
if (paraText.Length > MAX_CHARS)
paraText = $"{paraText.Substring(0, MAX_CHARS)}...";
Console.WriteLine($"Paragraph '{paraText}' has {count} line(-s).");
}
如果这不是您要询问的内容,请通过提供用例的完整详细信息进一步详细说明您的询问。 这将有助于我们了解您的情况,并且我们将可以更好地解决您的问题。
额,我不懂js,请问有java的嘛?
@xl1,
我在上一篇文章中共享的C#代码的Java等效代码如下:
Document doc = new Document("C:\\Temp\\Queries.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator it = new LayoutEnumerator(doc);
for (Paragraph paragraph : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
Object paraBreak = collector.getEntity(paragraph);
Object stop = null;
com.aspose.words.Node prevItem = paragraph.getPreviousSibling();
if (prevItem != null) {
Object prevBreak = collector.getEntity(prevItem);
if (prevItem.getNodeType() == NodeType.PARAGRAPH) {
it.setCurrent(collector.getEntity(prevItem)); // para break
it.moveParent(); // last line
stop = it.getCurrent();
} else if (prevItem.getNodeType() == NodeType.TABLE) {
Table table = (Table) prevItem;
it.setCurrent(collector.getEntity(table.getLastRow().getLastCell().getLastParagraph())); // cell break
it.moveParent(); // cell
it.moveParent(); // row
stop = it.getCurrent();
} else {
throw new Exception();
}
}
it.setCurrent(paraBreak);
it.moveParent();
// We move from line to line in a paragraph.
// When paragraph spans multiple pages the we will follow across them.
int count = 1;
while (it.getCurrent() != stop) {
if (!it.movePreviousLogical())
break;
count++;
}
int MAX_CHARS = 16;
String paraText = paragraph.getText();
if (paraText.length() > MAX_CHARS)
paraText = paraText.substring(0, MAX_CHARS) + "...";
System.out.println("Paragraph '" + paraText + "' has " + count + " line(-s).");
}