إضافة جداول إلى PDF في Java

Aspose.PDF for Java توفر مجموعة غنية Table API لإنشاء الجداول مع تخصيص التخطيط والمحتوى.

إنشاء جدول أساسي

استخدم هذا المثال عندما تحتاج إلى إضافة جدول بسيط بحدود موحدة وخلايا نصية.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وهيّئ حدوده.
  3. أضف صفوفًا وخلايا، وأرفق الجدول بالصفحة، ثم احفظ المستند.
public static void createTable(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, 5, Color.getLightGray()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 5, Color.getLightGray()));
        for (int rowCount = 0; rowCount < 10; rowCount++) {
            Row row = table.getRows().add();
            row.getCells().add("Column (" + rowCount + ", 1)");
            row.getCells().add("Column (" + rowCount + ", 2)");
            row.getCells().add("Column (" + rowCount + ", 3)");
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة خلايا مع امتداد الصف وامتداد العمود

استخدم هذا المثال عندما تحتاج الجدول إلى خلايا مدمجة عبر الصفوف أو الأعمدة.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وأضف الصفوف.
  3. اضبط ColSpan و RowSpan على الخلايا المستهدفة، ثم احفظ ملف PDF..
public static void addRowspanOrColspan(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getBlack()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getBlack()));

        Row row1 = table.getRows().add();
        for (int cellCount = 1; cellCount < 5; cellCount++) {
            row1.getCells().add("Test 1" + cellCount);
        }

        Row row2 = table.getRows().add();
        row2.getCells().add("Test 2 1");
        Cell cell = row2.getCells().add("Test 2 2");
        cell.setColSpan(2);
        row2.getCells().add("Test 2 4");

        Row row3 = table.getRows().add();
        row3.getCells().add("Test 3 1");
        row3.getCells().add("Test 3 2");
        row3.getCells().add("Test 3 3");
        row3.getCells().add("Test 3 4");

        Row row4 = table.getRows().add();
        row4.getCells().add("Test 4 1");
        cell = row4.getCells().add("Test 4 2");
        cell.setRowSpan(2);
        row4.getCells().add("Test 4 3");
        row4.getCells().add("Test 4 4");

        Row row5 = table.getRows().add();
        row5.getCells().add("Test 5 1");
        row5.getCells().add("Test 5 3");
        row5.getCells().add("Test 5 4");

        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة حدود الجدول وتعبئة الخلايا

استخدم هذا المثال عندما تحتاج إلى تكوين الحدود والحشوة وسلوك تغليف الخلايا.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وضبط العرض والحدود والهوامش الداخلية.
  3. أضف صفوفًا واحفظ المستند الناتج.
public static void addBorders(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        page.getParagraphs().add(table);
        table.setColumnWidths("50 50 50");
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1f));
        table.setBorder(new BorderInfo(BorderSide.All, 1));
        table.setDefaultCellPadding(new MarginInfo(5, 5, 5, 5));

        Row row1 = table.getRows().add();
        row1.getCells().add("col1");
        row1.getCells().add("col2");
        row1.getCells().add();
        row1.getCells().get_Item(2).getParagraphs().add(new TextFragment("col3 with large text string"));
        row1.getCells().get_Item(2).setWordWrapped(false);

        Row row2 = table.getRows().add();
        row2.getCells().add("item1");
        row2.getCells().add("item2");
        row2.getCells().add("item3");
        document.save(outputFile.toString());
    }
}

تمكين تخطيط الجدول تلقائي التكيّف

استخدم هذا المثال عندما يجب أن يتم تعديل الجدول تلقائيًا لعرض الصفحة المتاح.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وعيّن ColumnAdjustment.AutoFitToWindow.
  3. أضف صفوفًا عينةً واحفظ ملف PDF..
public static void autoFit(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        page.getParagraphs().add(table);
        table.setColumnWidths("50 50 50");
        table.setColumnAdjustment(ColumnAdjustment.AutoFitToWindow);
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1f));
        table.setBorder(new BorderInfo(BorderSide.All, 1));
        table.setDefaultCellPadding(new MarginInfo(5, 5, 5, 5));

        Row row1 = table.getRows().add();
        row1.getCells().add("col1");
        row1.getCells().add("col2");
        row1.getCells().add("col3");
        Row row2 = table.getRows().add();
        row2.getCells().add("item1");
        row2.getCells().add("item2");
        row2.getCells().add("item3");
        document.save(outputFile.toString());
    }
}

إضافة صورة داخل خلية جدول

استخدم هذا المثال عندما يحتاج الجدول إلى عرض محتوى صورة نقطية داخل أحد خلاياه.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وأضف صفًا يحتوي على خلايا نصية وصورية.
  3. اضبط Image حدد الحجم واحفظ المستند.
public static void addImage(Path imageFile, Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setColumnWidths("200 100");

        Row row = table.getRows().add();
        row.getCells().add().getParagraphs().add(new TextFragment(imageFile.toString()));
        Image image = new Image();
        image.setFile(imageFile.toString());
        image.setFixWidth(50);
        image.setFixHeight(50);
        row.getCells().add().getParagraphs().add(image);

        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة صور SVG داخل خلايا الجداول

استخدم هذا المثال عندما يجب على الجدول عرض ملفات SVG صفًا بصف.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وتكرار عبر ملفات SVG..
  3. أضف صفًا واحدًا لكل صورة، اضبط SVG Image، واحفظ ملف PDF..
public static void addSvgImage(List<Path> imageFiles, Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setColumnWidths("200 100");
        for (Path imageFile : imageFiles) {
            Row row = table.getRows().add();
            row.getCells().add().getParagraphs().add(new TextFragment(imageFile.toString()));
            Image image = new Image();
            image.setFileType(ImageFileType.Svg);
            image.setFile(imageFile.toString());
            image.setFixWidth(50);
            image.setFixHeight(50);
            row.getCells().add().getParagraphs().add(image);
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة مقتطفات HTML إلى خلايا الجداول

استخدم هذا المثال عندما ينبغي أن يتضمن محتوى الجدول تنسيق HTML مضمّن.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وتهيئة الحدود.
  3. أضف الكائنات HtmlFragment إلى الخلايا واحفظ المستند.
public static void addHtmlFragments(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray()));
        for (int rowCount = 1; rowCount < 10; rowCount++) {
            Row row = table.getRows().add();
            row.getCells().add().getParagraphs().add(new HtmlFragment("Column <strong>(" + rowCount + ", 1)</strong>"));
            row.getCells().add().getParagraphs().add(new HtmlFragment("Column <span style='color:red'>(" + rowCount + ", 2)</span>"));
            row.getCells().add().getParagraphs().add(new HtmlFragment("Column <span style='text-decoration: underline'>(" + rowCount + ", 3)</span>"));
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة مقاطع LaTeX إلى خلايا الجدول

استخدم هذا المثال عندما يجب أن يعرض محتوى الجدول تعابير TeX أو LaTeX.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table مع الحدود.
  3. أضف الكائنات TeXFragment إلى الخلايا واحفظ ملف الإخراج.
public static void addLatexFragments(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray()));
        for (int rowCount = 1; rowCount < 10; rowCount++) {
            Row row = table.getRows().add();
            row.getCells().add().getParagraphs().add(new TeXFragment("Column $\\mathbf{(" + rowCount + ", 1)}$"));
            row.getCells().add().getParagraphs().add(new TeXFragment("Column $\\textcolor{red}{(" + rowCount + ", 2)}$"));
            row.getCells().add().getParagraphs().add(new TeXFragment("Column $\\underline{(" + rowCount + ", 3)}$"));
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إجبار جدول على صفحة جديدة

استخدم هذا المثال عندما يجب أن يبدأ جدول ثانٍ في صفحة منفصلة بعد جدول كبير.

  1. أنشئ PDF جديد Document واضبط إعدادات الصفحة.
  2. ابنِ الأول كبير Table وأضفه إلى الصفحة.
  3. أنشئ جدول ثانٍ، تعيين InNewPage، واحفظ المستند.
public static void addTableOnNewPage(Path outputFile) {
    try (Document document = new Document()) {
        document.getPageInfo().getMargin().setLeft(37);
        document.getPageInfo().getMargin().setRight(37);
        document.getPageInfo().getMargin().setTop(37);
        document.getPageInfo().getMargin().setBottom(37);
        document.getPageInfo().setLandscape(true);

        Page page = document.getPages().add();
        Table table = new Table();
        table.setColumnWidths("50 100");
        for (int i = 1; i < 121; i++) {
            Row row = table.getRows().add();
            row.setFixedRowHeight(15);
            row.getCells().add().getParagraphs().add(new TextFragment("Content 1"));
            row.getCells().add().getParagraphs().add(new TextFragment("Content 2"));
        }
        page.getParagraphs().add(table);

        Table table1 = new Table();
        table1.setColumnWidths("100 100");
        for (int i = 1; i < 11; i++) {
            Row row = table1.getRows().add();
            row.getCells().add().getParagraphs().add(new TextFragment("Content 3"));
            row.getCells().add().getParagraphs().add(new TextFragment("Content 4"));
        }
        table1.setInNewPage(true);
        page.getParagraphs().add(table1);
        document.save(outputFile.toString());
    }
}

بناء جدول مكسور عموديًا بأعمدة متكررة

استخدم هذا المثال عندما يجب أن يستمر جدول عريض عموديًا ويكرر الأعمدة الرئيسية.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table واضبط الانقطاع العمودي مع الأعمدة المتكررة.
  3. أضف رأس الجدول وصفوف البيانات، ثم احفظ المستند.
public static void addTableHideBorders(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBroken(TableBroken.Vertical);
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All));
        table.setRepeatingColumnsCount(2);
        page.getParagraphs().add(table);

        Row row = table.getRows().add();
        Cell cell = row.getCells().add("header 1");
        cell.setColSpan(2);
        cell.setBackgroundColor(Color.getLightGray());
        row.getCells().add("header 3");
        Cell cell2 = row.getCells().add("header 4");
        cell2.setColSpan(2);
        cell2.setBackgroundColor(Color.getLightBlue());
        row.getCells().add("header 6");
        Cell cell3 = row.getCells().add("header 7");
        cell3.setColSpan(2);
        cell3.setBackgroundColor(Color.getLightGreen());
        Cell cell4 = row.getCells().add("header 9");
        cell4.setColSpan(3);
        cell4.setBackgroundColor(Color.getLightCoral());
        for (int i = 12; i < 18; i++) {
            row.getCells().add("header " + i);
        }

        for (int rowCounter = 0; rowCounter < 3; rowCounter++) {
            Row row1 = table.getRows().add();
            for (int i = 1; i < 18; i++) {
                row1.getCells().add("col " + rowCounter + ", " + i);
            }
        }
        document.save(outputFile.toString());
    }
}

إعادة استخدام مثال الحدود والمسافات الداخلية

استخدم هذا المساعد عندما يجب أن يُفوَّض سيناريو الهوامش والحشو إلى مثال الحد المشترك.

  1. استدعِ طريقة حدود الجدول والحشو الحالية.
  2. أعد استخدام منطق تخطيط الجدول نفسه دون تكرار الشيفرة.
public static void addMarginsOrPadding(Path outputFile) {
    addBorders(outputFile);
}

إنشاء جدول بزوايا مستديرة

استخدم هذا المثال عندما يجب أن يستخدم الجدول تنسيق الزوايا المستديرة بدلاً من الحدود المستطيلة القياسية.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وتهيئة إعدادات الحدود المستديرة.
  3. أضف صفوفًا إلى الجدول واحفظ ملف PDF..
public static void createTableWithRoundCorner(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        BorderInfo borderInfo = new BorderInfo(BorderSide.All);
        borderInfo.setRoundedBorderRadius(15);
        table.setCornerStyle(BorderCornerStyle.Round);
        table.setBorder(borderInfo);
        for (int rowCount = 0; rowCount < 10; rowCount++) {
            Row row = table.getRows().add();
            row.getCells().add("Column (" + rowCount + ", 1)");
            row.getCells().add("Column (" + rowCount + ", 2)");
            row.getCells().add("Column (" + rowCount + ", 3)");
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة صفوف رأسية متكررة

استخدم هذا المثال عندما يجب على الجداول متعددة الصفحات تكرار صفوف الرأس في كل صفحة متابعة.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ مكسور عموديًا Table واضبط عدد الصفوف المتكررة والنمط.
  3. أضف صفوف العنوان وصفوف البيانات، ثم احفظ المستند.
public static void addRepeatingRows(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBroken(TableBroken.Vertical);
        table.setRepeatingRowsCount(2);
        TextState textState = new TextState();
        textState.setFontSize(12);
        textState.setFont(FontRepository.findFont("TimesNewRoman"));
        textState.setForegroundColor(Color.getRed());
        table.setRepeatingRowsStyle(textState);
        table.setColumnWidths("100 100 100");
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getBlack()));
        table.setBorder(new BorderInfo(BorderSide.All, 1, Color.getBlack()));

        Row headerRow1 = table.getRows().add();
        headerRow1.getCells().add("Header 1-1");
        headerRow1.getCells().add("Header 1-2");
        headerRow1.getCells().add("Header 1-3");
        for (Cell cell : headerRow1.getCells()) {
            cell.setBackgroundColor(Color.getLightGray());
        }
        Row headerRow2 = table.getRows().add();
        headerRow2.getCells().add("Header 2-1");
        headerRow2.getCells().add("Header 2-2");
        headerRow2.getCells().add("Header 2-3");
        for (Cell cell : headerRow2.getCells()) {
            cell.setBackgroundColor(Color.getLightBlue());
        }
        for (int i = 1; i < 101; i++) {
            Row row = table.getRows().add();
            row.getCells().add("Data " + i + "-1");
            row.getCells().add("Data " + i + "-2");
            row.getCells().add("Data " + i + "-3");
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

إضافة أعمدة متكررة في جدول عريض

استخدم هذا المثال عندما يجب أن تتكرر الأعمدة الأولى بينما ينكسر الجدول عموديًا على نفس الصفحة.

  1. أنشئ PDF جديد Document وإعداد حجم الصفحة.
  2. أنشئ كائنًا من الفئة Table وأضبط الأعمدة المتكررة بالإضافة إلى سلوك الملاءمة التلقائية.
  3. أضف رأس وصفوف البيانات، ثم احفظ ملف PDF..
public static void addRepeatingColumns(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        page.setPageSize(PageSize.getA5().getHeight(), PageSize.getA5().getWidth());
        BorderInfo border = new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray());
        Table table = new Table();
        table.setBroken(TableBroken.VerticalInSamePage);
        table.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
        table.setRepeatingColumnsCount(5);
        table.setBorder(border);
        table.setDefaultCellBorder(border);
        page.getParagraphs().add(table);

        Row row = table.getRows().add();
        for (int i = 1; i < 6; i++) {
            Cell cell = row.getCells().add("header " + i);
            cell.setBackgroundColor(Color.getLightGray());
        }
        for (int i = 6; i < 18; i++) {
            row.getCells().add("header " + i);
        }

        for (int rowCounter = 1; rowCounter < 6; rowCounter++) {
            row = table.getRows().add();
            for (int i = 1; i < 6; i++) {
                Cell cell = row.getCells().add("cell " + rowCounter + "," + i);
                cell.setBackgroundColor(Color.getLightGray());
            }
            for (int i = 6; i < 18; i++) {
                row.getCells().add("cell " + rowCounter + "," + i);
            }
        }
        document.save(outputFile.toString());
    }
}

إدراج فواصل صفحات بين صفوف الجدول

استخدم هذا المثال عندما يجب أن تبدأ صفوف الجدول المحددة في صفحة جديدة.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table واملأ العديد من الصفوف.
  3. ضع علامة على الصفوف المحددة بـ InNewPage واحفظ المستند.
public static void insertPageBreak(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, Color.getRed()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, Color.getRed()));
        table.setColumnWidths("100 100");
        for (int counter = 0; counter < 201; counter++) {
            Row row = new Row();
            table.getRows().add(row);
            row.getCells().add().getParagraphs().add(new TextFragment("Cell " + counter + ", 0"));
            row.getCells().add().getParagraphs().add(new TextFragment("Cell " + counter + ", 1"));
            if (counter % 10 == 0 && counter != 0) {
                row.setInNewPage(true);
            }
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}

تدوير النص داخل خلايا الجدول

استخدم هذا المثال عندما يجب عرض نص الخلية بزاويات دوران مختلفة.

  1. أنشئ PDF جديد Document وأضف صفحة.
  2. أنشئ كائنًا من الفئة Table وأضف صفًا يحتوي على عدة خلايا.
  3. أنشئ مائل الكائنات TextFragment، أضفها إلى الخلايا، واحفظ ملف PDF..
public static void rotatedTextTable(Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();
        Table table = new Table();
        table.setBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getBlack()));
        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.5f, Color.getBlack()));
        Row row = table.getRows().add();
        row.setMinRowHeight(200);
        for (int cellCount = 0; cellCount < 4; cellCount++) {
            Cell cell = row.getCells().add();
            TextFragment textFragment = new TextFragment("Cell 1 " + (cellCount - 1));
            textFragment.getTextState().setRotation(90 * cellCount);
            textFragment.setHorizontalAlignment(HorizontalAlignment.Center);
            cell.getParagraphs().add(textFragment);
        }
        page.getParagraphs().add(table);
        document.save(outputFile.toString());
    }
}