Java create link to page in PDF with PDFBox - java

My program links a position in a PDF-file to another page in the same file. So you can click on a defined position in the file an you'll be linked to another page.
I use a PDRectangle to define the position. Unfortunately the rectangle is visible in the document. I want to create the link without a visible border.
My code:
PDActionGoTo action = new PDActionGoTo();
action.setDestination(destination);
PDAnnotationLink annotationLink = new PDAnnotationLink();
annotationLink.setAction(action);
PDRectangle position = new PDRectangle();
position.setLowerLeftX(bookmarkLinkPositionEntry.getLowerLeftX());
position.setLowerLeftY(bookmarkLinkPositionEntry.getLowerLeftY());
position.setUpperRightX(bookmarkLinkPositionEntry.getUpperRightX());
position.setUpperRightY(bookmarkLinkPositionEntry.getUpperRightY());
annotationLink.setRectangle(position);
destinationPDF.getPage(0).getAnnotations().add(annotationLink);
I tried to use annotationLink.setHidden(true); and annotationLink.setNoView(true);. The documentation just says "Set the hidden flag." and "Set the noView flag." and I don't know what actually happened there.
How can I change the visibility of my rectangle or remove the border completely?

You'll need to set the border style:
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
borderULine.setWidth(0);
annotationLink.setBorderStyle(borderULine);
More on this topic in the AddAnnotations.java example in the source code download.

Related

itext7 - How to copy page as form XObject while keeping hidden OCGs hidden

I am using PdfFormXObject pageCopy = sourcePage.CopyAsFormXObject(pdf); to then insert pageCopy into a new PDF page using pdfCanvas.AddXObjectFittedIntoRectangle. The copied page is visible in the new PDF as expected, but it how has it's 'hidden' OCGs visible.
The reason I am doing this is to be able to take a PDF page, scale and crop it and add it to a new PDF where it may be collated with other contents.
Is there a way to remove OCG PDF content prior to create the XObject, or is there a different way of achieving my goal without using the XObject route that allows me to maintain the 'off' status of hidden OCGs
OCG removal functionality is not yet available in iText 7.
There is, however, a workaround that you can try to apply: we can copy all the information about OCGs from your source document to the target document which should create the same OCGs in the target document and preserve default on/off states.
To copy the OCGs, you can copy a page from one document to another one (which is going to copy all the OCGs) and then remove that page.
When the OCG removal functionality becomes available in iText the approach would become cleaner but for now you can use the code similar to the following:
PdfDocument sourceDocument = new PdfDocument(new PdfReader(sourcePdfPath));
PdfDocument targetDocument = new PdfDocument(new PdfWriter(targetPdfPath));
PdfFormXObject pageCopy = sourceDocument.getFirstPage().copyAsFormXObject(targetDocument);
PdfPage page = targetDocument.addNewPage();
PdfCanvas canvas = new PdfCanvas(page);
canvas.addXObject(pageCopy);
// Workaround: copying the page from source document to destination document also copies OCGs
sourceDocument.copyPagesTo(1, 1, targetDocument);
// Workaround: remove the page that we only copied to make sure OCGs are copied
targetDocument.removePage(targetDocument.getNumberOfPages());
sourceDocument.close();
targetDocument.close();

How to remove hyperlink style using PDFBox

I was able to disable hyperlink using
PDActionHide actionshow = new PDActionHide();
actionshow.setH(true);
However I want to remove blue colour hyperlink but keep the text. How do I do this using Apache PDFBox?
Do this (works only if the line is part of the annotation and not of the content stream):
PDAnnotationLink txtLink = ...
txtLink.setBorderStyle(null);
txtLink.setBorder(new COSArray());
this removes the border style and sets an empty color.

Add hyperlink to pdf files using pdfbox

I need to create a small tool that adds a hyperlink on the first page of a PDF file. I'm using Apache PDFBox to read the pdf files.
Any ideas how to add a hyperlink on a page using this library?
I found this question: how to set hyperlink in content using pdfbox, but this doesn't work.
I just want to add a hyperlink on the first page of a pdf file.
File file = new File(filename);
PDDocument doc = PDDocument.load(file);
PDPage page = doc.getPage(0);
...
I have at least 2 problems with the solution that I found on this question:
The method drawString(String) in the type PDPageContentStream is not applicable for the arguments (PDAnnotationLink)
colourBlue is not initialized
I would prefer to add the hyperlink at the bottom of the page with the URL centered. But for the moment any suggestion will help
First of all, you need to create a PDAnnotationLink like this:
PDAnnotationLink link = new PDAnnotationLink();
The link should have an action:
PDActionURI actionURI = new PDActionURI();
actionUri.setURI("http://www.Google.com");
link.setAction(action);
Finally, you need to define a rectangle at the desired position and finally add the link to the page's annotations.
PDRectangle pdRectangle = new PDRectangle();
pdRectangle.setLowerLeftX(...);
pdRectangle.setLowerLeftY(...);
pdRectangle.setUpperRightX(...);
pdRectangle.setUpperRightY(...);
link.setRectangle(pdRectangle);
page.getAnnotations().add(link);
If you want you can also add an underline for the link by calling the setBorderStyle(...) methid.
Hope this works for you !
If you want to add some text, then you need to create a PDPageContentStream like this:
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.newLineAtOffset(..., ...);
contentStream.showText(...);
contentStream.endText();
contentStream.close();
The newLineAtOffset(..., ...) method is used for positioning the text at the desired location.
P.S. Sorry for the bad indentation but it is pretty hard to write on the mobile. If you need any further help you can even write me a private message in Romanian.

How do numbering of page in word file by JAVA

How do i numbering of pages in word file by Java.
I am using Apache POI driver to interact JAVA and word .
i want border and page number as well in my word file while i am creating file from JAVA.
Please help.
The question marked as a duplicate has a complex answer to a relatively simple question.
The simple answer (for page number) is very similar to this answer: https://stackoverflow.com/a/40264237/2296441. The difference is just which field to insert. The afore mentioned answer shows how to insert a TOC field. In your case you want a PAGE field.
XWPFParagraph p;
...
// get or create your paragraph
....
CTP ctP = p.getCTP();
CTSimpleField page = ctP.addNewFldSimple();
page.setInstr("PAGE");
page.setDirty(STOnOff.TRUE);
Note: setDirty tells Word to update the field which causes a dialog to be opened when the document is opened. This dialog is MS Word making sure you want to update the field. I don't think you can disable the dialog and still have the field calculated on open.
To set page borders you are once again going to have to break into the CT classes. In this case the appropriate location in the document is the section properties. Here is how to set a double line border around the whole page set back 24 points from the page edge.
// Page Borders
CTDocument1 ctDoc = doc.getDocument();
CTBody ctBody = ctDoc.getBody();
CTSectPr ctSectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr();
CTPageBorders ctPgBorders = ctSectPr.isSetPgBorders() ? ctSectPr.getPgBorders() : ctSectPr.addNewPgBorders();
ctPgBorders.setOffsetFrom(STPageBorderOffset.PAGE);
CTBorder ctBorder = CTBorder.Factory.newInstance();
ctBorder.setVal(STBorder.DOUBLE);
ctBorder.setSpace(new BigInteger("24"));
ctPgBorders.setTop(ctBorder);
ctPgBorders.setBottom(ctBorder);
ctPgBorders.setRight(ctBorder);
ctPgBorders.setLeft(ctBorder);
Disclaimer
The MS-Word functionality in POI is still largely unfinished, and subject to change.

Creating PDF using JAVA (Netbeans) with images and multi pages

I am developing a Java program with the following requirements:
The application will take 5 input fields and 3 images (browse and "attach" to the Java application).
Once the "form" is completed it will be submitted using a button called "submit".
Once submitted the JAVA application will create a PDF file with the 5 inputed text and the 3 attached images.
I should be able to control which goes to which page number.
How do I implement such a solution with iText?
The application will take 5 input fields and 3 images (browse and "attach" to the Java application).
Once the "form" is completed it will be submitted using a button called "submit".
These first two requirements are unclear; are they to be implemented in a Java GUI (AWT? Swing? FX?), in some independent web UI (Plain HTML? Vaadin?), or in some derived UI (Portlet? ...)?
But as the question title "Creating PDF using JAVA (Netbeans) with images and multi pages" focuses on the PDF creation, let's look at the third and fourth requirements.
Once submitted the JAVA application will create a PDF file with the 5 inputed text and the 3 attached images.
I should be able to control which goes to which page number.
Let's assume you already have those inputs in the variables
String text1, text2, text3, text4, text5;
byte[] image1, image2, image3;
The framework
With iText you now create the document like this:
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
...
// where you want to create the PDF;
// use a FileOutputStream for creating the PDF in the file system
// use a ByteArrayOutputStream for creating the PDF in a byte[] in memory
OutputStream output = ...;
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
// Add content for the first page(s)
...
// Start e new page
document.newPage();
// Add content for the next page(s)
...
// Start a new page
document.newPage();
// etc etc
document.close();
Adding text
You can add text in one of the Add content for the ... page(s) sections using
import com.itextpdf.text.Paragraph;
...
document.add(new Paragraph(text1));
Adding an image
You can add an image in one of the Add content for the ... page(s) sections using
import com.itextpdf.text.Image;
...
document.add(Image.getInstance(image1));
Adding at a given position
Adding text or images as described above leaves the layout details to iText, and iText fills the page from top to bottom except some margins.
If you want to control the positioning of the content yourself (which also means you have to take care that the content parts do not overlap or are drawn outside the page area), you can do so like this:
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.Phrase;
...
PdfContentByte canvas = writer.getDirectContent();
Phrase phrase = new Phrase(text2);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 200, 572, 0);
Image img = Image.getInstance(image2);
img.setAbsolutePosition(200, 200);
canvas.addImage(img);
And there are many more options how to manipulate your content, e.g. choosing a font, choosing text sizes, scaling images, rotating content, ..., simply have a look at the iText samples from the book iText in Action - Second Edition.
You can use XSL-FO. A basic example here. After this, you can search and add other options for your PDF.

Categories

Resources