Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for LineSeparator in iText old versions?

Tags:

java

pdf

itext

I'm trying to insert a line separator (you know, that horizontal line that runs across documents) into my document with iText. I've found some resources via Google that use com.lowagie.text.pdf.draw.LineSeparator but the version of iText that I'm using (1.4.2) doesn't seem to have that package.

Can anyone suggest another way to add a nice line seperator for my pdf? And please don't say update the .jar-- I'm locked in to 1.4.2.

Thanks!

like image 796
Fran Fitzpatrick Avatar asked Sep 15 '10 16:09

Fran Fitzpatrick


People also ask

What are the best alternatives to iText in 2022?

SourceForge ranks the best alternatives to iText in 2022. Compare features, ratings, user reviews, pricing, and more from iText competitors and alternatives in order to make an informed decision for your business. 1 Foxit PDF Editor Foxit Software

Should I use iText 5 or iText 7?

We HIGHLY recommend customers use iText 7 for new projects, and to consider moving existing projects from iText 5 to iText 7 to benefit from the many improvements such as: Better language support: Indic, Thai, Khmer, Arabic, Hebrew. (Close-source addon) iText 5 consists of several jars. itext-xtra-x.y.z.jar: extra functionality (PDF 2!)

What's new in itextview Master Edition?

The Master Edition represents a huge step forward compared to the previous Classic version, and introduces advanced features like complete form fields manipulation, pdf linearization and optimization, xfdf import/export, signature and certificate handling, and much more. Compare vs. iTextView Software 19 BCL easyPDF SDK

What is the highest level of support for iText?

They vary from L1 to L5 with "L5" being the highest. Do you think we are missing an alternative of iText or a related project? PLEASE NOTE: iText 5 is EOL, and has been replaced by iText 7. Only security fixes will be added


2 Answers

LineSeparator ls = new LineSeparator();
document.add(new Chunk(ls));

Example: iText in action

like image 152
ftarucco Avatar answered Sep 18 '22 16:09

ftarucco


There is a bit of a messy way around this in the earlier versions of iText. If you store the element above the horizontal line in a PdfPCell, you can then set the border of that to show only the bottom. (That cell can also be blank if needed)

PdfPCell myCell = new PdfPCell(new Paragraph("Hello World") );
myCell.setBorder(Rectangle.BOTTOM);

The result should look like (solid line, not checkered)

Hello World
-----------

This should give you what you desire. Not the optimal solution but it is a way to work around the limitations of the old jar.

For your reference, if you want to perform this trick to put a line on top and below your text to give a result of

-----------
Hello World
-----------

The argument to setBorder() is an int which you can use bitwise operation on to manipulate the values. So the above example can would be accomplished with

myCell.setBorder(Rectangle.BOTTOM | Rectangle.TOP);

edit: Example

//Create the table which will be 2 Columns wide and make it 100% of the page
PdfPTable myTable = new PdfPtable(2);
myTable.setWidthPercentage(100.0f);

//create a 3 cells and add them to the table
PdfPCell cellOne = new PdfPCell(new Paragraph("Hello World"));
PdfPCell cellTwo = new PdfPCell(new Paragraph("Bottom Left"));
PdfPcell cellThree = new PdfPCell(new Paragraph("Bottom Right"));

cellOne.setColspan(2);
cellOne.setBorder(Rectangle.BOTTOM);
cellOne.setHorizontalAlignment(Element.ALIGN_LEFT);

cellTwo.setBorder(Rectangle.NO_BORDER);
cellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
cellThree.setBorder(Rectangle.LEFT);
cellThree.setHorizontalAlignment(Element.ALIGN_RIGHT);

//Add the three cells to the table
myTable.addCell(cellOne);
myTable.addCell(cellTwo);
myTable.addCell(cellThree);

//Do something to add the table to your root document

This should create you a table which looks something like the following (assuming you correct my typos)

Hello World
------------------------------------
Bottom Left      |      Bottom Right
like image 26
Sean Avatar answered Sep 19 '22 16:09

Sean