Table api

thank you
I have two questions.

  • Is there any way to reduce the capacity of docx because the capacity is too large?
  • Can I upload docx with aws streaming?

@wogus1919

  1. The document size depend on it’s content. Usually DOCX format is quite compact, since this is a zip archive. But if your document contains data that cannot be compressed well, like large images, fonts, etc, I am afraid, there is no way to reduce the document size. There are few things you can do:

    • You can try specify CompressionLevel in OoxmlSaveOptions.
    • Reduce size of images before inserting them into the document.
    • Reduce number of images in the document.
  2. You can load document from stream and save it to stream so there is no limitations from Aspose.Words side from where to load document or where to save them.

How do I use the MAXIMUM maximum?
docx.save(bo, SaveFormat.DOCX.CompressionLevel(MAXIMUM));
Should I do it like the code above?

@wogus1919 Your code should look like this:

OoxmlSaveOptions opt = new OoxmlSaveOptions();
opt.setCompressionLevel(CompressionLevel.MAXIMUM);
doc.save("C:\\Temp\\out.docx", opt);

So you don’t use Save Format??
I save it in two forms: pdf and docx.
PdfSaveOptions options = new PdfSaveOptions();
SaveFormat.DOCX

@wogus1919 Yes, if it is required to specify additional options, you should use the Document.save overload that accepts SaveOptions as the second parameter. Please see our documentation for more information:
https://docs.aspose.com/words/java/specify-save-options/

Are you telling me to do this??
PdfSaveOptions options = new PdfSaveOptions(); options.setCompressionLevel(CompressionLevel.MAXIMUM);

@wogus1919 No. I mean OoxmlSaveOptions, just like I have shown in the code example above:

OoxmlSaveOptions opt = new OoxmlSaveOptions();
opt.setCompressionLevel(CompressionLevel.MAXIMUM);
doc.save("C:\\Temp\\out.docx", opt);

PdfSaveOptions are using when you save document to PDF. OoxmlSaveOptions are used when you save the document to DOCX.

@wogus1919 No. I mean OoxmlSaveOptions, just like I have shown in the code example above:

OoxmlSaveOptions opt = new OoxmlSaveOptions();
opt.setCompressionLevel(CompressionLevel.MAXIMUM);
doc.save("C:\\Temp\\out.docx", opt);

PdfSaveOptions are using when you save document to PDF. OoxmlSaveOptions are used when you save the document to DOCX.

I’d like to improve the image quality.
Is there a way to improve the quality of the image you put in the document?
out.docx (2.3 MB)

@wogus1919 Aspose.Words keeps the original image quality when you insert images in the document and cannot make the image quality better then it is actually is.
If possible, please provide the original image and simple code that you use to insert image into the document. We will check the issue on our side and provide you more information.

private static BufferedImage autoCropImage(BufferedImage image, int targetWidth, int targetHeight)
{
    double sourceRatio = (double)image.getWidth() / image.getHeight();
    double destRatio = (double)targetWidth / targetHeight;

    //이미지를 일단 폭에 맞춰 줄인다
    if (sourceRatio < destRatio)
    {
        int newWidth = 1200;
        int newHeight = (int)(newWidth / destRatio);
        BufferedImage resizedImage = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, newWidth, (int)(newWidth / sourceRatio), Scalr.OP_ANTIALIAS);
        int offsetY = (resizedImage.getHeight() - newHeight) / 2;
        BufferedImage croppedImage = Scalr.crop(resizedImage, 0, offsetY, newWidth, newHeight, Scalr.OP_ANTIALIAS);
        return croppedImage;
    }
    else
    {
        int newHeight = 1200;
        int newWidth = (int)(newHeight * destRatio);
        BufferedImage resizedImage = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, (int)(newHeight * sourceRatio), newHeight, Scalr.OP_ANTIALIAS);
        int offsetX = (resizedImage.getWidth() - newWidth) / 2;
        BufferedImage croppedImage = Scalr.crop(resizedImage, offsetX, 0, newWidth, newHeight, Scalr.OP_ANTIALIAS);
        return croppedImage;
    }
}

public void addCellImg(DocumentBuilder builder, BufferedImage img, int reportType, PageSetup ps) throws Exception
{
        float pageWidth = (float) (ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin());
        float pageHeight = (float) (ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin());
    pageHeight = pageHeight - 68;
        float rowHeight = pageHeight / 3;
        float rowWidth = pageWidth / 2;
    rowWidth = rowWidth - 3;
        int cellWidth = (int) rowWidth;
        int imgWidth = (int) cellWidth - 8;
        int cellHeight = (int) rowHeight - 43;
        int imgHegiht = cellHeight - 8;
    BufferedImage thumbImage = autoCropImage(img, imgWidth, imgHegiht);

    RowFormat rowFormat2 = builder.getRowFormat();
    rowFormat2.setHeight(cellHeight);
    rowFormat2.setHeightRule(HeightRule.EXACTLY);

    builder.insertCell();
    builder.getCellFormat().getBorders().setLineWidth(1.2);
    builder.getCellFormat().setPaddings(4, 4, 4, 4);
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
    builder.getCellFormat().setWidth(cellWidth);
    Shape shapeImg = builder.insertImage(thumbImage);

    shapeImg.setWidth(imgWidth);
    shapeImg.setAspectRatioLocked(false);
    shapeImg.setHeight(imgHegiht);
}

@wogus1919 Thank you for additional information. As I can see in your code you resize/crop the image using Scalr.resize and Scalr.crop methods. Most likely this image preprocessing causes the image quality degradation. Please try inserting the image without image preprocessing.

It takes a long time to produce a report because of the image import, is it possible to parallelize the table?

@wogus1919 Unfortunately, there is no way to paralyze table creation process. However, I would suggest you to consider using Mail Merge with regions or LINQ Reporting Engine for table creation. In this case you should configure a template and then fill it with data.

thank you
I’m drawing a circle in the shape of the image. The font comes out properly when I save it as word, but when I save it as pdf, the font gets cut off, so can I not cut the font in pdf by any chance?

[word image]
doc.jpg (107.9 KB)
[pdf image]
pdf.jpg (101.9 KB)

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

프로젝트 10월_202307212144.docx (42.2 KB)

public void defectAddRowCell(DocumentBuilder builder, JSONObject obj, String type, PageSetup ps, int no) throws Exception {
    float pageWidth = (float) (ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin());
    int rowWidth = (int) pageWidth - 2;
    int rowHeight = 21;
    int cellWidth = 50;
    int cellWidth2 = 90;
    int cellWidth3 = 40;
    int cellWidthmm = (int) (rowWidth - (cellWidth * 2) - (cellWidth2 * 2) - cellWidth3);

    RowFormat rowFormat = builder.getRowFormat();
    rowFormat.setHeight(rowHeight);
    rowFormat.setHeightRule(HeightRule.EXACTLY);

    Color customColor = Color.decode("#F1F2F2");
    Color whiteColor = Color.decode("#ffffff");

    if(type.equals("header")) {
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth3);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getFont().setSize(8);
        builder.getFont().setName("나눔고딕");
        builder.getCellFormat().getShading().setBackgroundPatternColor(customColor);
        builder.write("NO");

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("구분");

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write("위치(부재)");

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write("손상내용");

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidthmm);
        builder.write("폭 x 길이 x 개수");

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("비고");
        builder.endRow();
    } else {
        String floor_name = (String) obj.get("floorName");
        if(floor_name == null) floor_name = "";
        String sub_name = (String) obj.get("sub_name");
        if(sub_name == null) sub_name = "";
        String report_desc = (String) obj.get("report_desc");
        if(report_desc == null) report_desc = "";
        String table_type_text = (String) obj.get("table_type_text");
        if(table_type_text == null) table_type_text = "";

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth3);
        builder.getCellFormat().getShading().setBackgroundPatternColor(whiteColor);
        builder.write(String.valueOf(no));

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write(floor_name);
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write(sub_name);
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write(table_type_text);

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidthmm);
        builder.write(report_desc);

        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("");
        builder.endRow();
    }
}

If you look at the table on the last page
The width value of no cell is strange. If you look at the code, it’s cellWidth3 = 40, but I think it’s actually too big. Please check it out.

스크린샷 2023-07-21 오후 9.59.55.png (19.2 KB)

@wogus1919 Unfortunately, I cannot reproduce the problem on my side. I have used the following code for testing:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
        
builder.startTable();
defectAddRowCell(builder, "header", builder.getPageSetup(), 0);
for(int i=0; i< 20; i++)
    defectAddRowCell(builder, "", builder.getPageSetup(), 0);
    
doc.save("C:\\Temp\\out.docx");
public static void defectAddRowCell(DocumentBuilder builder, String type, PageSetup ps, int no) throws Exception {
    float pageWidth = (float) (ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin());
    int rowWidth = (int) pageWidth - 2;
    int rowHeight = 21;
    int cellWidth = 50;
    int cellWidth2 = 90;
    int cellWidth3 = 40;
    int cellWidthmm = (int) (rowWidth - (cellWidth * 2) - (cellWidth2 * 2) - cellWidth3);
        
    RowFormat rowFormat = builder.getRowFormat();
    rowFormat.setHeight(rowHeight);
    rowFormat.setHeightRule(HeightRule.EXACTLY);
        
    Color customColor = Color.decode("#F1F2F2");
    Color whiteColor = Color.decode("#ffffff");
        
    if(type.equals("header")) {
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth3);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getFont().setSize(8);
        builder.getFont().setName("나눔고딕");
        builder.getCellFormat().getShading().setBackgroundPatternColor(customColor);
        builder.write("NO");
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("구분");
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write("위치(부재)");
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write("손상내용");
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidthmm);
        builder.write("폭 x 길이 x 개수");
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("비고");
        builder.endRow();
    } else {
        String floor_name = "floorName";
        if(floor_name == null) floor_name = "";
        String sub_name = "sub_name";
        if(sub_name == null) sub_name = "";
        String report_desc = "report_desc";
        if(report_desc == null) report_desc = "";
        String table_type_text = "table_type_text";
        if(table_type_text == null) table_type_text = "";
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth3);
        builder.getCellFormat().getShading().setBackgroundPatternColor(whiteColor);
        builder.write(String.valueOf(no));
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write(floor_name);
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write(sub_name);
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth2);
        builder.write(table_type_text);
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidthmm);
        builder.write(report_desc);
            
        builder.insertCell();
        builder.getCellFormat().setWidth(cellWidth);
        builder.write("");
        builder.endRow();
    }
}

out.docx (8.6 KB)

스크린샷 2023-07-22 오전 1.55.40.png (19.7 KB)
The width property is a little strange.
Does the width attribute change when creating a table?? ㅠㅠ