Table NoWrap Option in iText 7? - itext

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

Related

The text position in TextForm is different between iText5 and iText7

I'm trying to update from iText5 to iText7. In that, I'm aware that the text top position in TextField is difference. See the link image, It is hard to see the difference but the text position is not same; the text position in the pdf generated by iText 7 is slightly upper.
The pdf generation code by iText5
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("itext5.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_7);
writer.setTagged();
doc.open();
doc.addTitle("itext5 sample");
TextField tf = new TextField(writer, new Rectangle(100, 100, 261, 131), "text");
tf.setTextColor(BaseColor.BLACK);
tf.setFontSize(0);
tf.setText("TEST");
writer.addAnnotation(tf.getTextField());
doc.close();
PdfReader reader = new PdfReader("itext5.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("itext5_flatten.pdf"));
stamper.setFormFlattening(true);
stamper.close();
The pdf generation code by iText7
PdfDocument doc = new PdfDocument(new PdfWriter("itext7_flatten.pdf"));
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
PdfTextFormField nameField = PdfTextFormField.createText(doc, new Rectangle(100, 100, 161, 31), "text", "TEST");
nameField.setFontSize(0);
form.addField(nameField);
form.flattenFields();
doc.close();
I searched iText API as much as possible, but any API change text position in TextField seemed to be not provided. Could I adjust text poistion in TextField, in iText7 ?
The source codes and PDf files I used is here.

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:

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 can i change color of text in a cell in itext pdf?

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.

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