Add anchor to pdf using itext java - java

I am trying to add anchor(named destinations) to pdf using itext java api. But it's not working.When I click the text , nothing happens.This is what I am doing .
Anchor anchor =
new Anchor("Jump down to next paragraph");
anchor.setReference("#linkTarget");
Paragraph paragraph = new Paragraph();
paragraph.add(anchor);
document.add(paragraph);
Anchor anchorTarget =
new Anchor("This is the target of the link above");
anchor.setName("linkTarget");
Paragraph targetParagraph = new Paragraph();
targetParagraph.setSpacingBefore(50);
targetParagraph.add(anchorTarget);
document.add(targetParagraph);
What am I doing wrong?. Any help

It worked for me. setLocalGoto() and setLocalDestination() will do the magic.
Chunk chunk = new Chunk("Contact information");
chunk.setLocalGoto("contact");
document.add(new Paragraph(chunk));
document.newPage();
chunk chunk1 = new Chunk("Contact information");
chunk1.setLocalDestination("contact");
Chapter chapter = new Chapter(new Paragraph(chunk1),1);
chapter.setNumberDepth(0);
document.add(chapter);

You are not passing anchorTarget in setName(), that's why it's not leading you towards #linktarget
Anchor anchorTarget = new Anchor("This is the target of the link above");
anchorTarget.setName("linkTarget");
Paragraph anchorTargetLinkParagraph = new Paragraph();
anchorTargetLinkParagraph.setAlignment(Element.ALIGN_CENTER);
anchorTargetLinkParagraph.add(anchorTarget);
document.add(anchorTargetLinkParagraph);

Related

I want to change the Position of Anchor link in my PDF. Is there anyway to do it?

This is my code which is redirecting me from 1 page to another in same PDF
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("PageShifting.pdf"));
document.open();
Anchor anchor = new Anchor("Click here to Redirect at Page no.03");
anchor.setReference("#linkTarget");
Paragraph paragraph = new Paragraph("Hello this is new PDF \n");
paragraph.add(anchor);
paragraph.setPaddingTop(1000);
document.add(paragraph);
document.newPage();
Anchor anchorTarget = new Anchor("This is the target of the link above");
anchorTarget.setName("linkTarget");
Paragraph targetParagraph = new Paragraph();
targetParagraph.setSpacingBefore(50);
Chunk chunk = new Chunk("Contact information");
chunk.setLocalGoto("contact");
document.add(new Paragraph(chunk));
document.newPage();
targetParagraph.add(anchorTarget);
document.add(targetParagraph);
System.out.println("PDF Generated");
document.close();

Set width of space with iText 7

Is there a way to set the width of all spaces in a Paragraph in iText 7?
So for example when showing the text "Hello world spacetest" I would like to be able to set the space between "Hello" and "world", and the space between "world" and "spacetest" to 3em.
Use Paragraph#setWordSpacing for that: e.g. p.setWordSpacing(10);.
Full code sample:
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdfDocument);
Paragraph p = new Paragraph("Hello world spacetest");
p.setWordSpacing(10);
document.add(p);
document.close();
Visual result:

Adding Top level Bookmark to the exsting PDF using PDFBOX

I would like to add a top level bookmark to an existing pdf file using PDFBOX in JAVA.
Not sure why the following code was not working, can anyone help me out? Thanks.
Below is how the Document.pdf looks like in the bookmark section.
Top
---Node-1
-------Node-11
-------Node-12
....
---Node-2
-------Node-21
....
Java code (Part within the program) :
PDDocument document = PDDocument.load(new File("C:/Users/Desktop/document.pdf"))
PDDocumentOutline documentOutline = new PDDocumentOutline();
document.getDocumentCatalog().setDocumentOutline(documentOutline);
PDOutlineItem pagesOutline = new PDOutlineItem();
pagesOutline.setTitle("All Pages");
documentOutline.addFirst(pagesOutline);
pagesOutline.openNode();
documentOutline.openNode();
document.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);
document.save("C:/Users/Desktop/document.pdf");
document.close()
Here's my attempt at this, I'm keeping my filename if asked questions later.
What I did is to wrap the old outline into a new item. It is not possible to add the existing items one by one, because only "orphans" can be added.
PDDocument document = PDDocument.load(new File("000009.pdf"));
PDDocumentOutline oldDocumentOutline = document.getDocumentCatalog().getDocumentOutline();
PDDocumentOutline documentOutline = new PDDocumentOutline();
document.getDocumentCatalog().setDocumentOutline(documentOutline);
PDOutlineItem pagesOutline = new PDOutlineItem();
//pagesOutline.setTitle("All Pages");
//documentOutline.addFirst(pagesOutline);
PDOutlineItem oldOutlineItemWrapped = new PDOutlineItem(oldDocumentOutline.getCOSObject());
oldOutlineItemWrapped.setTitle("All Pages");
documentOutline.addFirst(oldOutlineItemWrapped);
//pagesOutline.openNode();
oldOutlineItemWrapped.openNode();
documentOutline.openNode();
document.getDocumentCatalog().setPageMode(PageMode.USE_OUTLINES);
document.save("000009-modified.pdf");
document.close();

iText7: How to add text to the bottom of last page?

I am new to iText7, and I want to add a text after the content in the last page. I basically got a pdf file and create a new one, copying the pages from the first into the last. After, I get the last page and tried to append some text:
PdfPage lastPage = pdfOut.getLastPage();
PdfCanvas canvas = new PdfCanvas(lastPage.newContentStreamAfter(), lastPage.getResources(), pdfOut);
Paragraph paragraph = new Paragraph().add(new Text("Este documento foi assinado Hoje"));
Canvas c = new Canvas(canvas, pdfOut, rect);
c.add(paragraph);
c.close();
The text is not being appended after the content, sometimes appears in the first line of the last page or even it is not shown. Some ideas?
Thanks
What's definition of rect in the code? Despite this uncertainty below snippet should work
final Rectangle canvasSize = document.getPageEffectiveArea(new PageSize(lastPage.getPageSize()));
Canvas c = new Canvas(canvas, pdfOut, canvasSize);
Paragraph paragraph = new Paragraph()
.add(new Text("Este documento foi assinado Hoje"))
.setHeight(canvasSize.getHeight())
.setVerticalAlignment(VerticalAlignment.BOTTOM);

Align Paragraph at the center of the page

I am using itext to generate pdf file. I want to align my title in the middle of the page. Presently i am using like this
Paragraph preface = new Paragraph();
for (int i = 0; i < 10; i++) {
preface.add(new Paragraph(" "));
}
Is it correct or is there any another best way to do this.
Use Paragraph#setAlignment(int) :
Paragraph preface = new Paragraph();
preface.setAlignment(Element.ALIGN_CENTER);
See the ALIGN_* constants in the Element interface for more possible values.
Not sure if this is an old version, but for PdfWriter these methods weren't there. Instead I used:
Paragraph p = new Paragraph("This too shall pass");
p.Alignment = Element.ALIGN_CENTER;
public static final String DEST = "results/tables/centered_text.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CenteredTextInCell().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph para = new Paragraph("Test", font);
para.setLeading(0, 1);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(50);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.addElement(para);
table.addCell(cell);
document.add(table);
document.close();
}
If you are looking for a solution to Itext7 then you can use the method setTextAlignment(...).
Example:
Paragraph preface = new Paragraph();
// add text
preface.setTextAlignment(TextAlignment.CENTER);
If any one is looking for .NET/C# version, below is how I achieved the CENTER alignment.
I am using iText7 library for .NET/C#, and I achieved this using :
Paragraph preface = new Paragraph();
preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
This is what worked for me (itext 5.0.5 ):
Paragraph p3= new Paragraph("Hello" );
p3.setAlignment(Element.ALIGN_CENTER);
I have searching solution for this to align PdfPCell text into right and also center. After modify and change the code sequence it is work.
This code is not work to align text to center.
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Testing Page");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
After modifying code with this , it is working now.
Paragraph p = new Paragraph("Testing Page");
//Pass Paragraph object into PdfPCell
PdfPCell cell = new PdfPCell(p);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);

Categories