Change PDF-pagesize to content after generation - c#

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?

Related

how to add footer (with variables/class) with itextsharp pdf

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 };

iTextSharp NET Remove spacing between cells

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);

iTextSharp - footer on top of the PDF page issue

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;

iTextSharp SetCharacterSpacing broken when using Right Align

I have a table with two cells. The first cell is left aligned and the second cell is right aligned. I also want to change the character spacing of the text. I have found that changing the character spacing breaks the alignment so the right align text ends up outside the boundaries if the table.
I have created some test code below and the link to the output PDF is in this link
https://skydrive.live.com/redir?resid=1ECE2061CFF9124B!190&authkey=!ANcB0z_BN3N4UFo
Are there any alternatives or workarounds to getting the character spacing working well with the right alignment?
public void CharacterSpacingTest()
{
float margin = 50;
using (var stream = new MemoryStream())
{
Document document = new Document();
document.SetPageSize(new Rectangle(PageSize.A4.Width, PageSize.A4.Height));
document.SetMargins(margin, margin, 30f, 100f);
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.CloseStream = false;
document.Open();
PdfPTable table = new PdfPTable(new float[] { 100 });
table.TotalWidth = PageSize.A4.Width - margin - margin;
table.WidthPercentage = 100f;
table.LockedWidth = true;
// Create a row that is right aligned
PdfPCell cell1 = new PdfPCell();
cell1.AddElement(new Paragraph("Hello World") { Alignment = Element.ALIGN_RIGHT });
cell1.BorderWidth = 1;
table.AddCell(cell1);
// Change the character spacing
PdfContentByte cb = writer.DirectContent;
cb.SetCharacterSpacing(1f);
// Create a row that is left aligned
PdfPCell cell2 = new PdfPCell();
cell2.AddElement(new Paragraph("Hello World"));
cell2.BorderWidth = 1;
table.AddCell(cell2);
document.Add(table);
document.Close();
Blobs.SaveToFile(Blobs.LoadFromStream(stream), #"c:\Dev\test.pdf");
}
}
I have managed to fix it by using chunks to set the character spacing. See amended code.
public void CharacterSpacingTest()
{
float margin = 50;
using (var stream = new MemoryStream())
{
Document document = new Document();
document.SetPageSize(new Rectangle(PageSize.A4.Width, PageSize.A4.Height));
document.SetMargins(margin, margin, 30f, 100f);
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.CloseStream = false;
document.Open();
PdfPTable table = new PdfPTable(new float[] { 100 });
table.TotalWidth = PageSize.A4.Width - margin - margin;
table.WidthPercentage = 100f;
table.LockedWidth = true;
// Create a row that is right aligned
PdfPCell cell1 = new PdfPCell();
cell1.AddElement(new Paragraph(GetChunk("Hello World")) { Alignment = Element.ALIGN_RIGHT });
cell1.BorderWidth = 1;
table.AddCell(cell1);
// Create a row that is left aligned
PdfPCell cell2 = new PdfPCell();
cell2.AddElement(new Paragraph(GetChunk("Hello World")));
cell2.BorderWidth = 1;
table.AddCell(cell2);
document.Add(table);
document.Close();
Blobs.SaveToFile(Blobs.LoadFromStream(stream), #"c:\Dev\test.pdf");
}
}
private Chunk GetChunk(string text)
{
Chunk chunk = new Chunk(text);
chunk.SetCharacterSpacing(1);
return chunk;
}

ITextSharp full page height layout

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();

Categories