java


How to create a email link in POI Word Format


How can I create an external link or an email link in a XWPFDocument? There is a description for Excel (HSSF XSSF), but i haven't found anything similar for Word (HWPF XWPF).
public void example() throws Exception{
XWPFDocument document = new XWPFDocument();
//Append a link to
appendExternalHyperlink("https://poi.apache.org", " Link to POI", document.createParagraph());
document.write(new FileOutputStream("resultat.docx"));
}
/**
* Appends an external hyperlink to the paragraph.
*
* #param url The URL to the external target
* #param text The linked text
* #param paragraph the paragraph the link will be appended to.
*/
public static void appendExternalHyperlink(String url, String text, XWPFParagraph paragraph){
//Add the link as External relationship
String id=paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId();
//Append the link and bind it to the relationship
CTHyperlink cLink=paragraph.getCTP().addNewHyperlink();
cLink.setId(id);
//Create the linked text
CTText ctText=CTText.Factory.newInstance();
ctText.setStringValue(text);
CTR ctr=CTR.Factory.newInstance();
ctr.setTArray(new CTText[]{ctText});
//Insert the linked text into the link
cLink.setRArray(new CTR[]{ctr});
}
At the moment, XWPF has support for reading and manipulating hyperlinks, see XWPFHyperLinkRun and XWPFHyperlink for details.
There's not currently any user facing code to handle creating hyperlinks in XWPF, but all the components are there (handling of the low level hyperlink objects, ability to add hyperlinks into the relations etc). A patch to tie this together to provide the missing functionality would be very much appreciated by all!
I'm afraid that Apache POI isn't near as far in the handling of Word files than it is in handling Excel documents. If you are in the early stage of development maybe you could consider moving to Docx4j.
Cheers,
Wim
All,
The above example shows how to create an external hyperlink. For those who need to create an internal hyperlink, see below code:
XWPFParagraph hyperPara = document.createParagraph();
hyperPara.setAlignment(ParagraphAlignment.CENTER);
addHyperlink(hyperPara, "Hyperlink Text", "Heading Text");
private static void addHyperlink(XWPFParagraph para, String text, String bookmark) {
//Create hyperlink in paragraph
CTHyperlink cLink=para.getCTP().addNewHyperlink();
cLink.setAnchor(bookmark);
//Create the linked text
CTText ctText=CTText.Factory.newInstance();
ctText.setStringValue(text);
CTR ctr=CTR.Factory.newInstance();
ctr.setTArray(new CTText[]{ctText});
//Create the formatting
CTFonts fonts = CTFonts.Factory.newInstance();
fonts.setAscii("Calibri Light" );
CTRPr rpr = ctr.addNewRPr();
CTColor colour = CTColor.Factory.newInstance();
colour.setVal("0000FF");
rpr.setColor(colour);
CTRPr rpr1 = ctr.addNewRPr();
rpr1.addNewU().setVal(STUnderline.SINGLE);
//Insert the linked text into the link
cLink.setRArray(new CTR[]{ctr});
}

Related Links

Do brackets (parentheses) change the order of methods?
Android Studio: NavigationDrawer displaying a black screen
Reverse single linked list
Cache Control not Working in Firefox
Location Based Android Development
“Incoming Call” Activity may not be killed on Home Press
What is Android's SharedPreferences equivalent in Java?
how to mark avro field deprecated in JSON/avsc?
How do I keep my Android app running when it doesn't have focus?
Any easier way to construct dynamic deep nested json objects in Java
Change 6.51 to 6.50 in java BigDecimal
Auto delete database rows after an expiration date
I try to use the method repaint after paintComponent but its not work
Defining an equation outside of function
TransformException: java.util.zip.ZipException: duplicate entry: com/google/gson/JsonPrimitive.class
When given a pair of ints, I need my code to be able to return the int that is closest to 21 without going over [closed]

Categories

HOME
xamarin
osgi
netbeans
stock
fft
platform-builder
xmpp
at-command
razor
indesign
iggrid
python-unittest
modelica
google-apps-marketplace
moonmail
zebra-printers
pc
serilog
text-rendering
samoa
dbext
swingx
immutable.js
autocad-plugin
mustache.php
format-specifiers
java-7
android-widget
h2db
windows-server-2000
quote
delicious-api
sql-server-2012-express
frame
language-concepts
force-layout
wpf-controls
serverside-rendering
objectlistview
temporary-files
mapzen
nand2tetris
fakeiteasy
qcombobox
angular-resource
user-accounts
filepicker
jedis
ionicons
hls.js
nsarray
python-cryptography
pdfclown
sqldf
specrun
spring-android
passport-google-oauth
mongocsharpdriver
hexdump
nessus
oauth2client
asp.net5
ford-fulkerson
pervasive-sql
tcpserver
atk4
coveralls
event-bubbling
generic-programming
cisco-ios
graph-api-explorer
jsapi
lustre
qcodo
fluentautomation
bundles
resty-gwt
meteor-velocity
cdc
rabl
random-seed
interface-orientation
broken-links
appconkit
nsmanagedobject
assembly-loading
bigcouch
mercurial-server
mod-auth
appendto
celltable
datareader
subviews
gamma
vc90
adrotator
pascal-fc
w3wp
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App