how can i change color of text in a cell in itext pdf? - itext

I just want to change color of "PAN DETAILS"(cell text) in my code please tell with a good example.
PdfPTable table2 = new PdfPTable(1);
table2.setWidthPercentage(100);
PdfPCell cellsss;
cellsss = new PdfPCell(new Phrase("PAN DETAILS"));
cellsss.setBorderColorTop(BaseColor.BLACK);
cellsss.setColspan(0);
cellsss.setBorderColor(BaseColor.RED);
BaseColor myColorpan = WebColors.getRGBColor("#b60548");
cellsss.setBackgroundColor(myColorpan);
cellsss.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cellsss);
cellsss = new PdfPCell(new Phrase("PAN DETAILS"));
cellsss.setBorderColor(BaseColor.BLACK);
document.add(table2);

This is maybe a duplicate. But anyway: The Font class holds the color so you have to set the color in the Font-class.
Font font = new Font(bf_times)
font.setColor(Color.BLACK);
This must be set in the Phrase:
new Phrase("PAN DETAILS", font)
From the duplicate.

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

Itext How to set both UTF encoding and Font to a Paragraph

I want to set a font to a text that is in cyrillic.I successfully convert the text to cyrilic, but i cannot set a Font to the same text.
File fontFile = new File("arialuni.ttf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
writer.getAcroForm().setNeedAppearances(true);
Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.ITALIC);
BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(unicode));
addContent(document,article.getTitle(),fs,boldFont);
private static void addContent(Document document,String paragraph,FontSelector fs,Font font) throws DocumentException {
Phrase phrase = fs.process(paragraph);
Paragraph p = new Paragraph(phrase.toString(),font);
document.add(p);
}
As #mkl indicates in the comments, you are mixing FontSelector functionality that gives you a Phrase that could use the appropriate unicode fonts (fonts with BaseFont.IDENTITY_H as encoding parameter), with creating a Paragraph with a simple font (Font.FontFamily.TIMES_ROMAN).
When you do fs.process(paragraph), you get a Phrase in which every Chunk has the correct font, but when you do phrase.toString(), you throw away all those fonts, and you replace them with Font.FontFamily.TIMES_ROMAN. That doesn't make any sense.
Why don't you replace this:
Phrase phrase = fs.process(paragraph);
Paragraph p = new Paragraph(phrase.toString(),font);
document.add(p);
with:
document.add(fs.process(paragraph));
Why does your addContent() method need a Font as parameter? Also, if you really need a Paragraph object you can also do this:
Paragraph p = new Paragraph();
p.add(fs.process(paragraph));
document.add(p);
Or even:
Paragraph p = new Paragraph(fs.process(paragraph));
document.add(p);
As long as you don't replace the correct fonts with the incorrect fonts by "flattening" the Phrase to a String, you're probably OK.
Note that you probably even don't need the FontSelector. There's nothing wrong with doing this:
BaseFont unicode = BaseFont.createFont(
fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(unicode, 12);
Paragraph p = new Paragraph(paragraph, font);
It seems to me that you are making things unnecessary complex.

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

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

how to set header font color in iText

HeaderFooter header =
new HeaderFooter(new Phrase("test", new Font(bf_times)), false);
header.setAlignment(Element.ALIGN_CENTER);
header.setBackgroundColor(new Color(0xB5091E));
document.setHeader(header);
I want to set the Font color for test to white (FFFFFF)
The Font is the class holding the color.
Font font = new Font(bf_times)
font.setColor(Color.WHITE);
This should do it.

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