Footer image printed twice when printing certificates in iText - itext

When we print certificate all most print in certificate but footer image is printing twice. I am using Pdfptable for printing footer image.
table = new PdfPTable(1);
int footerWidth[] = {100};
table.setWidths(footerWidth);
table.setWidthPercentage(100);
Image image2 = Image.getInstance("footerImage.jpg");
cell = new PdfPCell(image2,true);
cell.setBorder(0);
table.setTotalWidth(document.getPageSize().width()-document.leftMargin()-document.rightMargin());
table.addCell(cell);
//write the footer
table.writeSelectedRows(0, -1, document.leftMargin(), table.getTotalHeight()+document.bottomMargin(), writer.getDirectContent());
EventLog.write(appName,"done adding footer",logFile);
document.add(table);

Your table is added twice because you add it twice.
You add it a first time when you use writeSelectedRows():
table.writeSelectedRows(0, -1, document.leftMargin(), table.getTotalHeight()+document.bottomMargin(), writer.getDirectContent());
This adds the table at a very specific position defined by the coordinates:
x = document.leftMargin();
y = table.getTotalHeight()+document.bottomMargin();
You add the table a second time, when you do this:
document.add(table);
This adds the table at whichever position the page cursor is at, which is usually immediately after the previous content that was added with a document.add() method.
PS:
Headers and footers are usually added in an OnEndPage() page event. You're adding the footer only once. You may want to use page events to add the footer on every page.

Related

iTextPDF 5.5 : Why 2 times table created when used table.writeSelectedRows()?

I want to add table middle of page after header.
Found method 'writeSelectedRows' which can be used to move table location on page.
But with this method, table is created 2 times, one at the start of the page, other at the specified location.
Please help in resolving this.
//Using iTextPDF 5.5.10
document.open();
PdfPTable table = new PdfPTable(1);
// table.setPaddingTop(250);
table.setTotalWidth(100);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
// cell.setFixedHeight(13);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(1);
cell.setBackgroundColor(BaseColor.CYAN);
table.addCell(cell);
table.writeSelectedRows(0, -1, 50, 650, writer.getDirectContent());
document.add(table);
document.open();
This generates following output.
I want to add table at given location one time only and not at start of the page.
The table appears twice in your document because you add it twice! First you add it at the desired position:
table.writeSelectedRows(0, -1, 50, 650, writer.getDirectContent());
And then you add it for automatic layout, putting it at the top of the page:
document.add(table);
If you only want it once, add it only once, i.e. remove one of those lines.

Why do the two table rows get so close to each other?

I am creating two rows. The first row contains 0, 1, ..., 10. The second row contains a radio button beneath each number. Here is my code:
for (int k=0; k<=10; k++) {
String text = k + "";
cell = new PdfPCell(new Phrase(text));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setPaddingTop(5);
table.addCell(cell);
}
for (int k=0; k<=10; k++) {
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
cell.setPaddingTop(10);
cell.setCellEvent(new MyCellField(radiogroup, k+""));
table.addCell(cell);
}
MyCellField is similar to what is available on this page:
http://developers.itextpdf.com/examples/form-examples/create-fields-table
The problem is that no matter what spacing I give to the second row, the produced radio buttons in the second always overlap with their respective numbers.
You create a cell without any content when you do this: cell = new PdfPCell();
No content means: no height. You probably want to add this line:
cell.setFixedHeight(30);
This way, you are sure that the height of the cell will be sufficient to add a radio button.
In iText 5, we often use cell.setUseAscender(true); and cell.setUseDescender(true); to get a better positioning of the text. This problem is addressed in iText 7.

Add Header with image in word corrupts document

I have a Header with a table (1 row , 3 cells).In first cell there is a image and in 2nd and 3rd there are some text paragraphs
I append header to doc dynamically like this:
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
string rId = mainPart.GetIdOfPart(headerPart);
HeaderReference headerRef = new HeaderReference();
headerRef.Id = rId;
headerRef.Type = HeaderFooterValues.Default;
sectionProps.RemoveAllChildren<HeaderReference>();
sectionProps.Append(headerRef);
headerPart.Header = HeaderCreator.GenerateHeader();
headerPart.Header.Save();
In document there is a page with a group of shapes.
Everything works ok if I
Remove the page with shapes
Remove the logo from Header
Any suggestions?

Itextsharp V 5.x:image cell height in pdfptable

I am creating table and adding cells to table with text or image content.
var pdfTable = new PdfPTable(2);
nCell = new PdfPCell(new Phrase("A")) {HorizontalAlignment = 1};
pdfTable.AddCell(nCell);
pdfTable.AddCell("B");
pdfTable.AddCell(qrImg);
pdfTable.AddCell(image39);
pdfTable.AddCell("C");
pdfTable.AddCell("D");
pdfTable.SpacingBefore = 20f;
pdfTable.SpacingAfter = 30f;
document.Add(pdfTable);
renders 3 rows and displays image in row2
if I add cells by creating pdfpcell object first:
var cell = new PdfPCell(qrImg};
pdfTable.AddCell(nCell);
only rows 1 and 3 are visible.
If I add height property to cell then the image gets diaplayed.
my questions ( 3 but related);
is it required for us to specify height when adding cell with image ( cells added with text content - phrase resenders correctly) ?
Is there something I am missing when creating a new cell, which prevents images to be rendered?
should I always be using Addcell(image) when adding image content?
Thank you all,
Mar
Its easier to understand what's going on if you browse the source. A table within iText keeps a property around called DefaultCell that gets reused over and over. This is done so that the basic cell properties are kept from cell to cell. When you call AddCell(Image) the DefaultCell's image is set to the image, then added to the table, and finally the image get's null'd out.
543 defaultCell.Image = image;
544 AddCell(defaultCell);
545 defaultCell.Image = null;
The PdfCell(Image) constructor actually internally calls an overload PdfPCell(Image, bool) and passes false as the second parameter, fit. Here's the constructor's conditional on fit:
152 if (fit) {
153 this.image = image;
154 Padding = borderWidth / 2;
155 }
156 else {
157 column.AddText(this.phrase = new Phrase(new Chunk(image, 0, 0, true)));
158 Padding = 0;
159 }
If you pass false to fit, which is the default, you'll see that the image is added in a much more complicated way.
So basically you can add an image in three main ways (okay, lots more actually if you use nested tables or chunks or phrases), the first below picks up the defaults and is probably what you want. The second is more raw but gets you closer to what you probably want. The third is the most raw and assumes that you know what you're doing.
var qrImg = iTextSharp.text.Image.GetInstance(sampleImage1);
//Use the DefaultCell, including any existing borders and padding
pdfTable.AddCell(qrImg);
//Brand new cell, includes some padding to get the image to fit
pdfTable.AddCell(new PdfPCell(qrImg, true));
//Brand new cell, image added as a Chunk within a Phrase
pdfTable.AddCell(new PdfPCell(qrImg));

Setting Alignment for each Element when more than one Element added to a PDFPcell

In a table, I have a few cells that contain multiple elements. For Example, to indicate address , the cell may contain a phrase containing an "ADDRESS:" header Chunk followed by another chunk containing the actual address:
FROM: -- Chunk 1 in Phrase
123 Main St, Some City, ST -- Chunk 2 in Phrase
As of now, to align the cell contents, I am using the following code in the PDFPcell:
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
However, this aligns all of the cell contents to the middle of the cell. If I want to put Chunk 1 at TOP_LEFT and Chunk 2 at BOTTOM_LEFT, is it possible to achieve it with iTextSharp? Essentially, I am looking for a way to align various elements within a cell at different locations.
Unfortunately the only way to do what you want is to add a sub-table in place of your multiple chunks.
t.AddCell("Row 1");
PdfPTable subTable = new PdfPTable(1);
subTable.DefaultCell.VerticalAlignment = Element.ALIGN_TOP;
subTable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
subTable.AddCell("Top Align");
subTable.DefaultCell.VerticalAlignment = Element.ALIGN_BOTTOM;
subTable.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
subTable.AddCell("Bottom Align");
t.AddCell(subTable);
doc.Add(t);