Hello, I use aspose word to save the doc as a PDF. When the table cell in the document spans pages, the border of this cell will be missing. How to deal with it?
@yjsdfsdf Could you please attach your source and output documents here for testing? we will check the issue and provide you more information.
Thank you, I solved it temporarily. Can I help handle another problem? It is to convert the DOC to PDF, and the title runs to the right side。
Downloads.zip (640.5 KB)
@yjsdfsdf Unfortunately, I cannot reproduce the problem using the latest 23.5 version of Aspose.Words for Java. Please try using the latest version.
Sorry, I I forgot to paste the code。
// Open document
Document doc = new Document(dataDir + "华泰柏瑞沪深300交易型开放式指数证券投资基金2021年年度报告.doc");
for (Table table:(Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true))
{
for (Row row:(Iterable<Row>)table.getChildNodes(NodeType.ROW, true))
{
for (Cell cell: (Iterable<Cell>)row.getChildNodes(NodeType.CELL, true))
{
cell.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE);
cell.getCellFormat().getBorders().getTop().setLineStyle(LineStyle.SINGLE);
}
}
}
Iterable<Cell> cells = doc.getChildNodes(NodeType.CELL, true);
LayoutCollector collector = new LayoutCollector(doc);
for (Cell cell:cells)
{
if (collector.getNumPagesSpanned(cell) > 0)
{
cell.getParentRow().getFirstCell().getFirstParagraph().getParagraphFormat().setPageBreakBefore(true);
collector.clear();
doc.updatePageLayout();
}
}
doc.save(dataDir + "finals地方.pdf");
@yjsdfsdf The problem occur because the table is floating and when content is moved document layout is changed. You can make the table inline to avoid such probems:
table.setTextWrapping(TextWrapping.NONE);
But the table contains a lot of empty rows underneath, which is why
Dingtalk_20230515131130.jpg (39.5 KB)
@yjsdfsdf Yes, this is side effect of the proposed workaround. You can remove empty paragraphs to avoid this:
Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph p : paragraphs)
{
if (IsEmptyParagraph(p) && IsEmptyParagraph(p.getNextSibling()))
{
p.remove();
}
}
private static boolean IsEmptyParagraph(Node node) throws Exception
{
if (node == null || node.getNodeType() != NodeType.PARAGRAPH)
return false;
Paragraph p = (Paragraph)node;
// Consider paragraph as empty if it does not have text and shapes.
return p.toString(SaveFormat.TEXT).trim().equals("") &&
(p.getChildNodes(NodeType.SHAPE, true).getCount() == 0);
}
Run this code before processing tables. But note all changes to the document node structure affects the document layout. So there is no general solution for all documents.
I used the following code to convert doc to PDF, and only one table had missing edges.
can you help me? thank you
// Open document
Document doc = new Document(dataDir + "华泰柏瑞健康生活灵活配置混合型证券投资基金2022年年度报告-1.doc");
for (Table table:(Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true))
{
table.setTextWrapping(TextWrapping.NONE);
for (Row row:(Iterable<Row>)table.getChildNodes(NodeType.ROW, true))
{
for (Cell cell: (Iterable<Cell>)row.getChildNodes(NodeType.CELL, true))
{
cell.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE);
cell.getCellFormat().getBorders().getTop().setLineStyle(LineStyle.SINGLE);
}
}
}
Iterable<Cell> cells = doc.getChildNodes(NodeType.CELL, true);
LayoutCollector collector = new LayoutCollector(doc);
for (Cell cell:cells)
{
if (collector.getNumPagesSpanned(cell) > 0)
{
cell.getParentRow().getFirstCell().getFirstParagraph().getParagraphFormat().setPageBreakBefore(true);
collector.clear();
doc.updatePageLayout();
}
}
doc.save(dataDir + "output.pdf");
Downloads.zip (810.1 KB)
@yjsdfsdf Please try to change order of calling Document.updatePageLayout
and LayoutCollector.clear
methods:
doc.updatePageLayout();
collector.clear();
It does not work, other tables will have problems, you can see the attached picture
@yjsdfsdf If the problem is the top border, you can set border around the whole table:
table.setBorders(LineStyle.SINGLE, 1, Color.BLACK);