Thread: Split PDF with GS and a little addon

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    11

    Split PDF with GS and a little addon

    Hello together
    Please i have here a little toy to learn and for "possible" usage.
    the goal are that i need to split me pdf document to single sides.

    with the Appl GS, i can this do without problem, but i need to edit
    any time my batch script. here i was thinking with this little app
    that i have here i can do any workaround, and ask how meny pages you have, and this will do automatic this split.

    Please are this a possible realistic way??
    thanks meny time for your possible help
    regards and thanks
    Mauri

    Code:
    #include <stdio.h>
    #include <process.h>
    
    
    int main()
    {
        int choice=0;
    
    
        printf("\n******* this App will split your PDF in single PDF Sites **********\n");
        printf("1. how meny pages have your PDF 1\n");
        printf("2. how meny pages have your PDF 2\n");
        printf("3. how meny pages have your PDF 3\n");
        printf("4. how meny pages have your PDF 4\n");
        printf("5. how meny pages have your PDF 5\n");
        printf("6. how meny pages have your PDF 6\n");
        printf("7. how meny pages have your PDF 7\n");
    
    
        printf("** Enter your choice :");
        scanf("%d",&choice);
    
    
        switch(choice)
        {
            case 1:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 2:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=2 -dLastPage=2 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 3:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=3 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 4:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=4 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 5:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=5 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 6:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=6 -sOutputFile=%%d.pdf combine.pdf");
                break;
            case 7:
                system("C:\Program Files\gs\gs9.53.3\bin\gswin64.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=7 -sOutputFile=%%d.pdf combine.pdf");
                break;
            default:
                printf("\n Invalid choice !!!");
        }
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,618
    To be honest, you'd be better off writing this as a batch script (.bat or .cmd) file to begin with.

    Code:
    #include <stdio.h>
    
    int main ( ) {
      // backslashes need quoting
      // the whole program name needs to be in double "" since it contains spaces
      const char *program = "\"C:\\Program Files\\gs\\gs9.53.3\\bin\\gswin64.exe\"";
      const char *fixOpts = "-dBATCH -dNOPAUSE -sDEVICE=pdfwrite";
      char pageChoices[100];
      char fileChoices[100];
      char wholeCommand[1000];
      int choice;
    
      printf("** Enter your choice :");
      scanf("%d",&choice);
    
      sprintf(pageChoices,"-dFirstPage=1 -dLastPage=%d", choice);
      sprintf(fileChoices,"-sOutputFile=%%d.pdf combine.pdf");    // what is %%d here?
    
      sprintf(wholeCommand, "%s %s %s %s",
              program, fixOpts, pageChoices, fileChoices);
    
      printf("Calling system with\n%s\n",wholeCommand);
      //system(wholeCommand);
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    ** Enter your choice :3
    Calling system with
    "C:\Program Files\gs\gs9.53.3\bin\gswin64.exe" -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=3 -sOutputFile=%d.pdf combine.pdf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    497
    That's a brute force way of doing it.

    You can construct a string to pass to system(). Use snprintf(), and test it first by outputting the command. Then write a real program that passes it to system().
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    851
    I strongly recommend to avoid using system() in any project. And I think Salem is right saying this is better suited to a script/batch file. Just as an exercise here's my implementation using execv(), instead:
    Code:
    /* cutpdf.c */
    #ifndef _WIN32
    # error This is for Windows only.
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <errno.h>
    
    #include <io.h>
    #include <process.h>
    
    // Windows don't have asprintf() function. Emulate one.
    static int asprintf ( char **, char *, ... );
    
    int main ( int argc, char *argv[] )
    {
      unsigned long first, last;
      char *p;
    
      if ( argc != 5 )
      {
        fputs ( "Usage: cutpdf <first page> <last page> <inpdffile> <outpdffile>\n",
                 stderr );
        return EXIT_FAILURE;
      }
    
      char *argv_[8] = { "\"C:\\Program Files\\gs\\gs9.53.3\\bin\\gswin64.exe\"",
                         "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite"
                       };
    
      // Converts and verify validity of arguments.
      first = strtoul ( argv[1], &p, 10 );
    
      if ( *p || errno == ERANGE )
      {
        fputs ( "Error: First page invalid.\n", stderr );
        return EXIT_FAILURE;
      }
    
      last = strtoul ( argv[2], &p, 10 );
    
      if ( *p || errno == ERANGE )
      {
        fputs ( "Error: First page invalid.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // last page must be >= first page.
      if ( first > last )
      {
        fputs ( "Error: First page cannot be greater than the last page.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // Checks if input file is readable.
      if ( _access ( argv[3], 4 ) )
      {
        fprintf ( stderr, "Error: Cannot read '%s'.\n", argv[3] );
        return EXIT_FAILURE;
      }
    
      asprintf ( &argv_[4], "-dFirstPage=%lu", first );
      asprintf ( &argv_[5], "-dLastPage=%lu", last );
      asprintf ( &argv_[6], "-sOutputFile=\"%s\"", argv[3] );
      argv_[7] = argv[4];
      // Or change to:
      //    asprintf( &argv_[7], "\"%s\"", argv[4] );
    
      _execv ( argv_[0], ( const char *const * ) argv_ );
    
      // We'll get here only if _execv() fails.
      // Free the allocated strings.
      free ( argv_[4] );
      free ( argv_[5] );
      free ( argv_[6] );
      // add 'free( argv_[7] );' if you use asprintf above.
    
      fputs ( "ERROR: Process not executed.\n", stderr );
      return EXIT_FAILURE;
    }
    
    int asprintf ( char **p, char *fmt, ... )
    {
      char c;
      int size;
      va_list args;
    
      va_start ( args, fmt );
    
      if ( ( size = vsnprintf ( &c, sizeof c, fmt, args ) ) >= 0 )
      {
        *p = malloc ( size + 1 );
        vsprintf ( *p, fmt, args );
      }
    
      va_end ( args );
    
      return size;
    }
    Last edited by flp1969; 02-06-2021 at 09:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split strings (and split thread)
    By mahi in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 06:56 AM
  2. Fast Dial(Firefox addon) users, beware
    By stevesmithx in forum Tech Board
    Replies: 6
    Last Post: 01-07-2009, 01:51 PM
  3. How I do Split?
    By Nasimov in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2003, 09:24 AM
  4. Need Outlook 2003 addon: sort emails in special folders?
    By gicio in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-07-2003, 01:30 PM
  5. Outlook addon to make mail rules/filters?!!?!?
    By gicio in forum Tech Board
    Replies: 1
    Last Post: 09-23-2003, 07:26 PM

Tags for this Thread