The text position in TextForm is different between iText5 and iText7 - itext

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.

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:

iText Pdf Merging Checkbox Issue

We are using iText Pdf API version 5.5.3 and we are trying to merge pdf documents which sometimes contain checkboxes , we write pdf document in servlet output stream and displays in iframe.
It works well in IE(tried in 11) but checkbox doesn't appear in Chrome(tried in 52).
Below is the sample code we use to display PDF.
InputStream in = docFile.getImage()
.getBinaryStream();
reader = new PdfReader(in);
pdfReaderList.add(reader);
document = new Document(PageSize.A4);
byteArrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, byteArrayOutputStream);
document.setMargins(35, 35, 48, 75);
document.open();
copy.setMargins(35, 35, 48, 75);
// add a document
Iterator<PdfReader> pdfReader = pdfReaderList.iterator();
while (pdfReader.hasNext()){
PdfImportedPage page;
reader = MCPDFFileWriter.unlockPdf(pdfReader.next());
for (int currentPage = 1; currentPage <= reader.getNumberOfPages(); currentPage++) {
page = copy.getImportedPage(reader, currentPage);
pageNum++;
copy.addPage(page);
footerNamesList.add(fileName);
footerMap.put(footerMap.size() + 1, fileName);
}
}
document.close ();
reader.close();
copy.close();
I also gone through this Link and given solution seems to be working when I use below code -
InputStream in = docFile.getImage()
.getBinaryStream();
reader = new PdfReader(in);
pdfReaderList.add(reader);
document = new Document(PageSize.A4);
byteArrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, byteArrayOutputStream);
document.setMargins(35, 35, 48, 75);
document.open();
copy.setMargins(35, 35, 48, 75);
**copy.setMergeFields();**
// add a document
**copy.addDocument(reader);**
document.close ();
reader.close();
copy.close();
However I sometimes face OutOfMemoryError and looking for some idea if there is any known issue with copy.setMergeFields();copy.addDocument(reader); and how it can be overcome ?
A bug was also reported in chrome ( in the thought of plugin issue) and sample pdf are attached there

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

Change PDF's hyperlink display text

Using iTextSharp how can I modify the display text of hyperlink in a pdf document. I can change the annotation link, but my target is to change the displaying text of the hyperlink not the actual web link. Is it possible?
I don't want to add a new anchor, I just want to modify the existing anchor with a different text, not changing existing web link.
Thanks
use this,
using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
{
Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
doc.Open();
Font link = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);
Anchor anchor = new Anchor("google", link);
anchor.Reference = "https://www.google.co.in";
doc.Add(anchor);
doc.Close();
}