I am working on an older project so the iTextSharp version is 3.1.7.0.
The header part is working just fine, but I have some issues with the footer. When I add only text, footer is showing where it should - at the bottom of the page. But when I add table instead of just text, footer jumps on top of the page. This is my code, what am I missing?
Document document = new Document(PageSize.A4, 36, 40, 0, 30);
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Table Header"));
cell.Colspan = 3;
cell.HorizontalAlignment = 2;
table.AddCell(cell);
table.AddCell("Cell1example");
table.AddCell("Cell2example");
table.AddCell("Cell3example");
table.AddCell("Cell4example");
table.AddCell("Cell5example");
table.AddCell("Cell6example");
Phrase footerPhrase = new Phrase();
footerPhrase.Add(table);
footerPhrase.Leading = 10;
HeaderFooter footer = new HeaderFooter(footerPhrase, false);
document.Footer = footer;
Related
I use iTextSharp to create a PDF (it is a Receipt which will be printed on a Receipt-Printer with endless-paper-length):
public void GenrateReport() {
Document doc = new Document(new RectangleReadOnly(225f, 10000f), 5, 5, 5, 5);
var temp = Path.GetTempFileName();
PdfWriter.GetInstance(doc, new FileStream(this.reportConfiguration.DestinationPath, FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(1);
var cell = new PdfPCell();
cell.Border = iTextSharp.text.Rectangle.NO_BORDER;
cell.AddElement(new Paragraph("aaa"));
cell.VerticalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("bbb"));
cell.VerticalAlignment = Element.ALIGN_LEFT;
table.AddCell(cell);
doc.Add(table);
doc.Close();
}
The content of the table can varying. This is only a sample with two rows.
The font and the size of the font can also varying.
I am searching for a way, that I can change the height of the page at the and to the content of the page. Is this possible?
I have this code and I want to add a table as a footer. But the problem is I cannot pass the variables:
Document doc = new Document();
doc.AddTitle("e-8D Report_" + report.OSIE8DNO + ".pdf");
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.SetPageSize(PageSize.A4);
doc.SetMargins(18f, 18f, 18f, 18f); // 0.25 inch margins
doc.Open();
PdfPTable table = new PdfPTable(10)
{
WidthPercentage = 100,
LockedWidth = true,
TotalWidth = 560f
};
float[] widths = new float[] { 32f, 73f, 70f, 70f, 70f, 70f, 70f, 70f, 73f, 32f };
table.SetWidths(widths);
// rest of the document
// ...
// rest of the document
// Below is the part that I want to add as footer
#region Footer
// left margin
cell = new PdfPCell(new Phrase(" ", font6))
{
Border = Rectangle.NO_BORDER
};
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Reviewed & Approved by :", font6))
{
Colspan = 4,
BackgroundColor = bgBlue,
HorizontalAlignment = Element.ALIGN_LEFT,
Border = Rectangle.TOP_BORDER | Rectangle.LEFT_BORDER
};
table.AddCell(cell);
cell = new PdfPCell(new Phrase("For document approval by email, no Manual / e-Signature required &", font2))
{
Colspan = 4,
BackgroundColor = bgBlue,
HorizontalAlignment = Element.ALIGN_LEFT,
Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER
};
table.AddCell(cell);
// right margin
cell = new PdfPCell(new Phrase(" ", font6))
{
Border = Rectangle.NO_BORDER
};
table.AddCell(cell);
// the rest of the cell
//...
// the rest of the cell
#endregion
doc.Add(table);
//writer.PageEvent = new PDFFooter();
doc.Close();
With this variables, I can't make the footer with PdfPageEventHelper from this source:
Footer in pdf with iTextSharp
Please help
Already got the answer. I add parameter to the PdfPageEventHelper Class:
public PdfPTable footer { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
document.Add(footer);
}
And call from the page (create complete table first on the page):
writer.PageEvent = new PDFHeaderFooter() { footer = table };
I'm trying to add three cells in one row, which have colored background with the same color. The problem is that I see space (or vertical border) between these cells, but I don't need this space.
As you can see on the image, there is some space. Maybe this is the wrong way to do what I need. I need this green text in the center of page and white text on the right side at the same row and with the same background color.
This is my code:
PdfPCell emptyCell = new PdfPCell();
emptyCell.BackgroundColor = BaseColor.DARK_GRAY;
emptyCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
emptyCell.VerticalAlignment = Element.ALIGN_CENTER;
emptyCell.MinimumHeight = 30f;
emptyCell.Colspan = 3;
emptyCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(emptyCell);
iTextSharp.text.Font clubFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL, new BaseColor(0.541f, 0.765f, 0f));
PdfPCell cell = new PdfPCell();
Paragraph clubName = new Paragraph(clubString, clubFont) {Alignment = Element.ALIGN_CENTER};
cell.AddElement(clubName);
cell.BackgroundColor = BaseColor.DARK_GRAY;
cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.MinimumHeight = 30f;
cell.Colspan = _currentTable.NumberOfColumns - 6;
cell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(cell);
iTextSharp.text.Font dispFont = new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 10, Font.NORMAL, BaseColor.WHITE);
PdfPCell dispCell = new PdfPCell();
Paragraph dispersion = new Paragraph(dispersionString, dispFont);
dispersion.Alignment = Element.ALIGN_CENTER;
dispCell.AddElement(dispersion);
dispCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
dispCell.VerticalAlignment = Element.ALIGN_CENTER;
dispCell.MinimumHeight = 30f;
dispCell.Colspan = 3;
dispCell.BackgroundColor = BaseColor.DARK_GRAY;
dispCell.Border = Rectangle.NO_BORDER;
_currentTable.AddCell(dispCell);
I want to create a PDF file with Icard showing in it.
Following is the code:
iTextSharp.text.Font custFont = new iTextSharp.text.Font() { Size=6f};
int borderSize = 1;
Document IcardDoc = new Document(PageSize.HALFLETTER);
PdfWriter.GetInstance(IcardDoc, Response.OutputStream);
IcardDoc.Open();
PdfPTable icardTable = new PdfPTable(2);
iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/logo.jpg"));
logoImage.WidthPercentage = 15f;
PdfPCell cell = new PdfPCell(logoImage, false);
cell.PaddingLeft = 10f;
cell.PaddingTop = 10f;
cell.PaddingBottom = 10f;
cell.PaddingRight = 10f;
cell.Colspan = 2;
cell.Border = 0;
cell.HorizontalAlignment = 1;
icardTable.AddCell(cell);
icardTable.AddCell(new PdfPCell(new Phrase("Name:", custFont)) { Border = borderSize });
icardTable.AddCell(new PdfPCell(new Phrase(student.firstname + " " + student.lastname, custFont)) { Border = borderSize });
The print document size will be 8.5 cm in height and 5.4 cm in width.
So need to fit it that way, is there any solution to this because the above code is not fitting the table on page and in proper size?
Any other format will also do like word etc.
figured out the solution :
Document IcardDoc = new Document(new Retangele(200f, 315f),35f,35f,0f,0f);
and adjust the width of the table accordingly:
PdfPTable icardTable = new PdfPTable(2);
icardTable.WidthPercentage = 140f;
to fit table exactly on page, if decrease rectangle width then increase table WidthPercentage and vice versa.
You also need to care of left and right margins defined as 35f in Document class constructor
This worked for me.
var pgSize = new iTextSharp.text.Rectangle(1042, 651);
var doc = new iTextSharp.text.Document(pgSize, 0, 0, 0, 0);
I want to create th following PDF layout with ITextSharp:
I use the following code to generate my table:
Document document = new Document(PageSize.A4);
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
table.SetWidths(new float[] { 450, 100 });
table.WidthPercentage = 100;
cell = new PdfPCell(new Phrase("Item cod werwerwer"));
table.AddCell(cell);
cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(string.Empty));
table.AddCell(cell);
cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);
document.Add(table);
writer.CloseStream = false;
document.Close();
memoryStream.Position = 0;
return memoryStream.ToArray();
How can I force table to cover full page height without use fixed height value?
you can use table.ExtendLastRow = true;
Tables flow, that's just what they do. If you want to change the height then you're going to need to use fixed values. You could calculate these fixed values at runtime by trying to figure out what the height of some text will be in a given cell at a given width using a given font. Or you could just fix it at a magic number which is what the code below does.
At the top is the magic constant. When we create the document we specify 0 for all margins so that we fill the entire page. You can change this but you'll have to adjust the calculations below. Then in the first row we set one of the cell's MinimumHeight to the page's height minus the constant and in the second row we set one of the cell's height to the constant.
//Fixed height of last cell
float LAST_CELL_HEIGHT = 50f;
//Create our document with zero margins
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "A4.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
table.SetWidths(new float[] { 450, 100 });
table.WidthPercentage = 100;
cell = new PdfPCell(new Phrase("Item cod werwerwer"));
//Set the first cell's height to the document's full height minus the last cell
cell.MinimumHeight = document.PageSize.Height - LAST_CELL_HEIGHT;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);
cell = new PdfPCell(new Phrase(string.Empty));
//Set the last cell's height
cell.MinimumHeight = LAST_CELL_HEIGHT;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("100"));
table.AddCell(cell);
document.Add(table);
writer.CloseStream = false;
document.Close();
fs.Close();