Table api

Thank you.
The file doesn’t go up because of the file size.
Can I put a table in the text box? Below is the code I used.

Shape mapSectionName = new Shape(doc, ShapeType.TEXT_BOX);
Preformatted text`mapSectionName.setStroked(false);
mapSectionName.setWidth(newW);
mapSectionName.setHeight(30);

mapSectionName.setWrapType(WrapType.NONE);
mapSectionName.setFilled(false);
mapSectionName.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
mapSectionName.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
PageSetup ps = section.getPageSetup();
mapSectionName.setLeft(ps.getLeftMargin());
mapSectionName.setTop(ps.getPageHeight() - mapSectionName.getHeight() - mapSectionName.getHeight());
Paragraph para = new Paragraph(doc);

Table table = builder.startTable();
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();
builder.endTable();

para.appendChild(table);
mapSectionName.appendChild(para);

section.getBody().getFirstParagraph().appendChild(mapSectionName);

@wogus1919 Yes, you can put table into a textbox:

mapSectionName.appendChild(table);

Please see our documentation to learn more about Aspose.Words Document Object Model.

Thank you.
I’m sorry for asking so many questions.
LOL

  1. I’d like to give the table a top, bottom, left and right margin. What should I do?
  2. How do I centralize the text in the table?

@wogus1919

  1. You can use CellFormat.setPaddings method to specify inner table cell margins.

  2. You should specify paragraph alignment to achieve this. For example see the following code:

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

builder.startTable();
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.write("Left text");
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Center text");
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Right text");
builder.endRow();
builder.endTable();

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

1.When I add text box color, I can’t see the table line properly. Can I add background to the table?
2.Can I remove the stroke from the table?

@wogus1919

  1. Sure, you can use CellFormat().getShading() property to set background color of table cells:
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
  1. You can use CellFormat().getBorders() to set or reset borders of table cells. For example, the following code produces table with yellow background and without borders:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startTable();
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.write("Left text");
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Center text");
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Right text");
builder.endRow();
builder.endTable();

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

Please see our documentation to learn how to work with tables in Aspose.Words:
https://docs.aspose.com/words/java/working-with-tables/

Thank you.
I have a question about the image.
Can I put the image in 100px 100px cell in the table like css background-size:cover property?
If I can put it in, is it possible to background-position:center?

@wogus1919 It is not quite clear what you need to achieve. Could you please create the expected output in MS Word and attach the document (not screenshot) here for our reference? We will check it and provide you more information.

Thank you. I attached the document.
I want to crop two images with different ratios vertically long right image and keep the ratio on the left image.
sample.docx (6.3 MB)

@wogus1919 You can achieve this by setting zero inner margins for cells and specifying the same size of image and cell. For example see the following code:

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

builder.startTable();
builder.insertCell();
builder.getCellFormat().setWidth(200);
builder.getCellFormat().setPaddings(0, 0, 0, 0);
Shape shape1 = builder.insertImage("C:\\Temp\\image1.png");
shape1.setAspectRatioLocked(true);
shape1.setWidth(200);
builder.insertCell();
Shape shape2 = builder.insertImage("C:\\Temp\\image2.png");
shape2.setAspectRatioLocked(true);
shape2.setWidth(200);
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endRow();
Table t = builder.endTable();
t.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

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

out.docx (6.3 MB)

thank you
I have two questions.

  • I’m adding the text as the code below, but is there a padding or margin in this code?
builder.write('test');
  • I’d like to do two lines of border only on the tabletop. Is it possible?
  • After giving the padded jacket, the center of the English and Korean tables is not aligned. Can’t you sort the center?

@wogus1919

  1. Paddings are inherited when you build the table. So if you rest paddings in the first cell, they will remain the same in the second cell.

  2. In this case you need to specify bottom border of the first row.

  3. Do you mean vertical center alignment? If so you can use CellFormat.VerticalAlignment

Here is the modified code:

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

builder.startTable();
builder.insertCell();
builder.getCellFormat().setWidth(200);
// Set double line first row bottom border
builder.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.DOUBLE);
// Reset paddings.
builder.getCellFormat().setPaddings(0, 0, 0, 0);
Shape shape1 = builder.insertImage("C:\\Temp\\image1.png");
shape1.setAspectRatioLocked(true);
shape1.setWidth(200);
builder.insertCell();
Shape shape2 = builder.insertImage("C:\\Temp\\image2.png");
shape2.setAspectRatioLocked(true);
shape2.setWidth(200);
builder.endRow();
// The second row
builder.insertCell();
// Reset border style in the second row.
builder.getCellFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE);
// Set paddings for the second row.
builder.getCellFormat().setPaddings(3, 3, 3, 3);
// Set vertical alignment in the cell
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
// Set horizontal alignment using paragraph alignment.
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("center text");
builder.insertCell();
builder.write("center text (formatting is inherited)");
builder.endRow();
Table t = builder.endTable();
t.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

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

out.docx (6.3 MB)

Thank you.
Is it possible to save the file made in java word as a pdf file?

@wogus1919 Sure you can, simply specify PDF extension if you save document to file, or specify SaveFormat.PDF as the second parameter of the Document.save method:

doc.save("out.pdf");

or

doc.save("out.pdf", SaveFormat.PDF);

Please see our documentation to learn more about converting document to PDF:
https://docs.aspose.com/words/java/convert-a-document-to-pdf/

Also, please see the following article to learn what file formats are supported by Aspose.Words:
https://docs.aspose.com/words/java/supported-document-formats/

thank you
Can I check the metadata among the image data?
I found the crop function in the API document. I want to check the returned image, so I wonder if there is a way to check the metadata.
The purpose is to check if there is a direction in which the image is returned to orientation.

@wogus1919 Reading image files metadata is out of Aspose.Words scope. You can consider using Aspose.Imaging to process your images:
https://docs.aspose.com/imaging/java/features/

public void uploadDocx(com.aspose.words.Document docx, String keyPdf, String keyDocx) throws Exception {
    {
        PdfSaveOptions options = new PdfSaveOptions();
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        docx.save(bo, options);
        byte[] b = bo.toByteArray();
        ByteArrayInputStream bi = new ByteArrayInputStream(b);
        AwsS3.getInstance().upload(bi, keyPdf, "application/pdf", b.length);
    }
    {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        docx.save(bo, SaveFormat.DOCX);
        byte[] b = bo.toByteArray();
        ByteArrayInputStream bi = new ByteArrayInputStream(b);
        AwsS3.getInstance().upload(bi, keyDocx, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", b.length);
    }
}

Thank you. We need to find out more about the image.
I have one more question.
pdf and docx. I save them in two ways. Above is the code used.
The file saved as docx shows normal Korean.
Files saved as pdf show only numbers and symbols.
Did I save it wrong?
스크린샷 2023-03-09 오전 9.58.39.jpg (156.0 KB)

@wogus1919 Could you please attach your output DOCX and PDF documents here for our reference? We will check the issue and provide you more information.

The problem might occur because the fonts used in the source document are not available in the environment where the document is rendered to PDF. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the layout difference, since substitution fonts might have different font metrics. You can implement IWarningCallback to get a notification when font substitution is performed.

Or the font you are using for text does not have glyphs for Korean characters and Aspose.Words cannot find an alternative font with the required glyphs. This process is called font fallback:
https://docs.aspose.com/words/java/manipulate-and-substitute-truetype-fonts/#font-fallback-settings-from-xml

https://drive.google.com/file/d/1TnMlE4FhuQaPYKBohD4XdMF-CrBz2Gcy/view?usp=sharing
The pdf file is not uploaded because of the capacity, so I am attaching it to Google Drive and sharing it.

@wogus1919 You have shared only PDF document. Could you lease share DOCX document, so I can check the conversion to PDF on my side.
Also, since the problem occurs only with text, you can generate a document without images for testing purposes, this will significantly reduce the file size.