We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add table to existing PDF Document and Set its position using Aspose.PDF for Java

Add table to existing pdf can’t set ideal position

@Adnan.Ahmad

Hi there, I follow the example of AddTableInExistingPDFDocument to add table in existing pdf, but I can’t
set the position where I want(somewhere is near the right border), so I want konw is there any other examples that help me.

My File

document_with_table.pdf (283.6 KB)

Code

There is my code:

public static void addTableInExistingPDFDocument(String dataDir) {
        Document doc = new Document(dataDir + "demo.pdf");
        Table table = new Table();
        table.setTop(15f);
        table.setLeft(450f);
        table.setColumnWidths("50 50 50");
        table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.fromArgb(0, 45, 255)));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.Box, .5f, Color.fromArgb(0, 45, 255)));
        TextState textState = new TextState(10d);
        textState.setHorizontalAlignment(HorizontalAlignment.Center);
        table.setDefaultCellTextState(textState);
        table.setZIndex(9999);
        table.setInNewPage(true);
        table.setBackgroundColor(Color.getRed());

        MarginInfo margin = new MarginInfo();
        margin.setTop(5f);
        margin.setLeft(3f);
        margin.setRight(3f);
        margin.setBottom(5f);

        table.setDefaultCellPadding(margin);

        Row row1 = table.getRows().add();
        row1.getCells().add("全宗号");
        row1.getCells().add("年度");
        row1.getCells().add("件号");

        Row row2 = table.getRows().add();
        row2.getCells().add("问题");
        row2.getCells().add("保管期限");
        row2.getCells().add("页数");

        doc.getPages().get_Item(1).getParagraphs().add(table);
        doc.save(dataDir + "document_with_table.pdf");
    }

Thanks!

@JordanZhang,

Thanks for contacting support.

I have worked with sample code shared by you. Can you please share source PDF file along with desired result so that we may further investigate to help you out.

@ Adnan.Ahmad

Thanks for responding.
When I use table method setColumnAdjustment with ColumnAdjustment.AutoFitToContent , I almost get what I want. But the column content disordered. So I don’t konw how to do . I set the table position
with the method setTop and setLeft, I think it not working. And also the setDefaultColumnWidth.

Document doc = new Document(dataDir + "3c77da1ad8f84a08b606a89f2be05832.pdf");
Table table = new Table();
// I want fixed the table position
table.setAlignment(HorizontalAlignment.Right);
table.setTop(15f);
table.setLeft(425f);

//fixed the column widths
table.setColumnWidths("50 50 50");
table.setDefaultColumnWidth("50");
table.setCornerStyle(BorderCornerStyle.Round);
table.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
//set content center
table.setVerticalAlignment(VerticalAlignment.Center);

table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.fromArgb(0, 45, 255)));
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f, Color.fromArgb(0, 45, 255)));
table.getDefaultCellTextState().setFontSize(8F);
table.getDefaultCellTextState().setForegroundColor(Color.fromArgb(0, 45, 255));
table.getDefaultCellTextState().setHorizontalAlignment(HorizontalAlignment.Center);

table.setDefaultCellPadding(new MarginInfo(1f, 5f, 1f, 5f));

Row row1 = table.getRows().add();
row1.getCells().add("Item");
row1.getCells().add("Item");
row1.getCells().add("Item");

Row row2 = table.getRows().add();
row2.getCells().add("Item");
row2.getCells().add("Item");
row2.getCells().add("Item");

doc.getPages().get_Item(1).getParagraphs().add(table);
doc.save(dataDir + "document_with_table.pdf");

Source pdf

demo.pdf (50.5 KB)

The almost Result

nextTry.pdf (264.7 KB)

The desired result

desireResult.jpg (137.1 KB)

I have read all the API about Table and try times the whole day yesterday, but I never got what I wanted. So I really need your help, wish for your response again.

@JordanZhang,

I have observed issue you mentioned and have logged it as PDFJAVA-39215 in our issue tracking system. We will further look into details of the issue and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.

@ Adnan.Ahmad

Thanks again, if there’s any progress, please contact me. I really need to solve this problem.

Wish for your response!

@JordanZhang,

Sure, We will inform you.

The issues you have found earlier (filed as PDFJAVA-39215) have been fixed in Aspose.PDF for Java 20.5.