Header with 2 cells aligned in the same row, itextpdf 5.5.10 - itext

I have a row in my header, which looks good, but I need to add another String in the same row, so it is on the right side of it
When I try to add that String in the same cell, everything loses the alignment
private void addHeader(PdfWriter writer) {
PdfPTable tableHeader = new PdfPTable(2);
try {
// set defaults
header.setWidths(new int[] { 20, 12, 12 });
header.setTotalWidth(527);
header.setLockedWidth(true);
header.getDefaultCell().setFixedHeight(5);
header.getDefaultCell().setBorder(Rectangle.BOTTOM);
header.getDefaultCell().setBorderColor(BaseColor.LIGHT_GRAY);
// add text
PdfPCell text = new PdfPCell();
text.setPaddingBottom(12);
text.setPaddingLeft(8);
text.setBorder(Rectangle.BOTTOM);
text.setBorderColor(BaseColor.LIGHT_GRAY);
text.addElement(new Phrase("Hi", new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD)));
text.addElement(new Phrase("", new Font(Font.FontFamily.HELVETICA, 8)));
text.addElement(new Phrase("Hi" + "CVE-2017-2018", new Font(Font.FontFamily.HELVETICA, 9)));
header.addCell(text);
// add image
Image logo = Image.getInstance(App.class.getResource(LOGO_2));
header.addCell(logo);
PdfPCell cveTitle = new PdfPCell();
cveTitle.setPaddingBottom(15);
cveTitle.setPaddingLeft(8);
cveTitle.setBorder(Rectangle.BOTTOM);
cveTitle.setBorderColor(BaseColor.LIGHT_GRAY);
cveTitle.addElement(new Phrase("3 Cell", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD)));
header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
header.addCell(cveTitle);
}
CVE-2010-2016
Is just what I want to do, leave it on the right side
Update
The solution was to add a new value to the array, corresponding to a new cell, and also instantiating PdfPCell again, thks

Just from the classname PdfPTable, I can tell the iText version you are using is pretty outdated.
Please consider switching to a newer version. Latest version is iText7.

I think what you're looking for is colspan and rowspan.
Have a look at the tutorial:
http://developers.itextpdf.com/examples/tables/clone-colspan-and-rowspan

Related

Table NoWrap Option in iText 7?

I'm converting iText5 code to iText7. One of the properties we use is NoWrap (= true) on some of our cells. What is the iText 7 equivolent? Thanks!
new PdfPCell { NoWrap = true, ... }
You can use Property.OVERFLOW_X to control the overflow strategy of the content. But it needs to be set on the elements that contain the content, normally on a paragraph.
Here is an example of a code adding a table with a cell that contains content which does not fit in the given cell width (100pt):
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdfDocument);
Table table = new Table(new float[] {100, 100, 100});
table.setFixedLayout();
table.setWidth(300);
table.addCell("Hello world");
Paragraph p = new Paragraph("ThisIsAVeryLongLongWordWhichOverflowsCellWidth").setFontColor(ColorConstants.GREEN);
table.addCell(p);
p.setProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE);
table.addCell("Last cell");
document.add(table);
document.close();

How can I take text and convert to PDF with landscape orientation and two text columns?

I need to change the paper orientation, and then divide the text into two columns. I want like it:
I come from a large text file and I need to add all of this text in the PDF as in the photo.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
PageSize ps = PageSize.A4;;
Document doc = new Document(pdfDoc, ps);
BufferedReader br = new BufferedReader(new FileReader("verybigfileWithText.txt"));
while ((line = br.readLine()) != null) {
//split pdf and add text in two column without overlapping page
}
doc.close();
And I need to be able to change margin in center(between two columns) and margin top/right/bot/left. And get width every columns :)
Help me please
Pdf:
https://dropmefiles.com/HqD6f
To change the orientation of the page you can use PageSize#rotate, e.g. PageSize.A4.rotate()
To put content into two (or more) columns, you can create your own document renderer, or use an existing ColumnDocumentRenderer which suits your needs. It accepts column areas which allows you to control margins (or even position columns in a peculiar way which is probably not your use case):
Document document = new Document(pdfDocument, PageSize.A4.rotate());
Rectangle[] columnAreas = new Rectangle[] {new Rectangle(30, 30, 350, 520), new Rectangle(430, 30, 350, 520)};
ColumnDocumentRenderer renderer = new ColumnDocumentRenderer(document, columnAreas);
document.setRenderer(renderer);
document.add(new Paragraph(text).setTextAlignment(TextAlignment.JUSTIFIED));
document.close();
The result looks like this:

Adding PdfPCell in iText PdfPtable doesnt work anymore

I have created several PdfPTable without problem, but now I dont know what is wrong. Fourth line doesnt displayed.
table = new PdfPTable(4);
table.setSpacingBefore(10);
columnWidths = new float[] {60,10,10,10};
table.setWidths(columnWidths);
PdfPCell cellFooter = new PdfPCell(new Phrase("Rows One", ARIAL_12_BOLD));
table.addCell(cellFooter);
for(int k=0; k<3; k++){
table.addCell("");
}
cellFooter = new PdfPCell(new Phrase("Row Two", ARIAL_12_BOLD));
table.addCell(cellFooter);
for(int k=0; k<3; k++){
table.addCell("");
}
cellFooter = new PdfPCell(new Phrase("Row Three", ARIAL_12_BOLD));
cellFooter.setColspan(4);
table.addCell(cellFooter);
// Row Four not displayed
for(int k=0; k<4; k++){
table.addCell("");
}
To answer, let me ask you this: How many pixels in height is an empty string?
The simplest way to get around this is to just add something. You should be able to just use:
table.addCell(" ");
However, if you're changing font sizes elsewhere the height might be different so you might want to explicitly set the font (and thus the height) of the empty cell:
table.addCell(new Phrase(" ", ARIAL_12_BOLD));
You could also explicitly define heights on cells but this is usually the easiest way.

positioning text using itextSharp in C3

I am trying to generate a pdf on button click. I am having challenge to design the page. If anybody can help on, how to position the text in particular position.
Say I want my address to in the top right corner and heading in the center.
Here is the code I am working on:
private void pdf_btn_Click(object sender, EventArgs e)
{
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
using (FileStream stream = new FileStream(svg.FileName + ".pdf", FileMode.Create))
{
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
// var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
// First, create our fonts... (For more on working w/fonts in iTextSharp, see: http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts
var titleFont = FontFactory.GetFont("Arial", 18, Convert.ToInt32(Font.Bold));
var AddressFont = FontFactory.GetFont("Arial", 7, Convert.ToInt32(Font.Bold));
var boldTableFont = FontFactory.GetFont("Arial", 12, Convert.ToInt32(Font.Bold));
var endingMessageFont = FontFactory.GetFont("Arial", 10, Convert.ToInt32(Font.Italic));
var bodyFont = FontFactory.GetFont("Arial", 12);
// Add the "" title
document.Add(new Paragraph("Certificate of Analysis", titleFont));
// Add Address
var text = document.Add(new Paragraph("Abc Company", AddressFont));
document.Add(new Paragraph("Tel:(xx) xxx-xxxx, (xxx) xxx-xxx", AddressFont));
document.Add(new Paragraph("Fax: (xxx) xxx-xxx, , AddressFont));
document.Close();
stream.Close();
}
}
There are different ways to achieve what you want.
Currently, you are telling iText to do the layout. If you want to center the text "Certificate of Analysis", then you could change the alignment of the Paragraph like this:
Paragraph header = new Paragraph("Certificate of Analysis", titleFont);
header.Alignment = Element.ALIGN_CENTER;
document.Add(header);
If you want the text "Abc Company" to be right-aligned, you can change he alignment like this:
Paragraph company = new Paragraph("Abc Company", AddressFont);
company.Alignment = Element.ALIGN_RIGHT;
document.Add(company);
Of course: when we add company, it will start on a new line under header. Maybe you want the text to be on the same line.
In that case, why not use a PdfPTable?
PdfPTable myTable = new PdfPTable(3);
myTable.WidthPercentage = 100;
PdfPCell mydate = new PdfPCell(new Phrase("April 22, 2015", myfont));
mydate.Border = Rectangle.NO_BORDER;
myTable.AddCell(mydate);
PdfPCell header = new PdfPCell(new Phrase("Certificate of Analysis", titleFont));
header.Border = Rectangle.NO_BORDER;
header.HorizontalAlignment = Element.ALIGN_CENTER;
myTable.AddCell(header);
PdfPCell address = new PdfPCell(new Phrase("Company Address", AddressFont));
address.Border = Rectangle.NO_BORDER;
address.HorizontalAlignment = Element.ALIGN_RIGHT;
myTable.AddCell(address);
doc.Add(myTable);
Now the available width of your page will be divided in three, the date will be aligned to the left, the header will be centered, and the address will be aligned to the right. Obviously, you can add more data to a PdfPCell and more rows to the PdfPTable. This is just a simple proof of concept.
Another option, is to add the text at an absolute position (using coordinates). This can be done by using the ColumnText object. There are plenty of examples on this in The Best iText Questions on StackOverflow

ITextSharp, Assigning a font to List within a cell?

Is there a way to assign a font to a list that is added to table cell in ITextsharp. I'm sure it must be straightforward but I'm missing it.
Dim tblSignature As New PdfPTable(1)
tblSignature.WidthPercentage = 90.0F
Dim _baseFont As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED)
Dim _bFont As New Font(_baseFont, 4, Font.ITALIC, Color.RED)
Dim cell As New PdfPCell(New Phrase(TheItem.Value, _bFont))
TheParagraph.Font = _bFont
Dim list As New List(list.UNORDERED, 25.0F)
list.SetListSymbol("[ ]")
If ListItems.Count > 0 Then
For Each ListItem In .ListItems
//I have tried adding as chunk/phrase and applying font but no joy
//Item not added to cell
//list.Add(New Chunk(ListItem, _bFont))
list.Add(ListItem)
Next
list.IndentationLeft = 5.0F
cell.AddElement(list)
End If
cell.Colspan = 6
cell.HorizontalAlignment = 0
cell.PaddingBottom = 5.0F
cell.PaddingLeft = 5.0F
cell.PaddingRight = 5.0F
'' add to doc
tblSignature.AddCell(cell)
TheParagraph.Add(tblSignature)
Does anyone know How I'd change this so that the list/cell has the specific font I've set at the top.
Cheers
The iTextSharp.text.ListItem object has an iTextSharp.text.Font property you can set.
Please note that declarations such as new PdfPCell or new Phrase will not work with iTextSharp.text.List's add method.But both of them fairly work with PdfPTable.
I used iTextSharp.text.List for one of my software's pdf print out in the following ways.I hope this may be useful to you also.
This is a program which is used in one of my software developed with vb.net(visual studio 2010 and windows 7 platform) and iTextSharp latest version.
for pdf print out.It demonstrates how we can manupulate iTextSharp.text.List's font properties.
Dim doc1 As New Document()
Dim pathpdf As String = "C:/temp"
'path = ServerMapPath("PDFs");
PdfWriter.GetInstance(doc1, New FileStream(pathpdf + "/Doc1.pdf", FileMode.Create))
' Now to begin actually working with the document, open it, , add a new list,and, then close it:
doc1.Open()
Dim fnt1 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.NORMAL)
Dim fnt2 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD)
Dim fnt3 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.NORMAL)
Dim lst3 As New iTextSharp.text.List()
lst3.SetListSymbol("")
'add a list item in bold letters.
Dim m_DataHeadOnly As String
m_DataHeadOnly = "This is the itextsharp font setting with list"
lst3.Add(New iTextSharp.text.ListItem(m_DataHeadOnly, fnt2))
doc1.add(lst3)
doc1.close()