Change PDF's hyperlink display text - itext

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

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.

can we add multiline textual watermark in a document using latest iText jar?

I am looking for multi-line textual watermark feature. Does iText latest version support this feature?
I am attaching a picture of the requirement.
Let me know your findings.
Here is an example of how to add repeating watermark as the background to an existing document:
pdfDocument = new PdfDocument(new PdfReader(inFileName), new PdfWriter(outFileName));
PdfPattern.Tiling tiling = new Tiling(new Rectangle(100, 50));
new Canvas(new PdfPatternCanvas(tiling, pdfDocument), pdfDocument, tiling.getBBox()).add(new Paragraph("TESTING")
.setFontColor(ColorConstants.RED)
.setRotationAngle(Math.PI / 10));
for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
PdfPage page = pdfDocument.getPage(i);
new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument)
.saveState()
.setFillColor(new PatternColor(tiling))
.rectangle(page.getCropBox())
.fill()
.restoreState();
}
pdfDocument.close();
This is how the result look visually:

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 7 - canvas with customized page

In iText 2 , we can use PdfContentByte to set customized page-size, however, in iText 7.1.2.
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(0, 0, 2000, 800);
pdfCanvas.rectangle(rectangle);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
Text title =
new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
Text author = new Text("Robert Louis Stevenson").setFont(font);
Paragraph p = new Paragraph().add(title).add(" by ").add(author);
canvas.add(p);
canvas.close();
pdf.close();
even if we set larger width, it didn't work. still keep A4 size. How can i change the pageSize correctly ?
You are adding a new page without specifying a page size, hence the default page size (A4) is used. Please take a look at the API docs for the addPage() method: addNewPage(PageSize pageSize). You need to pass a PageSize argument if you want to get a page with another size.
There's also a setDefaultPageSize() method if you want to change the default page size from A4 to something else.
The PageSize class extends the Rectangle class: http://itextsupport.com/apidocs/iText7/latest/com/itextpdf/kernel/geom/PageSize.html

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