Jump to content

Adding Marks on a PDF


ziden

Recommended Posts

Hello there.

 

 Is it possible to make iText open a previously made PDF by ireport, and add marks on it ?

 Im not sure how would be done, heres what ive got:

 

Paragraph cTitle = new Paragraph("This is chapter " + i, chapterFont);

writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);

Chapter chapter = new Chapter(cTitle, 0);

section.add(Chunk.NEXTPAGE);

document.add(chapter);

 

is this correct ? how could i open a PDF with some anchors and link them to marks so people who open the pdf can navigate easyly ?

 

Thanx for any attention.

 

 

 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

 Just posting some improvements.

 

Im using the following code to generate an empty pdf for example with that bookmark menu

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document,

        new FileOutputStream("C:/Menu.pdf"));

        document.open();

        document.add(new Chunk("Chapter 1")

        .setLocalDestination("chappy1"));

        PdfContentByte cb = writer.getDirectContent();

        PdfOutline root = cb.getRootOutline();

        PdfOutline oline1 = new PdfOutline(root,

        PdfAction.gotoLocalPage("chappy1", false),"Go to Chapter 1");

    So, this is kinda simple: i can create an anchor and link the bookmark to the anchor on a new PDF. What i need is, to do this onto an existing PDF with previously anchors created. Ireport allows me to create anchors with the <anchorNameExpression> tag, all i would need to do is open that pdf and add the bookmark menu to it

but it seems i cannot do that, at least not as im tryng to do. I belive this would be usefull to many of you as well as it would be to me.

    Does anyone have any idea on how to do that ? My last tryout was doing a Menu.pdf and a Main.pdf, one with anchors other with bookmarks, then i tryed to concat em both using this code:

  public static void concatenarPdfs(List<InputStream> pdfs, FileOutputStream out) throws Throwable {

        PdfCopyFields copy = new PdfCopyFields(out);

        for(int x  = 0; x < pdfs.size() ; x++) {

            //byte [] temp = pdfs.get(x)

            byte[] bytes = new byte[pdfs.get(x).available()]; // cria vetor de bytes do tamanho dos bytes do pdf

            pdfs.get(x).read(bytes); // passa os bytes pro vetor

            PdfReader reader = new PdfReader(bytes); // cria um pdfreader com os bytes do pdf lido

            copy.addDocument(reader); // junta os 2

        }

        copy.close();

    }

 

  It concatenates both pdfs nicely, but it doesnt keep the bookmarks :(

 

  Does anyone have any ideas on how this could be possible ?

 

  Thanx for any attention, i would apreciate it.

 

Link to comment
Share on other sites

 Just posting to show progress.

 

 Ive managed to make the whole PDF with the bookmark menu, however i couldnt anchor it correctly. How could i ?

 

 Heres the code i have so far:

 

  PdfReader reader = new PdfReader(new FileInputStream("C:/Curso.pdf"));

         

        Document document = new Document();

        

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Menu.pdf"));

        

        document.open();

        

        PdfContentByte cb = writer.getDirectContent();


        // Load existing PDF

       

        int paginas = reader.getNumberOfPages();

        

        for(int pag = 1 ; pag <= paginas ; pag ++) {

            PdfImportedPage page = writer.getImportedPage(reader, pag);  

            document.newPage();

            cb.addTemplate(page, 0, 0);

        }

 PdfOutline root = cb.getRootOutline();

        

        // Code 3

        PdfOutline oline1 = new PdfOutline(root,

        PdfAction.gotoLocalPage("prof", false),"Profissional");

        

        PdfOutline oline2 = new PdfOutline(root,

        PdfAction.gotoLocalPage("reconhecimento", false),"Reconhecimento");

        oline2.setOpen(false);

        PdfOutline oline2_1 = new PdfOutline(oline2,

        PdfAction.gotoLocalPage("2.1", false),"Sub 2.1");

        PdfOutline oline2_2 = new PdfOutline(oline2,

        PdfAction.gotoLocalPage("2.2", false),"Sub 2.2");

        

        PdfOutline oline3 = new PdfOutline(root,

        PdfAction.gotoLocalPage("opcaolingua", false),"Opção Língua");

        

        document.close();

 

 

 At my iReport, i added some anchor tags such as 

 <anchorNameExpression>"prof"<\anchorNameExpression>

but when i click the following bookmark

 PdfOutline oline1 = new PdfOutline(root,

        PdfAction.gotoLocalPage("prof", false),"Profissional");

it doesnt redirects me into the selected anchor. Is there something i am doing wrong ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...