Are there any callback events for table cells or PDF pages.. something like onEndPage, onStartPage, onRender

Do we have callback event for cells or PDF pages… something like onEndPage, onStartPage, onRender

@BALKRUSHNA015478

We are afraid that such callbacks are not yet implemented in the API. However, please share your complete requirements so that we can log an enhancement ticket accordingly and share the ID with you.

I am creating a pdf document from scratch and part of it is adding header/footer. Per documentation, the way to add header/footer is via Stamps. However, this means that i have to save the PDF document first, then loop through the pages to “stamp” header and footer, then save the document again. It seems inefficient; saving the document twice.

Since the document is created from scratch, ideally, it should be done via callback events like onEndPage (add footer) or onStartPage (add header) but since aspose does not support callback events, maybe there is another way to do this without saving the document first.

@bgonzalez

You can use HeaderFooter Class as well while creating a PDF document from scratch to specify header and footer of the page on which you are working. For example, please check the below code snippet:

Document doc = new Document();
var page = doc.Pages.Add();
HeaderFooter docFooter = new HeaderFooter();
docFooter.Paragraphs.Add(new TextFragment("Hello Footer"));
page.Footer = docFooter;

Please let us know by sharing your complete sample code snippet in case you face any issues.

In your solution, is there a way to differentiate first page header from the rest of the pages. See code snippet below:

String s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.\n"+
					"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.\n" +
					"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.\n" +
					"Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.\n" +
					"Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy"+
					"\r\n\r\nLorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.\n"+
					"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.\n" +
					"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.\n" +
					"Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.\n" +
					"Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy";

			Document document = new Document();
			 
	        Page page = document.getPages().add();
	         
	        page.getParagraphs().add(new TextFragment(s));
	        
	        TextFragment fragment = new TextFragment("\r\n**** The goal is to have all ROWS BELONGING TO A TABLE TOGETHER IN ONE PAGE AND NOT SPLIT ACROSS TWO PAGES.");
			fragment.getTextState().setFontStyle(FontStyles.Bold);
	        page.getParagraphs().add(fragment);
	        
	        HeaderFooter firstPageHeader = new HeaderFooter();
	        firstPageHeader.getParagraphs().add(new TextFragment("THIS IS THE FIRST PAGE HEADER"));
	        page.setHeader(firstPageHeader);
	        
	        //TODO: how to add this in succeeding pages????
	        HeaderFooter normalHeader = new HeaderFooter();
	        normalHeader.getParagraphs().add(new TextFragment("THIS IS THE NORMAL PAGE HEADER"));
	        
	        MarginInfo margin = new MarginInfo();
	        margin.setTop (5f);
	        margin.setLeft (5f);
	        margin.setRight (5f);
	        margin.setBottom (20f);
	        Table mainTable = new Table();
	        mainTable.setColumnWidths("410");		
	        for (int i = 0; i < 10; i++) {
	        	Cell mainCell = mainTable.getRows().add().getCells().add();
	        	Table table = new Table();
		        table.setDefaultCellPadding(margin);
		        table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1F));
	        	table.setColumnWidths("200 200");
	        	int rowCnt = new Random().nextInt(9)+1;
	        	for (int j = 0; j < rowCnt; j++) {
	        		Row row = table.getRows().add();
	        		row.getCells().add("Table " + i + " : Row " + j + " : Col 1");
	        		row.getCells().add("Table " + i + " : Row " + j + " : Col 2");
				}
	        	fragment = new TextFragment("\r\nTABLE " + i);
				fragment.getTextState().setFontStyle(FontStyles.Bold);
				mainCell.getParagraphs().add(fragment);
				mainCell.getParagraphs().add(table);
			}
	        page.getParagraphs().add(mainTable);
	        // Save updated PDF
	        document.save("c:/temp/testOut.pdf");

@bgonzalez

In this case, you can call Document.ProcessParagraphs() method only to update the page collection of the document being generated. After calling this method, you will get actual count of page and can add header/footer to all of them. Later, the document can be saved.

1 Like

Thanks, this works. I really appreciate your prompt response.

1 Like