/*
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2008 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: FlattenPDF - Flattens Transparent PDF files
//
// Note: By default, this example program opens a file called TransPDF.pdf in the
//             source directory.   It flattens this file and saves the output in the 
//                             executable directory by the name out.pdf. It demonstrates use of 
//                             PDFlattenerConvertEx2() that allows user to specify (optional) Callback to Monitor 
//                             Flattening Progress. The User also has an Option to Cancel the Operation 
//                             by returning 1 in Callback, if required.
//
// Steps:
// * Initialize HFT with the statement gPDFlattenerHFT = InitPDFlattenerHFT;
// * Initialize FlattenPDF by invoking Plugin API PDFlattenerInitialize()
// * Flatten the input PDF by invoking Plugin API PDFlattenerConvertEx2() 
// * Terminate FlattenPDF by invoking Plugin API PDFlattenerTerminate()
*/

#include <stdio.h>
#include <sstream>
#ifdef UNIX_PLATFORM
#include <string.h>
#endif

//PDFL Headers
#include "PDFInit.h"
#include "ASCalls.h"
#include "PDCalls.h"
#include "PDFLExpT.h"
#include "ASExtraCalls.h"

// Flattener Plugin headers
#include "PDFlattenerCalls.h"

// Sample Headers
#include "MyPDFLibUtils.h"
#ifdef MAC_ENV
#include "MacUtils.h"
#endif

#define INPUT_PATH "../sources/TransPDF.pdf"
#define OUTPUT_PATH "out.pdf"

HFT   gPDFlattenerHFT;

void   FlattenPDFMain( char  * input,  char  * output, FlattenProgressMonitor = 0,  void  * clientData = 0);
void   MainProc();

//Progress Monitor CallBack for Flattener
ASBool   FlattenProgressMonitorCB  (ASInt32  pageNum,  ASInt32  totalPages,  float  current,  ASInt32  reserved,  void  *clientData);

void   MainProc()
{
DURING
                gPDFlattenerHFT  = InitPDFlattenerHFT;

                //initialize flattener plugin
                if (gPDFlattenerHFT   &&     PDFlattenerInitialize())
               {
                                //Flatten PDF using API PDFlattenerConvertEx2() with Callback
                               printf( "\nFlattening using PDFlattenerConvertEx2 (with Callback)\n" );
                                ASBool  progressData =  false ;
                                FlattenPDFMain(INPUT_PATH,  OUTPUT_PATH, 
                                                                                                FlattenProgressMonitorCB,  /* Progress Monitor Callback with Cancel Option */
                                                                                                                                               ( void *)&progressData  /* Client Supplied Data for Callback*/  );

                                //Terminate flattener plugin
                               PDFlattenerTerminate();
               }
                else
                               printf( "Initialization of PDFlatten Plugin failed...\n" );
HANDLER
               printf( "MainProc has thrown an exception...\n" );
                DisplayError(ERRORCODE);
END_HANDLER
}

void   FlattenPDFMain( char  * input,  char  * output, FlattenProgressMonitor  FlattenProgressMonitorCB,  void  * clientData)
{
DURING    
                PDDoc  pddoc=NULL;
        ASPathName  asOutPathName;

#ifdef MAC_ENV
       pddoc =  MyPDDocOpen(input);
#else
                ASFileSys  asFileSys =  ASGetDefaultFileSys();
                ASPathName  asInPathName =  ASFileSysCreatePathName(asFileSys,  ASAtomFromString( "Cstring" ), input,0);
               pddoc =  PDDocOpen(asInPathName, asFileSys,  NULL,  false );
#endif

                ASInt32  firstPage,lastPage;
               firstPage = 0;
               lastPage =  PDDocGetNumPages(pddoc) - 1; 

                //set up parameters for PDFlattenerConvertEx2
               PDFlattenerUserParamsRec flattenUserParams;
               PDFlattenRec flattenParams;
               memset(&flattenParams,0, sizeof  (PDFlattenRec));
               flattenParams.size =  sizeof (PDFlattenRec);
               memset(&flattenUserParams, 0,  sizeof (PDFlattenerUserParamsRec));
               flattenUserParams.size =  sizeof (PDFlattenerUserParamsRec);
               flattenParams.clipComplexRegions =  true ;
               flattenParams.strokeToFill =  true ;
               flattenParams.useTextOutlines =  false ;
               flattenParams.preserveOverprint =  true ;
               flattenUserParams.transQuality = 100.0;
               flattenParams.internalDPI = 1200;
               flattenParams.externalDPI = 300;
               flattenUserParams.colorCompression = kPDFlattenerJpegCompression;
               flattenUserParams.colorImageQuality =   kPDFlattenerMaximum;
               flattenUserParams.flattenParams = &flattenParams;
               flattenUserParams.monoCompression = kPDFlattenerMonoZipCompression;
               flattenUserParams.profileDesc =  NULL
               flattenUserParams.flattenProgress = FlattenProgressMonitorCB;
               flattenUserParams.progressClientData = FlattenProgressMonitorCB ? ( void  *)clientData :  NULL;

                // flatten the transparent pdf
                ASInt32  res;
                ASUns32  numFlattenedPages;
               
               res = PDFlattenerConvertEx2(pddoc, firstPage, lastPage, &numFlattenedPages, &flattenUserParams);

                if (res)
               {
                               printf( "File %s has been successfully flattened.\n" , input);
#ifdef MAC_ENV
               asOutPathName =  GetMacPath(output);
                PDDocSave  (pddoc,  PDSaveFull  |  PDSaveCollectGarbage, asOutPathName, 0,  NULLNULL);
#else
               asOutPathName =  ASFileSysCreatePathName(NULLASAtomFromString( "Cstring" ), output, 0);
                PDDocSave(pddoc,  PDSaveFull  |  PDSaveCollectGarbage  , asOutPathName,asFileSys,  NULLNULL);
#endif
               printf ( "Flattened file has been successfully saved to %s.\n" ,output);
                if  (asOutPathName)
                                ASFileSysReleasePathNULL, asOutPathName);
               }
                else
                               printf( "Flattening of file %s has failed...\n" , input);

                //clean up
                PDDocClose(pddoc);
#ifndef MAC_ENV
                if  (asInPathName)
                                ASFileSysReleasePathNULL, asInPathName);
#endif
HANDLER
                DisplayError(ERRORCODE);
END_HANDLER
}

ASBool   FlattenProgressMonitorCB  (ASInt32  pageNum,  ASInt32  totalPages,  float  current,  ASInt32  reserved,  void  *clientData)
{
                if  ( clientData ) 
               {
                                ASBool  * IsMonitorCalled = (ASBool*)clientData;
                                if  ( *IsMonitorCalled ==  false  )
                               {
                                               printf( "Flattening Progress Monitor CallBack\n" );
                                                //Set to true to Display this Message Only Once
                                               *IsMonitorCalled =  true ;
                               }
               }

               printf ( "Flattening Page %d of %d. Overall Progress = %f %%. \n" , 
                                                               pageNum + 1,  /* Adding 1, since Page numbers are 0-indexed*/  
                                                                               totalPages,   
                                                                                               current  /* Current Overall Progress */  );

                //Return 1 to Cancel Flattening
                return  0;
}

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