Free Support Forum - aspose.com

Multiple setText() on fragments of a table are breaking the pdf

Using java 8 and aspose.pdf v19.10.
The below code replace all blank cells with the “REPLACED TEXT”. But when changing font and font size, the table gets haywire.
Screenshot from 2019-12-02 12-51-43.png (4.8 KB)

All the empty cells, except the first one gets gibrish placed in them.
see the following code

for (AbsorbedCell cell : row.getCellList()) {
    for (TextFragment fragment : cell.getTextFragments()) {
        String token = fragment.getText().trim();
        if (token.length()==0) {
            fragment.getTextState().setFont(FONT);
            fragment.getTextState().setFontSize(FONT_SIZE);
            fragment.setText("REPLACED TEXT");
        }
    }
}

@jarvis447

Would you kindly share the sample PDF document along with complete code snippet that you are using. Also, please share the values for font size and font name so that scenario can be tested and addressed accordingly.

@asad.ali
Here is the code -

import com.aspose.pdf.AbsorbedCell;
import com.aspose.pdf.AbsorbedRow;
import com.aspose.pdf.AbsorbedTable;
import com.aspose.pdf.Document;
import com.aspose.pdf.Font;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.TableAbsorber;
import com.aspose.pdf.TextFragment;

import java.util.LinkedList;
import java.util.List;

public class Test {
    private static final String INPUT_FILE = "/Downloads/d.pdf";
    private static final String OUTPUT_FILE = "test.pdf";

    private static final Font FONT = FontRepository.openFont("ARIALN.TTF"); //Arial Narrow
    private static final float FONT_SIZE = 10f;

    public static void main(String[] args) {
        Document document = new Document(INPUT_FILE);
        replaceBlanks(document, 1, 1);
        document.save(OUTPUT_FILE);
    }


    // consider pageNumber and tableNumber index starting from 1
    public static void replaceBlanks(Document document, int pageNumber, int tableNumber) {
        String toReplaceWith = "REPLACED TEXT";
        List<AbsorbedTable> absorbedTables = new LinkedList<>();

        TableAbsorber absorber = new TableAbsorber();
        absorber.visit(document.getPages().get_Item(pageNumber));

        for (int j = 0; j < absorber.getTableList().size(); j++) {
            absorbedTables.add(absorber.getTableList().get_Item(j));
        }
        // sorting according to the Y co-ordinate of the table rectangle
        absorbedTables.sort((t1, t2) -> (int) (t2.getRectangle().getLLY() - t1.getRectangle().getLLY()));

        for (AbsorbedRow row : absorbedTables.get(tableNumber - 1).getRowList()) { // loop through rows
            for (AbsorbedCell cell : row.getCellList()) { // loop through row's cells
                for (TextFragment fragment : cell.getTextFragments()) {
                    String token = fragment.getText().trim();
                    if (token.length() == 0) {
                        fragment.getTextState().setFont(FONT);
                        fragment.getTextState().setFontSize(FONT_SIZE);
                        fragment.setText(toReplaceWith);
                    }
                }
            }
        }
    }
}

PDF -
d.pdf (11.9 KB)

@jarvis447

Thanks for sharing requested information.

We have tested the scenario using Aspose.PDF for Java 19.11 and were able to notice the issue. For the purpose of correction, we have logged it under the ticket ID PDFJAVA-39033 in our issue tracking system. We will further look into details of the issue and keep you informed with the status of its correction. Please spare us little time.

We are sorry for the inconvenience.

1 Like