Problem writting several pdf files

Help
derlud
2013-04-15
2013-05-28
  • derlud
    derlud
    2013-04-15

    Hello,
    First of all, thank you for your very useful library.
    I'm having trouble when i try to make a batch which write several files.
    I explain my problem :
    In my loop i create a PDDocument using PDDocument.createNew() method.
    Then i call a method which writes some content in the PDDocument given in argument. This method creates a PDPage using PDPage.meta.createNew(), then adds the page to the PDDocument using addPageNode() method.
    The problem is second time i call the method, I've got an exception "java.lang.IllegalStateException: You can not merge objects from different documents" but the PDDocument is reallocated and previous one is saved to a file and closed so i don't know how to make it work. It seems like PDPage.meta.createNew() gives me back the previous page.
    Thank you in advance for your help.

     
  • derlud
    derlud
    2013-04-15

    Thanks for your reply. I will look at your examples.
    The problem only occurs when i use a creator, i've tried to create two documents and add pages without content and no problem encountered.
    Here is a very simple example producing the problem :

    PDDocument doc1 = PDDocument.createNew();
    PDPage page1 = (PDPage)PDPage.META.createNew();
    CSCreator creator = CSCreator.createNew(page1);
    creator.textSetFont(null, font, fontSize);
    doc1.addPageNode(page1);
    creator.textShow("test");
    creator.flush();
    creator.close();
    doc1.save(new FileLocator("D:/test.pdf"));
    doc1.close();
    PDDocument doc2 = PDDocument.createNew();
    PDPage page2 = (PDPage)PDPage.META.createNew();
    CSCreator creator2 = CSCreator.createNew(page2);
    creator2.textSetFont(null, font, fontSize);
    doc2.addPageNode(page2);
    creator2.textShow("test");
    creator2.flush();
    creator2.close();
    doc2.save(new FileLocator("D:/test2.pdf"));
    doc2.close();
    

    Thanks again.

     
  • derlud
    derlud
    2013-04-15

    Thanks to you, i understood what was my problem. I was using the same PDFont object in both document because i thought it was not really part of the document.
    Thanks a lot !

     
  • derlud
    derlud
    2013-04-15

    Sorry again for such a newbie question but could you explain me how to make pdf documents in landscape format please ?
    I've seen a setRotation method on pages but it also rotates the content of the pages.
    Thanks in advance.

     
  • Waldemar Dick
    Waldemar Dick
    2013-04-16

    Hi,

    I was using the same PDFont object in both document because i thought it was not really part of the document./quote]

    If you want to reuse an object, you could try to copy it. For example:

      PDFont font = ...
      PDFont fontCopy = PDFont.META.createFromCos( font.cosGetObject().copyDeep() );
    

    how to make pdf documents in landscape format please

    To create a page in landscape format, just set the media box accordingly.

    PDPage page = ...
    
    // A4 page in landscape 
    float width = 840.0f; // user space units
    float height = 595.0f; // user space units
    CDSRectangle a4PageLandscape = new CDSRectangle(0, 0, width, height);
    page.setMediaBox(a4PageLandscape);
    

    See the CDSRectangle class for an example how to calculate user space units from the cm or inch size.

     
  • derlud
    derlud
    2013-04-16

    Thank you, i had already solved my problem with PDFont (it was static).
    A4 in landscape is exactly what i need. I will try it, thank you so much.