I am using iText 5.5.4 to generate a pdf.
I have a a bunch of rows that should be grouped together and have a rowspan cell to name the group. But, on page break, the word "GRP" is broken into "G"
and "RP". Is there a way to make group unbreakable so that it will be drawn on next page if it can't fit on the current one?
I tried keepRowsTogether and setBreakpoints, but didn't get consistent results.
-------------------
| G | row 1 |
| R | row 2 |
| P | row 3 |
-------------------
Layout Image:
Document document = new Document(new Rectangle(300, 125));
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("Table with setSplitLate(true):"));
PdfPTable table = new PdfPTable(2);
table.setWidths(new float[]{1,5});
table.setSpacingBefore(10);
PdfPCell cell = new PdfPCell();
cell.addElement(new Paragraph("G"));
cell.addElement(new Paragraph("R"));
cell.addElement(new Paragraph("O"));
cell.addElement(new Paragraph("U"));
cell.addElement(new Paragraph("P"));
//PdfPCell cell = new PdfPCell( new Phrase("GROUP"));
//cell.setNoWrap(true);
//cell.setRotation(90);
//cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(5);
table.addCell(cell);
table.addCell("row 1");
table.addCell("row 2");
table.addCell("row 3");
table.addCell("row 4");
table.addCell("row 5");
document.add(table);
document.add(new Paragraph("Table with setSplitLate(false):"));
table.setSplitLate(false);
document.add(table);
document.close();
PDF Output from above code
As you didn't provide any code which is kind of mandatory when you post a question on StackOverflow (unless you're not really interested in getting an answer), I have tried to write an example to reproduce the (undesired) result.
I didn't succeed. See my SplittingAndRowspan example:
document.add(new Paragraph("Table with setSplitLate(true):"));
PdfPTable table = new PdfPTable(2);
table.setSpacingBefore(10);
PdfPCell cell = new PdfPCell();
cell.addElement(new Paragraph("G"));
cell.addElement(new Paragraph("R"));
cell.addElement(new Paragraph("P"));
cell.setRowspan(3);
table.addCell(cell);
table.addCell("row 1");
table.addCell("row 2");
table.addCell("row 3");
document.add(table);
I didn't really use setSplitLate(true); because the default value of the split late parameter is true. The result looks like this:
As the cell containing G R P doesn't fit the page, the table is forwarded to the next page (which is exactly what you want).
Of course, this doesn't happen when I use setSplitLate(false):
document.add(new Paragraph("Table with setSplitLate(false):"));
table.setSplitLate(false);
document.add(table);
In this case, the cell containing G R P is split:
Based on the scarce information shared in your question, I assume that you have the line table.setSplitLate(false); somewhere in your code. If so, please remove it. If not, please share your code.
Related
I am still new to VBA. I wanted to clear all the contents of the data (Row 3 to Row 12, Row 15 to Row 24, etc) below the yellow headers, without deleting all of the headers as shown in the photos (Fig 1 becomes Fig. 2). The headers go all the way down to row 109 (increments of 12 from Row 1, so Rows 1,13,25 ...85). I have a code but its too basic and long:
Sub Clear_All()
Set Unitsheet = ThisWorkbook.Worksheets("Sheet"1)
Unitsheet.Range("A3:F12").ClearContents
Unitsheet.Range("A15:F24").ClearContents
.
.
.
.'up to
Unitsheet.Range("A111:F120").ClearContents
End Sub
I need a code that is short, since the rows may reach up to more than 1000.
Any help will be much appreciated.
|
|
V
Sub clear()
Dim i, rows As Long
rows = ActiveSheet.UsedRange.rows.Count
For i = 1 To rows
If Sheet1.Cells(i, 1).Interior.ColorIndex = -4142 Then
Sheet1.Cells(i, 1).EntireRow.ClearContents
End If
Next
End Sub
this function finds all used rows in sheet1
it iterates all rows , if color of cell in A column has no color index (-4142) it clears all contents in entire row
I am brand new to iText and leaped straight into iText 7, but find setting column widths on my table to be ridiculously challenging. Clearly there is something basic I'm not getting.
In combing the web for answers, I found some itext 5-based answers that don't seem to be relevant to itext 7 (hard-coding a fixed width). The iText 7 stuff seems to assume knowledge I don't have! "relative column widths" -- what does that mean? should the numbers add up to 100? Simple examples with 2 or 4 columns make sense, but I'm having difficulty translating that into a 7-column table.
I have a table with 7 columns. The 1st, 3rd, and 5th columns should be relatively narrow, they are simply labels. the 7th column can take up whatever space is available. For instance:
Col1 Col2 Col3 Col4 Col5 Col6 Col7
CMS#: C34827284 Date: 12/5/16 End Date: 12/6/16 approved
I thought I had it, but my latest attempt produces "Element does not fit current area" errors. Here is how I defined it
Table htable = new Table(new float[] {3, 8, 5, 10, 5, 10, 30});
These numbers add up to 71 so why do they not fit? Do they need to add exactly up to 100 or am I completely on the wrong track with the "adds up to 100" idea?
Did you define a width for the table as a whole?
This is what I tried:
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf, PageSize.A4);
// Create table
Table htable = new Table(new float[] {3, 8, 5, 10, 5, 10, 30});
htable.setFontSize(8);
htable.setWidthPercent(100);
htable.addHeaderCell(new Cell().add("Col1"));
htable.addHeaderCell(new Cell().add("Col2"));
htable.addHeaderCell(new Cell().add("Col3"));
htable.addHeaderCell(new Cell().add("Col4"));
htable.addHeaderCell(new Cell().add("Col5"));
htable.addHeaderCell(new Cell().add("Col6"));
htable.addHeaderCell(new Cell().add("Col7"));
htable.addCell(new Cell().add("CMS#"));
htable.addCell(new Cell().add("C34827284"));
htable.addCell(new Cell().add("Date:"));
htable.addCell(new Cell().add("12/5/16"));
htable.addCell(new Cell().add("EndDate:"));
htable.addCell(new Cell().add("12/6/16"));
htable.addCell(new Cell().add("Approved"));
// Add the table
document.add(htable);
// Close the document
document.close();
}
The result is shown here:
Note that I reduced the font size to avoid that more text is split the way CMS# is split.
Try adding htable.setWidthPercent(100); to your code if you didn't already do so. The problem should disappear.
I am trying to draw a table in word document using Apache POI XWPF. But the table is drawing with double height rows like this
Here is my source
XWPFTable table = document.createTable(5, 1);
for (Test t : tests) {
XWPFTableRow row = table.getRow(k - 1);
XWPFTableCell cell = row.getCell(0);
XWPFParagraph ansParagraph = new XWPFParagraph(cell.getCTTc().insertNewP(0), cell);
XWPFRun ansRun = ansParagraph.createRun();
ansRun.setText(k + ") ");
cell.addParagraph(ansParagraph);
k++;
}
how can I reduced the height of row.
When you create a table in Apache POI XWPF it already has a blank paragraph in every cell.
You are getting double height rows because you are also adding a paragraph in it.
So you replace the line
XWPFParagraph ansParagraph = new XWPFParagraph(cell.getCTTc().insertNewP(0), cell);
with
XWPFParagraph ansParagraph = cell.getParagraphs().get(0);
And remove the line
cell.addParagraph(ansParagraph);
Then it will work OK.
My code below is lost when opening PDF file which has only one column on the front page and more than 1 column on other pages.
Someone can tell me what I'm doing wrong?
Below my code:
PdfReader pdfreader = new PdfReader(pathNmArq);
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
for (int page=1; page <= lastPage; page++)
{
extractText = PdfTextExtractor.GetTextFromPage(pdfreader, page, strategy);
extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));
/ / ...
}
You use the SimpleTextExtractionStrategy. This strategy assumes that the text drawing instructions in the PDF are sorted by the reading order. In your case that does not seem to be the case.
If you cannot count on the PDF containing drawing operations in reading order but are only using iText text extraction strategies from the distribution, you have to know areas which constitute a single column. If a page contains multiple columns, you have to use RegionTextRenderFilter to restrict to a column and then use the LocationTextExtractionStrategy.
PS: What exactly is your intention in that
extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));
line?
My problem is that I need to add new columns to existing table made in GUI Matlab. I am doing it with the code quoted below but there is one problem - new columns are not editable. Anyone know how to force it to them?
database = get(handles.table,'Data');
[height,width] = size(database);
database(1:height,width+1) = cellstr(get(handles.edit13,'String'));
database(1:height,width+2) = {str2num(get(handles.edit12,'String'))};
database(1,width+3) = cellstr(' ');
set(handles.table,'Data', database);
So.. it solves my problem:
wektortrue=true(1,width)
set(handles.table,'ColumnEditable',wektortrue);