/*
//
//   ADOBE SYSTEMS INCORPORATED
//   Copyright (C) 2000-2003 Adobe Systems Incorporated
//   All rights reserved.
//
//   NOTICE: Adobe permits you to use, modify, and distribute this file
//   in accordance with the terms of the Adobe license agreement
//   accompanying it. If you have received this file from a source other
//   than Adobe, then your use, modification, or distribution of it
//   requires the prior written permission of Adobe.
//
*/
/*
// 
// Project: Mergepdf - merging two pdf files
// 
// Note: By default, this example program opens two files called mergepdf1.PDF
//               mergepdf2.pdf in the source directory. It merges these two files and output
//               the resulting file to "out.pdf" under the working directory.
// 
// Steps: font
// 
// * Open two PDF documents
// * Insert pdDoc2 to pdDoc1
// * Save pdDoc1 as out.pdf
*/

#ifdef MAC_ENV
#include <Carbon/Carbon.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "PDCalls.h"
#include "PERCalls.h"
#include "PEWCalls.h"
#include "MyPDFLibUtils.h"

/**************************************************************************
               Please use the appropriate relative path for the input file if you want
               to run the application from the final folder. If you want to run the
               application from the MS IDE or the Mac default build directory, 
               use the default paths below.
**************************************************************************/

#define PDF_FNAME1   "../sources/mergepdf1.pdf"
#define PDF_FNAME2   "../sources/mergepdf2.pdf"

#include "PagePDECntCalls.h"

#ifdef MAC_ENV
#include "MacUtils.h"
#endif


void   MainProc( void );
void   MainProc( void )
{
                PDDoc  pdDoc1 =  NULL, pdDoc2 =  NULL;          /* reference to a PDF document   */
                ASPathName  outPath =  NULL;
                ASErrorCode  errCode = 0;

                /*==================================================================*\
                                                                                                                                   Open Doc
               \*==================================================================*/

                DURING
                               pdDoc1 =  MyPDDocOpen(PDF_FNAME1);
                                if  (!pdDoc1)
                               {
                                               fprintf(stdout, "Unable to open file %s \n" ,PDF_FNAME1);
                               }
                                else
                               {
                                               pdDoc2 =  MyPDDocOpen(PDF_FNAME2);
                                                if  (!pdDoc2)
                                               {
                                                               fprintf(stdout, "Unable to open file %s \n" ,PDF_FNAME2);
                                               }
                                                else
                                               {
                                                                /*==================================================================*\
                                                                               Insert pdDoc2 to the end of pdDoc1 and save PdDoc1 as out.pdf
                                                               \*==================================================================*/
                                                               
                                                                PDDocInsertPages  (pdDoc1,PDLastPage,pdDoc2, 0,  PDAllPages,PDInsertAllNULL,NULL,NULL,NULL);

                                                                /* save document to a file */
#if !MAC_ENV
                                                               outPath =  ASFileSysCreatePathFromDIPathNULL,  "out.pdf" ,  NULL  );
#else
                                                               outPath =  GetMacPath( "out.pdf" );
#endif
                                                                PDDocSave(pdDoc1,  PDSaveFull  |  PDSaveLinearized, outPath,  ASGetDefaultFileSys(),  NULLNULL);
                                               }
                               }
                HANDLER
                               errCode =  ERRORCODE;
                END_HANDLER
               
                if (errCode)
                                DisplayError(errCode);

                if (outPath)          ASFileSysReleasePathNULL, outPath);
                if (pdDoc1)            PDDocRelease(pdDoc1);
                if (pdDoc2)            PDDocRelease(pdDoc2);
}


#define INCLUDE_MYPDFLIBAPP_CPP 1
#include "MyPDFLibApp.cpp"
#undef INCLUDE_MYPDFLIBAPP_CPP