Keeping a (title-) Paragraph and a Table together on one page? - java

I'm generating a PDF document with iText 5.5.8
In this document there are numbered paragraphs that only contain a title Paragraph and a PdfPTable.
for (Item item : getItems()) {
Paragraph title = new Paragraph();
Chunk chunk = new Chunk(new Chunk(getIcon(item), 0, 0));
addBookmark(item, chunk);
title.add(chunk);
Chunk chunk2 = new Chunk(getName(item), catFont_u);
title.add(chunk2);
title.setSpacingBefore(20);
title.setSpacingAfter(14);
PdfPTable table = createTable(item); // can be more than a page!
table.setKeepTogether(true);
Section subSection = chapter.addSection(title);
subSection.add(table);
}
Now when the table is larger that te space left in the rest of the page, the table will be 'moved' to the next page (setKeepTogether()). This is good.
However, I want the title Paragraph to always be on the same page as the PdfPTable. So the title Paragraph should be moved to the next page also.
How do I accomplish this?
Thanks,
Carel

You can create an outer table of one column. then add your paragraph(title). after that create another table innerTable, here you can place your data, then add inner table to a cell and then add that cell to outer table. So that your title and table will be together, and also make outer table setsplitLate(false).

Related

How can I set table row's height different each column in word via apache poi

I want to create a table in my word document. In this table, I need something like this
In this image, third row's third column has different height from first and second columns. I want to create something like this with apache poi but I don't find any solution for this.
My codes is here
table.getRow(6).setHeight(630);
table.getRow(6).getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);
table.getRow(6).getCell(0).removeParagraph(0); //Cell0
XWPFParagraph row6Paragraph=table.getRow(6).getCell(0).addParagraph();
row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun row6Run=row6Paragraph.createRun();
row6Run.setFontFamily("Arial");
row6Run.setFontSize(10);
row6Run.setText("5) Engine Induction Date");
row6Run.addBreak();
String indDate="12-09-2018";
row6Run.setText(" "+indDate);
table.getRow(6).getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985));
table.getRow(6).getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(365));//cell1
table.getRow(6).getCell(2).removeParagraph(0); //Cell2
row6Paragraph=table.getRow(6).getCell(2).addParagraph();
row6Paragraph.setAlignment(ParagraphAlignment.LEFT);
row6Paragraph.setVerticalAlignment(TextAlignment.CENTER);
XWPFRun row6Run1=row6Paragraph.createRun();
row6Run1.setFontFamily("Arial");
row6Run1.setFontSize(10);
row6Run1.setBold(true);
row6Run1.setText("PART INFORMATION");
table.getRow(6).getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(4985));
You should switch on showing non-printing characters in Word (¶). Without seeing those characters it is not really clear from your screen shot. But creating this using only one table by merging cells would be really hard, not only using apache poi but also using Word's GUI. So I suspect this are three tables.
Table one is in a one column section and only contains one row having one cell containing the header.
Table two then is placed in a two columns section of the word document. Then the table breaks to the second column if needed. It has only one column and each row is as height as the content.
Table three is in a one column section again and only contains one row having one cell containing the footer.
Example using current apache poi 4.1.2:
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class Word2ColumnPageWithTable {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
//one column section
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("One column on top.");
//table over full width of one column section
XWPFTable table = document.createTable();
table.setWidth("100%");
XWPFTableRow row = table.getRow(0);
row.getCell(0).setColor("808080");
row.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("Table Header");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
row.getCell(0).getParagraphArray(0).getRuns().get(0).setColor("FFFFFF");
//paragraph with section setting for one column section above
paragraph = document.createParagraph();
CTSectPr ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
CTColumns ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
//two column section
//table over full width of one column in two column section
table = document.createTable();
table.setWidth("100%");
row = table.getRow(0);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
row = table.createRow();
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("SHORT CONTENT");
row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, so multiple lines will be needed, which increases the row height.");
table.createRow().getCell(0).setText("Long content, which increases the row height.");
//paragraph with section break continuous for two column section above
paragraph = document.createParagraph();
ctSectPr = paragraph.getCTP().addNewPPr().addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(2));
ctColumns.setEqualWidth(STOnOff.ON);
//one column section again
//table over full width of one column section
table = document.createTable();
table.setWidth("100%");
row = table.getRow(0);
row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
row.getCell(0).getParagraphArray(0).createRun().setText("TABLE FOOTER");
paragraph = document.createParagraph();
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("One column on bottom.");
//section setting continuous for one column section above and whole document from here on
CTDocument1 ctDocument = document.getDocument();
CTBody ctBody = ctDocument.getBody();
ctSectPr = ctBody.addNewSectPr();
ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
ctColumns = ctSectPr.addNewCols();
ctColumns.setNum(BigInteger.valueOf(1));
FileOutputStream out = new FileOutputStream("Word2ColumnPageWithTable.docx");
document.write(out);
out.close();
document.close();
}
}
This code produces a Word2ColumnPageWithTable.docx which looks like so:

values are getting overwritten in the table -PDFbox

I want to display the set of records in rows and columns. Am getting output but the thing is, it is getting overlapped. should i modify the loop can someone pls suggest.
ArrayList<ResultRecord> Records = new ArrayList<ResultRecord>(MainRestClient.fetchResultRecords(this.savedMainLog));
for(j=0; j<Records.size(); j++)
{
Row<PDPage> row4 = table.createRow(100.0f);
Cell<PDPage> cell10 = row4.createCell(15.0f, temp.getNumber());
Cell<PDPage> cell11 = row4.createCell(45.0f, temp.getDescription());
Cell<PDPage> cell12 = row4.createCell(15.0f, temp.getStatus());
Cell<PDPage> cell13 = row4.createCell(25.0f, temp.getRemarks());
}
The below is the full code for opening a PDF file. I want to retreive set of records in the row4 in the corresponding cells. But the is over written one above the another.
Expected output:
IT should display one below the another.
Is the overlapping reason,is it because of defining the row as row4.
try {
//table.draw();
cell.setFontSize(12);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
First of all, you should clarify the table drawing library you use. PDFBox only is the underlying PDF library. Considering the classes used I would assume you are using Boxable on top of it.
Furthermore, the reason why all the tables are printed over each other is that you start each table at the same position on the same page, you use
BaseTable table = new BaseTable(yPosition, yStartNewPage,
bottomMargin, tableWidth, margin, document, page, true, drawContent);
without ever changing yPosition or page.
To get one table after the other, you have to update yPosition and page accordingly, e.g. by using the return value of table.draw() and the state of table then, i.e. by replacing
table.draw();
by
yPosition = table.draw();
page = table.getCurrentPage();

How can I create an accessible PDF with Java PDFBox 2.0.8 library that is also verifiable with PAC 2 tool?

Background
I have small project on GitHub in which I am trying to create a section 508 compliant (section508.gov) PDF which has form elements within a complex table structure. The tool recommended to verify these PDFs is at http://www.access-for-all.ch/en/pdf-lab/pdf-accessibility-checker-pac.html and my program’s output PDF does pass most of these checks. I will also know what every field is meant for at runtime, so adding tags to structure elements should not be an issue.
The Problem
The PAC 2 tool seems to have an issue with two particular items in the output PDF. In particular, my radio buttons’ widget annotations are not nested inside of a form structure element and my marked content is not tagged (Text and Table Cells).
PAC 2 verifies the P structure element that is within top-left cell but not the marked content…
However, PAC 2 does identify the marked content as an error (i.e. Text/Path object not tagged).
Also, the radio button widgets are detected, but there seems to be no APIs to add them to a form structure element.
What I Have Tried
I have looked at several questions on this website and others on the subject including this one Tagged PDF with PDFBox, but it seems that there are almost no examples for PDF/UA and very little useful documentation (That I have found). The most useful tips that I have found have been at sites that explain specs for tagged PDFs like https://taggedpdf.com/508-pdf-help-center/object-not-tagged/.
The Question
Is it possible to create a PAC 2 verifiable PDF with Apache PDFBox that includes marked content and radio button widget annotations? If it is possible, is it doable using higher level (non-deprecated) PDFBox APIs?
Side Note: This is actually my first StackExchange question (Although I have used the site extensively) and I hope everything is in order! Feel free to add any necessary edits and ask any questions that I may need clarify. Also, I have an example program on GitHub which generates my PDF document at https://github.com/chris271/UAPDFBox.
Edit 1: Direct link to Output PDF Document
*EDIT 2: After using some of the lower-level PDFBox APIs and viewing raw data streams for fully compliant PDFs with PDFDebugger, I was able to generate a PDF with nearly identical content structure compared to the compliant PDF's content structure... However, the same errors appear that the text objects are not tagged and I really can't decide where to go from here... Any guidance would be greatly appreciated!
Edit 3: Side-by-side raw PDF content comparison.
Edit 4: Internal structure of the generated PDF
and the compliant PDF
Edit 5: I have managed to fix the PAC 2 errors for tagged path/text objects thanks in part to suggestions from Tilman Hausherr! I will add an answer if I manage to fix the issues regarding 'annotation widgets not being nested inside form structure elements'.
After going through a large amount of the PDF Spec and many PDFBox examples I was able to fix all issues reported by PAC 2. There were several steps involved to create the verified PDF (with a complex table structure) and the full source code is available here on github. I will attempt to do an overview of the major portions of the code below. (Some method calls will not be explained here!)
Step 1 (Setup metadata)
Various setup info like document title and language
//Setup new document
pdf = new PDDocument();
acroForm = new PDAcroForm(pdf);
pdf.getDocumentInformation().setTitle(title);
//Adjust other document metadata
PDDocumentCatalog documentCatalog = pdf.getDocumentCatalog();
documentCatalog.setLanguage("English");
documentCatalog.setViewerPreferences(new PDViewerPreferences(new COSDictionary()));
documentCatalog.getViewerPreferences().setDisplayDocTitle(true);
documentCatalog.setAcroForm(acroForm);
documentCatalog.setStructureTreeRoot(structureTreeRoot);
PDMarkInfo markInfo = new PDMarkInfo();
markInfo.setMarked(true);
documentCatalog.setMarkInfo(markInfo);
Embed all fonts directly into resources.
//Set AcroForm Appearance Characteristics
PDResources resources = new PDResources();
defaultFont = PDType0Font.load(pdf,
new PDTrueTypeFont(PDType1Font.HELVETICA.getCOSObject()).getTrueTypeFont(), true);
resources.put(COSName.getPDFName("Helv"), defaultFont);
acroForm.setNeedAppearances(true);
acroForm.setXFA(null);
acroForm.setDefaultResources(resources);
acroForm.setDefaultAppearance(DEFAULT_APPEARANCE);
Add XMP Metadata for PDF/UA spec.
//Add UA XMP metadata based on specs at https://taggedpdf.com/508-pdf-help-center/pdfua-identifier-missing/
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
xmp.createAndAddDublinCoreSchema();
xmp.getDublinCoreSchema().setTitle(title);
xmp.getDublinCoreSchema().setDescription(title);
xmp.createAndAddPDFAExtensionSchemaWithDefaultNS();
xmp.getPDFExtensionSchema().addNamespace("http://www.aiim.org/pdfa/ns/schema#", "pdfaSchema");
xmp.getPDFExtensionSchema().addNamespace("http://www.aiim.org/pdfa/ns/property#", "pdfaProperty");
xmp.getPDFExtensionSchema().addNamespace("http://www.aiim.org/pdfua/ns/id/", "pdfuaid");
XMPSchema uaSchema = new XMPSchema(XMPMetadata.createXMPMetadata(),
"pdfaSchema", "pdfaSchema", "pdfaSchema");
uaSchema.setTextPropertyValue("schema", "PDF/UA Universal Accessibility Schema");
uaSchema.setTextPropertyValue("namespaceURI", "http://www.aiim.org/pdfua/ns/id/");
uaSchema.setTextPropertyValue("prefix", "pdfuaid");
XMPSchema uaProp = new XMPSchema(XMPMetadata.createXMPMetadata(),
"pdfaProperty", "pdfaProperty", "pdfaProperty");
uaProp.setTextPropertyValue("name", "part");
uaProp.setTextPropertyValue("valueType", "Integer");
uaProp.setTextPropertyValue("category", "internal");
uaProp.setTextPropertyValue("description", "Indicates, which part of ISO 14289 standard is followed");
uaSchema.addUnqualifiedSequenceValue("property", uaProp);
xmp.getPDFExtensionSchema().addBagValue("schemas", uaSchema);
xmp.getPDFExtensionSchema().setPrefix("pdfuaid");
xmp.getPDFExtensionSchema().setTextPropertyValue("part", "1");
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, true);
PDMetadata metadata = new PDMetadata(pdf);
metadata.importXMPMetadata(baos.toByteArray());
pdf.getDocumentCatalog().setMetadata(metadata);
Step 2 (Setup document tag structure)
You will need to add the root structure element and all necessary structure elements as children to the root element.
//Adds a DOCUMENT structure element as the structure tree root.
void addRoot() {
PDStructureElement root = new PDStructureElement(StandardStructureTypes.DOCUMENT, null);
root.setAlternateDescription("The document's root structure element.");
root.setTitle("PDF Document");
pdf.getDocumentCatalog().getStructureTreeRoot().appendKid(root);
currentElem = root;
rootElem = root;
}
Each marked content element (text and background graphics) will need to have an MCID and an associated tag for reference in the parent tree which will be explained in step 3.
//Assign an id for the next marked content element.
private void setNextMarkedContentDictionary(String tag) {
currentMarkedContentDictionary = new COSDictionary();
currentMarkedContentDictionary.setName("Tag", tag);
currentMarkedContentDictionary.setInt(COSName.MCID, currentMCID);
currentMCID++;
}
Artifacts (background graphics) will not be detected by the screen reader. Text needs to be detectable so a P structure element is used here when adding text.
//Set up the next marked content element with an MCID and create the containing TD structure element.
PDPageContentStream contents = new PDPageContentStream(
pdf, pages.get(pageIndex), PDPageContentStream.AppendMode.APPEND, false);
currentElem = addContentToParent(null, StandardStructureTypes.TD, pages.get(pageIndex), currentRow);
//Make the actual cell rectangle and set as artifact to avoid detection.
setNextMarkedContentDictionary(COSName.ARTIFACT.getName());
contents.beginMarkedContent(COSName.ARTIFACT, PDPropertyList.create(currentMarkedContentDictionary));
//Draws the cell itself with the given colors and location.
drawDataCell(table.getCell(i, j).getCellColor(), table.getCell(i, j).getBorderColor(),
x + table.getRows().get(i).getCellPosition(j),
y + table.getRowPosition(i),
table.getCell(i, j).getWidth(), table.getRows().get(i).getHeight(), contents);
contents.endMarkedContent();
currentElem = addContentToParent(COSName.ARTIFACT, StandardStructureTypes.P, pages.get(pageIndex), currentElem);
contents.close();
//Draw the cell's text as a P structure element
contents = new PDPageContentStream(
pdf, pages.get(pageIndex), PDPageContentStream.AppendMode.APPEND, false);
setNextMarkedContentDictionary(COSName.P.getName());
contents.beginMarkedContent(COSName.P, PDPropertyList.create(currentMarkedContentDictionary));
//... Code to draw actual text...//
//End the marked content and append it's P structure element to the containing TD structure element.
contents.endMarkedContent();
addContentToParent(COSName.P, null, pages.get(pageIndex), currentElem);
contents.close();
Annotation Widgets (form objects in this case) will need to be nested within Form structure elements.
//Add a radio button widget.
if (!table.getCell(i, j).getRbVal().isEmpty()) {
PDStructureElement fieldElem = new PDStructureElement(StandardStructureTypes.FORM, currentElem);
radioWidgets.add(addRadioButton(
x + table.getRows().get(i).getCellPosition(j) -
radioWidgets.size() * 10 + table.getCell(i, j).getWidth() / 4,
y + table.getRowPosition(i),
table.getCell(i, j).getWidth() * 1.5f, 20,
radioValues, pageIndex, radioWidgets.size()));
fieldElem.setPage(pages.get(pageIndex));
COSArray kArray = new COSArray();
kArray.add(COSInteger.get(currentMCID));
fieldElem.getCOSObject().setItem(COSName.K, kArray);
addWidgetContent(annotationRefs.get(annotationRefs.size() - 1), fieldElem, StandardStructureTypes.FORM, pageIndex);
}
//Add a text field in the current cell.
if (!table.getCell(i, j).getTextVal().isEmpty()) {
PDStructureElement fieldElem = new PDStructureElement(StandardStructureTypes.FORM, currentElem);
addTextField(x + table.getRows().get(i).getCellPosition(j),
y + table.getRowPosition(i),
table.getCell(i, j).getWidth(), table.getRows().get(i).getHeight(),
table.getCell(i, j).getTextVal(), pageIndex);
fieldElem.setPage(pages.get(pageIndex));
COSArray kArray = new COSArray();
kArray.add(COSInteger.get(currentMCID));
fieldElem.getCOSObject().setItem(COSName.K, kArray);
addWidgetContent(annotationRefs.get(annotationRefs.size() - 1), fieldElem, StandardStructureTypes.FORM, pageIndex);
}
Step 3
After all content elements have been written to the content stream and tag structure has been setup, it is necessary to go back and add the parent tree to the structure tree root. Note: Some method calls (addWidgetContent() and addContentToParent()) in the above code setup the necessary COSDictionary objects.
//Adds the parent tree to root struct element to identify tagged content
void addParentTree() {
COSDictionary dict = new COSDictionary();
nums.add(numDictionaries);
for (int i = 1; i < currentStructParent; i++) {
nums.add(COSInteger.get(i));
nums.add(annotDicts.get(i - 1));
}
dict.setItem(COSName.NUMS, nums);
PDNumberTreeNode numberTreeNode = new PDNumberTreeNode(dict, dict.getClass());
pdf.getDocumentCatalog().getStructureTreeRoot().setParentTreeNextKey(currentStructParent);
pdf.getDocumentCatalog().getStructureTreeRoot().setParentTree(numberTreeNode);
}
If all widget annotations and marked content were added correctly to the structure tree and parent tree then you should get something like this from PAC 2 and PDFDebugger.
Thank you to Tilman Hausherr for pointing me in the right direction to solve this! I will most likely make some edits to this answer for additional clarity as recommended by others.
Edit 1:
If you want to have a table structure like the one I have generated you will also need to add correct table markup to fully comply with the 508 standard... The 'Scope', 'ColSpan', 'RowSpan', or 'Headers' attributes will need to be correctly added to each table cell structure element similar to this or this. The main purpose for this markup is to allow a screen reading software like JAWS to read the table content in an understandable way. These attributes can be added in a similar way as below...
private void addTableCellMarkup(Cell cell, int pageIndex, PDStructureElement currentRow) {
COSDictionary cellAttr = new COSDictionary();
cellAttr.setName(COSName.O, "Table");
if (cell.getCellMarkup().isHeader()) {
currentElem = addContentToParent(null, StandardStructureTypes.TH, pages.get(pageIndex), currentRow);
currentElem.getCOSObject().setString(COSName.ID, cell.getCellMarkup().getId());
if (cell.getCellMarkup().getScope().length() > 0) {
cellAttr.setName(COSName.getPDFName("Scope"), cell.getCellMarkup().getScope());
}
if (cell.getCellMarkup().getColspan() > 1) {
cellAttr.setInt(COSName.getPDFName("ColSpan"), cell.getCellMarkup().getColspan());
}
if (cell.getCellMarkup().getRowSpan() > 1) {
cellAttr.setInt(COSName.getPDFName("RowSpan"), cell.getCellMarkup().getRowSpan());
}
} else {
currentElem = addContentToParent(null, StandardStructureTypes.TD, pages.get(pageIndex), currentRow);
}
if (cell.getCellMarkup().getHeaders().length > 0) {
COSArray headerA = new COSArray();
for (String s : cell.getCellMarkup().getHeaders()) {
headerA.add(new COSString(s));
}
cellAttr.setItem(COSName.getPDFName("Headers"), headerA);
}
currentElem.getCOSObject().setItem(COSName.A, cellAttr);
}
Be sure to do something like currentElem.setAlternateDescription(currentCell.getText()); on each of the structure elements with text marked content for JAWS to read the text.
Note: Each of the fields (radio button and textbox) will need a unique name to avoid setting multiple field values. GitHub has been updated with a more complex example PDF with table markup and improved form fields!

Insert page number with text in footer aspose word java

I want to insert page number in footer alongside with text, but when footer contains some text, page number and text in footer switch places.
I am doing this while converting document from html to word using aspose words java library.
Text in footer is sent from html and I just want to add page number.
Code for add page number in footer:
log.debug("Add page number");
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.insertField("PAGE", null);
builder.write("/");
builder.insertField("NUMPAGES", null);
Also, is there any way to replace whole text in footer?
You can add page numbers and text together in footer using table as following:
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.startTable();
// Clear table borders
builder.getCellFormat().clearFormatting();
builder.insertCell();
// Set first cell to 1/3 of the page width.
builder.getCellFormat().setPreferredWidth(
PreferredWidth.fromPercent(100 / 3));
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to auto calculate current page
// number and total number of pages.
builder.insertField("PAGE", null);
builder.write("/");
builder.insertField("NUMPAGES", null);
// Align this text to the left.
builder.getCurrentParagraph().getParagraphFormat()
.setAlignment(ParagraphAlignment.LEFT);
builder.insertCell();
// Set the second cell to 2/3 of the page width.
builder.getCellFormat().setPreferredWidth(
PreferredWidth.fromPercent(100 * 2 / 3));
builder.write("(C) 2017 Aspose Pty Ltd. All rights reserved.");
// Align this text to the right.
builder.getCurrentParagraph().getParagraphFormat()
.setAlignment(ParagraphAlignment.RIGHT);
builder.endRow();
builder.endTable();
To replace whole text of footer you may access the footer, clear all text and add new contents as following:
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.getCurrentSection();
com.aspose.words.HeaderFooter primaryHeader = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
primaryHeader.getParagraphs().clear();
...
I am Tilal Ahmad, developer evangelist at Aspose.

Stop iText table from spliting on new page

I am developing an app for android that generates pdf.
I am using itextpdf to generate the pdf.
I have the following problem:
I have a table that has 3 rows and when this table is near the end of a page sometimes it puts one row on one page and two rows on the next page.
Is there a way to force this table to start on the next page so I can have the full table on the next page?
Thanks
As an alternative to Bruno's approach of nesting the table in a 1-cell table to prevent splitting, you can also use PdfPTable.setKeepTogether(true) to start the table on a new page when it doesn't fit the current page.
Using a similar example:
Paragraph p = new Paragraph("Test");
PdfPTable table = new PdfPTable(2);
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 40; i++) {
document.add(p);
}
// Try to keep the table on 1 page
table.setKeepTogether(true);
document.add(table);
Both approaches (nesting in a 1-cell table and using setKeepTogether()) behave exactly the same in my tests. This includes when the table is too large to fit on the new page and still needs to be split, e.g. when adding 50 instead of 5 rows in the example above.
Please take a look at the Splitting example:
Paragraph p = new Paragraph("Test");
PdfPTable table = new PdfPTable(2);
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 40; i++) {
document.add(p);
}
document.add(table);
We have a table with 5 rows, and in this case, we're adding some paragraphs so that the table is added at the end of a page.
By default, iText will try not to split rows, but if the full table doesn't fit, it will forward the rows that don't fit to the next page:
You want to avoid this behavior: you don't want the table to split.
Knowing that iText will try to keep full rows intact, you can work around this problem by nesting the table you don' want to split inside another table:
PdfPTable nesting = new PdfPTable(1);
PdfPCell cell = new PdfPCell(table);
cell.setBorder(PdfPCell.NO_BORDER);
nesting.addCell(cell);
document.add(nesting);
Now you get this result:
There was sufficient space on the previous page to render a couple of rows, but as we've wrapped the full table inside a row with a single column, iText will forward the complete table to the next page.

Categories

Resources