#!/usr/bin/perl
# this script prints page number in pdf files 
# 
# ./print_page_number.pl

use pdflib_pl 5.0;

$tnum = 0;
$nn=1;
$imagefile="logo4.jpg";
$inprogramfile = "program.in";
$outprogramfile = "program.txt";
$headerlines = 4;

$indir = "./InputFiles";
$outdir = "./OutputFiles";

if (! -e $outdir) { mkdir $oudir, 0755; }


#######################################################################
open(INPROG, $inprogramfile)||die;
$c = 0;
while (!eof(INPROG)){
    for ($i = 0; $i < $headerlines; $i++){ # just skip first four lines
        $_ = <INPROG>;
    }

    while (<INPROG>){
        chomp;
        @line = split;
        $paper = $line[0];
        if ($line[0] eq "---"){
            last;
		}

		$input_file = "$indir/$paper.pdf";
		print "testing... $paper : $input_file\n";

		if (! -e $input_file) {
			print "$paper: not exist\n";
		}

		$p = PDF_new();
		$form = PDF_open_pdi($p, $input_file, "pdiwarning", 0);
		if ($form == -1) {
   			printf(STDERR "Couldn't open input file '%s'.\n", $input_file);
   			exit(2);
		}

		$pdf_version=PDF_get_pdi_value($p, "version",$input_file,-1,0);
		if($pdf_version == 15){
			$change[$c] = $paper;
			#printf("%s hit\n",$change[$c]);
			$c++;
		}
	}
}
close(INPROG);


######################################################################

open(INPROG, $inprogramfile)||die;
open(OUTPROG, ">$outprogramfile")||die;

while (!eof(INPROG)){
	for ($i = 0; $i < $headerlines; $i++){
        $_ = <INPROG>;
        print OUTPROG;
    }

    while (<INPROG>){
        chomp;
        @line = split;
        $paper = $line[0];
        if ($line[0] eq "---"){ last; }

		$input_file = "$indir/$paper.pdf";
		$output_file = "$outdir/$paper.pdf";

		$p = PDF_new();
 		PDF_set_parameter($p, "license", "xxxxxxxxxxxxxxxxxxxxxxxxxx");

		foreach(@change){
			if($_ eq  $paper){
				PDF_set_parameter($p,"compatibility", '1.5');
			}
		}
		#PDF_set_parameter($p,"compatibility", '1.5');
		if(PDF_open_file($p, "$output_file") == -1) {
		    printf(STDERR "Error: cannot open PDF file personalize.pdf.\n");
			exit(2);
		}

		PDF_set_info($p, "Creator", "(C) RISP ");
		PDF_set_info($p, "Author", "NCSP'06");
		PDF_set_info($p, "Title", "$paper");
		#PDF_set_info($p, "Subject", "");
		PDF_set_info($p, "Keywords", "pdflib");

		#print "$input_file :";
		$form = PDF_open_pdi($p, $input_file, "pdiwarning", 0);
		if ($form == -1) {
		    printf(STDERR "Couldn't open input file '%s'.\n", $input_file);
		    exit(2);
		}

		open(CN, "<current_number") ||die;
		$cn=<CN>;
		close(CN);
		$pcn = $cn;

##################################################################
		$pdf_version=PDF_get_pdi_value($p, "version",$input_file,-1,0);
		if($pdf_version >= 16){

			$cn = 1;
			open(CN,">current_number")||die;
			print CN $cn;
			close(CN);
			printf("\n");
			printf(STDERR "Couldn't open input file '%s'.\n", $input_file);
			exit(2);
		}
##################################################################

		$num=PDF_get_pdi_value($p,"/Root/Pages/Count",$input_file,-1,0);
		if($num % 2 == 1 && $num != 1){
			$flag = 1;
			$num++;
		}else{
			$flag = 0;
		}
		$tnum += $num;

		$page_number = 1;
		$i = 1;

		while ($i <= $num)
		{

			if($i == $num && $flag == 1){
				PDF_begin_page($p, $width, $height);
			}else{
				$pidpage = PDF_open_pdi_page($p, $form, 
					$page_number, "pdiwarning");
				if ($pidpage == -1) {
                	printf(STDERR "Couldn't open page $pn in '%s'.\n", 
						$input_file);
                	PDF_close($p);
					break;
        		}
				# get the dimensions of the imported form
				$width = PDF_get_pdi_value($p, "width", $form, $pidpage, 0);
				$height = PDF_get_pdi_value($p, "height", $form, $pidpage, 0);
				#printf("%d ,%d ",$width, $height); 

				PDF_begin_page($p, $width, $height);
				PDF_place_pdi_page($p, $pidpage, 0, 0, 1, 1);
				PDF_close_pdi_page($p, $pidpage);
			}

			if($page_number == 1){
				PDF_set_parameter($p, "bookmarkdest", "type=file filename=../NCSP2006.pdf");
				PDF_add_bookmark($p, "NCSP2006 TOP", 0, 1);

				$image = PDF_load_image($p, "auto", $imagefile, "");
					die "Couldn't open image '$imagefile'" if ($image == -1);

				PDF_place_image($p, $image, $width-275, $height-66, 0.33);

				$font = PDF_findfont($p, "Times-Roman", "host", 0);
				PDF_setfont($p, $font, 8);
				PDF_show_xy($p, 
					"               2006 RISP International Workshop", 
					$width-240, $height-40);
				PDF_continue_text($p,
					"on Nonlinear Circuits and Signal Processing (NCSP'06)");
				PDF_continue_text($p,
					"               Waikiki Beach Marriott, Honolulu,");
				PDF_continue_text($p,
					"                  Hawaii, USA, March 3-5, 2006");
			}

			#$font = PDF_findfont($p, "Helvetica-Bold", "host", 0);
			#$font = PDF_findfont($p, "HeiseiMin-W3", "EUC-H", 0);
			$font = PDF_findfont($p, "Times-Roman", "host", 0);
			PDF_setfont($p, $font, 12);

			#pdf_show_xy ( int pdf object, string text, float x, float y)
			if($cn <10){ 
				PDF_show_xy($p, "- $cn -", ($width/2)-12, 20);
			} elsif($cn <100) {
				PDF_show_xy($p, "- $cn -", ($width/2)-12-1, 20);
			}else{
				PDF_show_xy($p, "- $cn -", ($width/2)-12-3, 20);
			}

			PDF_end_page($p);
			$page_number++;
			$cn++;
			$i++;
		}
		#
		#if($flag == 1){
		#	if($num==1){
		#	printf("\t%d(%d) page :", $num,$num-1);
		#	}else{
		#	printf("\t%d(%d) pages :", $num,$num-1);
		#	}
		#}else{
		#    if($num==1){
		#    printf("\t%d    page :", $num);
		#    }else{
		#    printf("\t%d    pages :", $num);
		#    }
		#}

		if($pcn-($cn-1)==0){
			printf OUTPROG ("%03d#%d#%d\n", $paper, $pcn,$cn-1);
		}else{
			printf OUTPROG ("%03d#%d#%d\n", $paper, $pcn,$cn-1);
		}

		open(CN,">current_number")||die;
		print CN $cn;
		close(CN);
		PDF_close($p);
		$nn++;
	}
	print OUTPROG "---\n";
	$nn=1;
}

printf("total page number = %d\n",$tnum);

$cn = 1;
open(CN,">current_number")||die;
print CN $cn;
close(CN);
printf("current_number file is initialized.\n");
close(INPROG);
close(OUTPROG);


