Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalArgumentException - Cell Width Exceeds Row Width #281

Open
ananthpai99 opened this issue Jun 13, 2024 · 0 comments
Open

IllegalArgumentException - Cell Width Exceeds Row Width #281

ananthpai99 opened this issue Jun 13, 2024 · 0 comments

Comments

@ananthpai99
Copy link

ananthpai99 commented Jun 13, 2024

package com.practice.pdfBox;

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.common.*;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.Matrix;
import java.awt.Color;
import java.awt.Transparency;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import be.quodlibet.boxable.BaseTable;
import be.quodlibet.boxable.Cell;
import be.quodlibet.boxable.HorizontalAlignment;
import be.quodlibet.boxable.Row;
import be.quodlibet.boxable.Table;
import be.quodlibet.boxable.datatable.DataTable;
import be.quodlibet.boxable.line.LineStyle;
import be.quodlibet.boxable.utils.FontUtils;

public class PdfBoxApplication {
	private static final float TABLE_MARGIN = 10;

	public static void main(String[] args) throws Exception {
		PDDocument mainDocument = new PDDocument();
		PDPage myPage = new PDPage(PDRectangle.A3);
		mainDocument.addPage(myPage);
		PDPageContentStream contentStream = new PDPageContentStream(mainDocument, myPage);

		List<List> data = new ArrayList<>();
		data.add(List.of("Exploring new realms of  possibility adventurers embark on a journey filled with excitement and uncertainty  eager to discover what lies beyond the horizon"));
		float yNextTable = 0;
		boolean provideSpaceBeforeTable = true;
		// Initialize table
		System.out.println(myPage.getMediaBox().getWidth());
		float tableWidth = 800f;
		System.out.println("Table width =" + tableWidth);
		float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * 10);
		float yStart = yStartNewPage;
		BaseTable dataTable2 = new BaseTable(yStart, yStartNewPage, 0, tableWidth, TABLE_MARGIN, mainDocument, myPage,
				true, true);
		dataTable2.setLineSpacing(1.3f);
		DataTable t2 = new DataTable(dataTable2, myPage);
		Cell headerCell2 = t2.getHeaderCellTemplate();
		headerCell2.setAlign(HorizontalAlignment.LEFT);
		headerCell2.setFont(PDType1Font.HELVETICA);
		headerCell2.setFontSize(11);

		Cell dataOddCell = t2.getDataCellTemplateOdd();
		dataOddCell.setAlign(HorizontalAlignment.LEFT);
		dataOddCell.setFont(PDType1Font.HELVETICA_BOLD);
		dataOddCell.setFontSize(13);

		Cell dataEvenCell = t2.getDataCellTemplateEven();
		dataEvenCell.setAlign(HorizontalAlignment.LEFT);
		dataEvenCell.setFont(PDType1Font.HELVETICA_BOLD);
		dataEvenCell.setFontSize(13);
		
		t2.addListToTable(data, DataTable.HASHEADER);
		yNextTable = dataTable2.draw();
		System.out.println(yNextTable);

		contentStream.close();

		mainDocument.save("testfile.pdf");
		mainDocument.close();
	}
}

When creating a table using the boxable library in a pdf created using pdfbox library as above. The table creation is failing because of java.lang.IllegalArgumentException adding the console output below:
841.8898
Table width =800.0
Exception in thread "main" java.lang.IllegalArgumentException: Cell Width=800.0002 can't be bigger than row width=800.0
at be.quodlibet.boxable.Cell.(Cell.java:107)
at be.quodlibet.boxable.Row.createCell(Row.java:134)
at be.quodlibet.boxable.datatable.DataTable.addCsvToTable(DataTable.java:266)
at be.quodlibet.boxable.datatable.DataTable.addListToTable(DataTable.java:204)
at com.practice.pdfBox.PdfBoxApplication.main(PdfBoxApplication.java:65)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant