Hi Larry,

Sometime ago I tried to extend podofomerge almost exactly like you did.

I attached my version to this email. The only problem I see in your version is 
that you never delete "input2" and leak quite a large amount of memory.
Allocating the PdfMemDocument on the stack should fix this issue.

Well, I did not try your code, but I just checked my version of podofomerge 
again. It has several problems, too:
* Outlines are not merged correctly
* Named destinations are not merged
* Most importantly: 
        The pagestree inserts pages in the wrong order! 

So no pages are lost in my case, but they are in the wrong oder.
The pagestree implementation would need some fixes in this case.

So, sorry that I can't help you right now with what exactly does not work in 
the pagestree, but at least it is not your code.

best regards,
        Dom


Am Sonntag, 28. Dezember 2008 schrieb Larry Doolittle:
> Hi -
>
> After some initially successful hello-world type experimentation,
> I ran into a problem with Append.  I tried to extend podofomerge
> to handle a list of files (not limited to 2), and it doesn't work.
> That is, the program runs to completion, and generates an output
> file, but that output file looks corrupt.
>
> Could someone here (who is not such a complete PoDoFo novice
> like I am) try the attached code, and try to figure out where
> the problem is?
>
> I'll certainly keep investigating here, but I'm still somewhat
> in the dark about how this stuff is supposed to work.
>
> I am using svn trunk as downloaded a couple of days ago.
>
>    - Larry



-- 
**********************************************************************
Dominik Seichter - [email protected]
KRename  - http://www.krename.net  - Powerful batch renamer for KDE
KBarcode - http://www.kbarcode.net - Barcode and label printing
PoDoFo - http://podofo.sf.net - PDF generation and parsing library
SchafKopf - http://schafkopf.berlios.de - Schafkopf, a card game,  for KDE
Alan - http://alan.sf.net - A Turing Machine in Java
**********************************************************************
/***************************************************************************
 *   Copyright (C) 2006 by Dominik Seichter                                *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <PdfDefines.h>

#include <PdfDestination.h>
#include <PdfMemDocument.h>
#include <PdfOutlines.h>

#include <stdlib.h>

using namespace PoDoFo;

#ifdef _HAVE_CONFIG
#include <config.h>
#endif // _HAVE_CONFIG

void print_help()
{
  printf("Usage: podofomerge [inputfile1] [inputfile2] [inputfile ...] [outputfile]\n\n");
}

void merge( const char* pszInput1, const char* pszInput2, const char* pszOutput )
{
    printf("Reading file: %s\n", pszInput1 );
    PdfMemDocument input1( pszInput1 );
    printf("Reading file: %s\n", pszInput2 );
    PdfMemDocument input2( pszInput2 );

// #define TEST_ONLY_SOME_PAGES
#ifdef TEST_ONLY_SOME_PAGES
    input1.InsertPages( input2, 1, 2 );
#else
    printf("Appending %i pages on a document with %i pages.\n", input2.GetPageCount(), input1.GetPageCount() );
    input1.Append( input2 );
#endif

    // we are going to bookmark the insertions
    // using destinations - also adding each as a NamedDest
    /*
      PdfDestination	p1Dest( input1.GetPage(0) );
    input1.AddNamedDestination( p1Dest, std::string("Input1") );
    PdfOutlines* bMarks = input1.GetOutlines();
    PdfOutlineItem*	bmRoot = bMarks->CreateRoot( "Merged Document" );
    PdfOutlineItem* child1 = bmRoot->CreateChild( pszInput1, p1Dest );
    PdfDestination	p2Dest( input1.GetPage(pgCount) );
    input1.AddNamedDestination( p2Dest, std::string("Input2") );
    child1->CreateNext( pszInput2, p2Dest );
    */
    
#ifdef TEST_FULL_SCREEN
    input1.SetUseFullScreen();
#else
    input1.SetPageMode( ePdfPageModeUseBookmarks );
    input1.SetHideToolbar();
    input1.SetPageLayout( ePdfPageLayoutTwoColumnLeft );
#endif

    printf("Writing file: %s\n", pszOutput );
    input1.Write( pszOutput );
}

int main( int argc, char* argv[] )
{
  char*   pszInput1;
  char*   pszInput2;
  char*   pszOutput;

  PdfError::EnableLogging( false );
  PdfError::EnableDebug( false );

  if( argc < 4 )
  {
    print_help();
    exit( -1 );
  }
  else if( argc == 4 )
  {
      pszInput1 = argv[1];
      pszInput2 = argv[2];
      pszOutput = argv[3];

      merge( pszInput1, pszInput2, pszOutput );
  
      try {
          //merge( pszInput1, pszInput2, pszOutput );
      } catch( PdfError & e ) {
          fprintf( stderr, "Error %i occurred!\n", e.GetError() );
          e.PrintErrorMsg();
          return e.GetError();
      }
  }
  else
  {
      const int nBufferLen = 512;
      pszInput1 = argv[1];
      char temp[nBufferLen];
      char input[nBufferLen];

      // merge multiple files
      for( int i=2;i<argc-2; i++ )
      {
          strncpy( temp, "/tmp/podofoXXXXXX", nBufferLen );
          mkstemp( temp );
          pszOutput = temp;
          pszInput2 = argv[i];

          try {
              merge( pszInput1, pszInput2, pszOutput );
          } catch( PdfError & e ) {
              unlink( pszOutput );
              fprintf( stderr, "Error %i occurred!\n", e.GetError() );
              e.PrintErrorMsg();
              return e.GetError();
          }

          // delete all temp file
          if( i != 2 )
              unlink( input );

          strncpy( input, temp, nBufferLen );
          pszInput1 = input;          
      }

      pszInput2 = argv[argc-2];
      pszOutput = argv[argc-1];

      try {
          merge( pszInput1, pszInput2, pszOutput );
      } catch( PdfError & e ) {
          unlink( pszOutput );
          fprintf( stderr, "Error %i occurred!\n", e.GetError() );
          e.PrintErrorMsg();
          return e.GetError();
      }
  }

  return 0;
}

Attachment: signature.asc
Description: This is a digitally signed message part.

------------------------------------------------------------------------------
_______________________________________________
Podofo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to