i've run pretty strange problem in itext 7 i'm hoping other people have dealt in past. i'm trying create table of contents adding series of link objects paragraph object, dropping paragraph canvas object. here's sample of simplified code:
pdfcanvas pdfcanvas = new pdfcanvas(document.getpdfdocument().addnewpage()); rectangle rectangle = new rectangle(36, 650, 100, 100); pdfcanvas.rectangle(rectangle); pdfcanvas.stroke(); canvas canvas = new canvas(pdfcanvas, document.getpdfdocument(), rectangle); canvas.add(new paragraph(new link("google", pdfaction.creategotor("hello", "www.google.com"))));
as can see, pretty bare bones. when this, however, getting null pointer exception. can add simple text without having problems, moment add link, things go haywire. appreciated.
this how draw rectangle (or in case, square) on pdfcanvas
:
pdfdocument pdf = new pdfdocument(new pdfwriter(dest)); pdfpage page = pdf.addnewpage(); pdfcanvas pdfcanvas = new pdfcanvas(page); rectangle rectangle = new rectangle(36, 650, 100, 100); pdfcanvas.rectangle(rectangle); pdfcanvas.stroke(); pdf.close();
you don't need document
object. create pdfcanvas
, draw rectangular shape bottom-left corner x = 36; y = 360
measuring 100 100 user units.
you introduce document
object, because want create link
. isn't necessary either. try (but wrong):
canvas canvas = new canvas(pdfcanvas, pdf, rectangle); canvas.add(new paragraph().add("google")); canvas.add(new paragraph(new link("google", pdfaction.creategotor("hello", "www.google.com")))); pdf.close();
as can see, work pdf
(a pdfdocument
) , pdf
alone. there's no document
involved. however, trying add link canvas
object. canvas
can used if want add pdf syntax content stream. link isn't part of content stream. link annotation stored in /annots
entry of page dictionary. in short: using canvas
can't used.
i think trying put link @ absolute position , want put rectangle around link. that's not done using canvas
. it's easier if put paragraph
@ absolute position.
for instance:
public void createpdf(string dest) throws ioexception { pdfdocument pdf = new pdfdocument(new pdfwriter(dest)); document document = new document(pdf); paragraph p = new paragraph() .add(new link("google", pdfaction.creategotor("hello", "www.google.com"))) .setfixedposition(36, 650, 80) .setborder(new solidborder(0.5f)); document.add(p); document.close(); }
this add paragraph
@ position (x = 36; y = 650)
, width of 80 user units. add 0.5 thick border around paragraph
.
this won't work either, because link wrong. using gotor
(go remote) action meant go specific destination in pdf file. think want uri action instead:
public void createpdf(string dest) throws ioexception { pdfdocument pdf = new pdfdocument(new pdfwriter(dest)); document document = new document(pdf); paragraph p = new paragraph() .add(new link("google", pdfaction.createuri("www.google.com"))) .setfixedposition(36, 650, 80) .setborder(new solidborder(0.5f)); document.add(p); document.close(); }
if want more space around text, can change padding:
public void createpdf(string dest) throws ioexception { pdfdocument pdf = new pdfdocument(new pdfwriter(dest)); document document = new document(pdf); paragraph p = new paragraph() .add(new link("google", pdfaction.createuri("www.google.com"))) .setfixedposition(36, 650, 80) .setpadding(10) .setborder(new solidborder(0.5f)); document.add(p); document.close(); }
this more intuitive tried achieve.
Comments
Post a Comment