Table api

Can I do this?

This is only what I want to do.
but I couldn’t find some feature

smaple.docx (19.4 KB)

@wogus1919 You can easily build a table using DocumentBuilder. Please see our documentation for more information:
https://docs.aspose.com/words/net/introduction-and-creating-tables/

Also, please see the following code example:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
// Build the first row
builder.InsertCell();
builder.CellFormat.Width = 240;
builder.InsertImage(@"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");
builder.InsertCell();
builder.CellFormat.Width = 240;
builder.InsertImage(@"https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");
builder.EndRow();
// Build the second row
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 120;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 120;
builder.Write("test");
builder.EndRow();
// Build the third row.
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 80;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 120;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 40;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 80;
builder.Write("test");
builder.InsertCell();
builder.CellFormat.Width = 120;
builder.Write("test");
builder.EndRow();
builder.EndTable();

table.AutoFit(AutoFitBehavior.FixedColumnWidths);

doc.Save(@"C:\Temp\out.docx");

Table table = builder.StartTable();

symbol: method StartTable()
location: variable builder of type DocumentBuilder

This error occurs. Do I need something else to use the StartTable?

@wogus1919 The provided code example is in C#, you are using Java. Here is the Java version of the same code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.startTable();
// Build the first row
builder.insertCell();
builder.getCellFormat().setWidth(240);
builder.insertImage("https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");
builder.insertCell();
builder.getCellFormat().setWidth(240);
builder.insertImage("https://cms.admin.containerize.com/templates/aspose/App_Themes/V3/images/aspose-logo.png");
builder.endRow();
// Build the second row
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(120);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(120);
builder.write("test");
builder.endRow();
// Build the third row.
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(80);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(120);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(80);
builder.write("test");
builder.insertCell();
builder.getCellFormat().setWidth(120);
builder.write("test");
builder.endRow();
builder.endTable();

table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

doc.save("C:\\Temp\\out.docx");

And here is link to the article in our Java documentation:
https://docs.aspose.com/words/java/introduction-and-creating-tables/

Thank you.

Where is the font size, table width, and vertical alignment method?

@wogus1919 you can set cell vertical alignment using CellFormat.VerticalAlignment property, the font using Font.Name property. For example see the following code:

builder.insertCell();
builder.getCellFormat().setWidth(40);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.getFont().setName("Arial");
builder.write("test");

Please see the following article to learn about setting table and cell widths:
https://docs.aspose.com/words/java/specifying-table-and-cell-widths/

thank you

  • Can you give me the table height?

  • I’d like to put the text on the table. How do you style these texts?
    I can’t enter the code as below.

builder.write("호우호우 테스트 123");
builder.getFont().setName("Malgun Gothic");
builder.getFont().setSize(6);

@wogus1919

There is no way to set height of whole table in both MS Word and Aspose.Words. However, you can specify row height. Please see our documentation for mro einformation:
https://docs.aspose.com/words/java/applying-formatting/#applying-formatting-on-the-row-level

You should first specify the formatting and then insert text:

builder.getFont().setName("Malgun Gothic");
builder.getFont().setSize(6);
builder.write("호우호우 테스트 123");

thank you

I gave the horizontal value to the table above the image. The image doesn’t shrink and the table gets really big.
Can’t you give the width value to the image?

builder.getCellFormat().setWidth(300);
builder.insertImage(img);

@wogus1919 You can specify image size by passing it into the insertImage method as parameters. For example see the following code:

builder.getCellFormat().setWidth(240);
builder.insertImage("C:\\Temp\\img.png", 240, 240);

Alternatively, you can specify width after insertion the image. For example the following code demonstrates how to change image width keeping it’s aspect ratio:

Shape img = builder.insertImage("C:\\Temp\\img.png");
img.setAspectRatioLocked(true);
img.setWidth(240);

thank you

I want to process the table cell in 2 lines like a sample. What should I do?

sample.docx

@wogus1919 You can use a paragraph break using code like this:

builder.insertCell();
builder.writeln("first paragraph");
builder.write("second paragraph");

or using soft line break:

builder.insertCell();
builder.write("Lines separated\u000bby soft line break");

Thank you.
I have two questions.

  • When you look at the lines on the table on the mobile,
    The images are overlaid.
    How can I fit the size of the table?
  • I want to place the text as a text box on the bottom left. How do I place the text where it says here?
    sample.docx (1.4 MB)

@wogus1919

  1. Could you please attach your source document here for testing? Also, to what format is the document converted to view it on mobile device?

  2. You can can create a textbox shape and specify it’s absolute position:

Document doc = new Document();

// Create shape and specify it's position.
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);
textBox.setStroked(true);
textBox.setWidth(200);
textBox.setHeight(50);
textBox.setWrapType(WrapType.NONE);
textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
textBox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
PageSetup ps = doc.getFirstSection().getPageSetup();
textBox.setLeft(ps.getLeftMargin());
textBox.setTop(ps.getPageHeight() - ps.getBottomMargin() - textBox.getHeight());
// Add content to the TextBox.
Paragraph para = new Paragraph(doc);
para.appendChild(new Run(doc, "This is Text of TextBox"));
textBox.appendChild(para);
// Insert shape into the document.
doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);

doc.save("C:\\Temp\\out.docx");

Proceed to print mode.

  1. table code
int cellWidth = 260;
int imgWidth = 260;
builder.insertCell();
builder.getCellFormat().setWidth(cellWidth);
Shape shapeImg = builder.insertImage(img);
shapeImg.setAspectRatioLocked(true);
shapeImg.setWidth(imgWidth);

@wogus1919 Thank you for additional information. But unfortunately, it is still not clear what is the target format and how to reproduce the problem. Could you please attach your output document that demonstrates the problem? We will check the issue and provide you more information.
Also, what viewer application do you use on your mobile device?

Thank you. The background color behind the text box is white, so can I make it transparent?

@wogus1919 Sure, simply set Filled property of the shape to false:

textBox.setFilled(false);

Thank you.
I have two questions.

  • Can I put a table in the text box?
    Is the text box center alignment function possible?
  • The first section has an empty page.
    How do I delete an empty page? Below is the code I tried.
out.getSections().remove(0);

@wogus1919

  1. You should simply specify paragraph alignment to center. Please see our documentation to learn how specify paragraph formatting.
paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
  1. There are several methods how page break can be inserted into the MS Word document - explicit page break; page break before property of a paragraph; section break; bunch of empty paragraphs that occupy whole page. Could you please attach your document here for our reference, we will check it and let you know what causes an empty page in your particular case.