Table api

I already distributed it to the server, so it will be a little hard to remove the image, so I am attaching the docx file to Google Drive.
https://docs.google.com/document/d/1VqBMsNw3FF2z4cFZwFoB2TDACk6E85Wp/edit?usp=sharing&ouid=101771540585367309678&rtpof=true&sd=true

@wogus1919 Thank you for additional information. I cannot reproduce the problem on my side. The text is rendered fin in the PDF document.
However, I see in your PDF document Liberation Serif font is used. Most likely, this is the first font available in your environment and it does not have the required glyphs:

In my PDF document Times New Roman and Batang fonts are used:

So you need to provide the required fonts to get the PDF document rendered properly.

Then how can I add fonts?
Can you show me an example in arial font?

@wogus1919 You can set the font using Font.Name property:

builder.getFont().setName("Arial");
builder.write("This is Arial Text");

But the font should be available to Aspose.Words when document is rendered to PDF or any other Fixed Page formats. Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/

FontSettings.getDefaultInstance().setFontsSources(
    new FontSourceBase[] { new SystemFontSource(), new FolderFontSource("C:\\NotoSansKR-Bold.otf\\", true) });

When you add system fonts,
Do I have to add an extension as well? Like the example above?

https://fonts.google.com/noto/specimen/Noto+Sans+KR

@wogus1919

  • When SystemFontSource is specified Aspose.Words looks for fonts installed in the OS, in known fonts locations, like /usr/share/fonts, /usr/local/share/fonts etc. in Linux, or C:\Windows\Fonts in Windows.

  • When FolderFontSource is specified, Aspose.Words looks for fonts in the specified folder. It is not required to specify extension of the file. You should specify a path to the folder with fonts.

Is it possible to save the font file in a subfolder like a web font and then load it?
Or is it possible to only get the system font?

@wogus1919 Yes, you can download your fonts into any accessible folder and then specify this folder as FolderFontSource. Aspose.Words will look for fonts in this folder.

final com.aspose.words.Document out = new com.aspose.words.Document();
FontSettings.getDefaultInstance().setFontsSources(new FontSourceBase[] { new SystemFontSource(), new FolderFontSource("/src/main/fonts/NanumGothic.ttf", true) });

builder.getFont().setName("Nanum Gothic");
builder.write("This is Arial Text");

I did it like this. The font does not change. ㅜㅠ

@wogus1919 You have specified font file name instead of folder name in your code. Please change this:

new FolderFontSource("/src/main/fonts/NanumGothic.ttf", true)

to this:

new FolderFontSource("/src/main/fonts/", true)
  1. Even if I change it like this, the font doesn’t change.
    Do I have to do setName with a file name instead of a font name?

new FolderFontSource("/src/main/fonts/", true) builder.getFont().setName("Nanum Gothic");

  1. If this font is not installed in the system, can you not see it?

@wogus1919 Please make sure Nanum Gothic font is available. You can implement IWarningCallback to get notification when Aspose.Words cannot find the requested font and substitutes it. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
        
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[] { new SystemFontSource(), new FolderFontSource("C:\\Temp\\fonts", true) });
    
builder.getFont().setName("Nanum Gothic");
builder.write("This is Nanum Gothic Text");
        
doc.setWarningCallback(new FontSubstitutionWarningCollector());
        
doc.save("C:\\Temp\\out.pdf");
private static class FontSubstitutionWarningCollector implements IWarningCallback {

    public void warning(WarningInfo info) {
        if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
            System.out.println(info.getDescription());
    }
}

thank you
After installing fonts on the server, the pdf comes out properly.

I have a question. Is there a way to get table height like section.getPageSetup()?
section.getPageSetup();

@wogus1919 As you may know MS Word documents are flow documents and does not contain any information about document layout. The consumer applications like MS Word or Open Office build the document layout on the fly. Aspose.Words has it’s own document layout engine. The facade classes LayoutCollector and LayoutEnumerator that allow to get layout information of document elements.
You can use code like the following to get bounding box of tables in your document:

// Open document
Document doc = new Document("C:\\Temp\\in.docx");

// Create LayoutCollector and LayoutEnumerator classes to get layout information of nodes.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Calculate bounding boxes of table in the document.
Iterable<Table> tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table t : tables)
{
    // Skip tables which are in header footer(LayoutCollector and LayoutEnumerator classes do not work with header/footer nodes)
    if (t.getAncestor(NodeType.HEADER_FOOTER) != null)
        continue;

    // Move LayoutEnumerator to the first row
    enumerator.setCurrent(collector.getEntity(t.getFirstRow().getFirstCell().getFirstParagraph()));
    while (enumerator.getType() != LayoutEntityType.ROW)
        enumerator.moveParent();

    //Get rectangle of the first row of the table.
    Rectangle2D first_rect = enumerator.getRectangle();

    // Do the same with last row
    enumerator.setCurrent(collector.getEntity(t.getLastRow().getFirstCell().getFirstParagraph()));
    while (enumerator.getType() != LayoutEntityType.ROW)
        enumerator.moveParent();

    // Get rectangle of the last row in the table.
    Rectangle2D last_rect = enumerator.getRectangle();
    // Union of the rectangles is the bounding box of the table.
    Rectangle2D result_rect = first_rect.createUnion(last_rect);

    System.out.println("Table rectangle : x=" + result_rect.getX() + ", y=" + result_rect.getY() + ", width=" + result_rect.getWidth() + ", height=" + result_rect.getHeight());
}

Please note, the code is simplified to demonstrate the basic technique and converts only tables placed on a single page. In MS Word tables can span more than one page.

thank you

  1. I want to know how to specify the thickness of the table line.
  2. In Word, you can specify the height from Table -> Table Properties -> Row -> Height Designation, is it possible to specify the height of the table? There is only text in the table cell.

@wogus1919

  1. You can use Border or BorderCollection classes properties to configure table border:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startTable();
// Specify cell border width
builder.getCellFormat().getBorders().setLineWidth(3);

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        builder.insertCell();
        builder.write("test");
    }
    builder.endRow();
}
builder.endTable();

doc.save("C:\\Temp\\out.docx");
  1. You can specify row height and row height rule using the appropriate properties of RowFormat class.

There is also a height specification for row size in the word table properties, but there is a row height minimum/fixed function next to it. How do I use the fixation function?

@wogus1919 You can specify row height using RowFormat.Height property and height rule using RowFormat.HeightRule and HeightRule enum.
Please see our documentation to learn how to specify table formatting on Tabel, Row and Cell levels:
https://docs.aspose.com/words/java/applying-formatting/

Can I make a table like this?

@wogus1919 Sure, you can create the tables with merged cells using Aspose.Words. Please see our documentation to learn how to achieve this:
https://docs.aspose.com/words/java/working-with-merged-cells/