use strict;
use FindBin;
use lib "$FindBin::Bin";
use Tk;
use Tk::HList;
use Tk::JPEG;
use Tk::DragDrop;
use Tk::DropSite;
use Tk::Photo;
use Tk::ItemStyle;
use PDF::Reuse;
use File::Find;
use FindBin;
my $programmpfad = $FindBin::RealBin;
my $gs_prog = ($^O eq 'MSWin32' ? "$programmpfad"."\\"."gswin32.exe" : "ghostscript");
my $current_file;
my ($mw);
my $first_pdf;
my $second_pdf;
my $goal_pdf;
my $dnd_token;
my $dnd_token2;
my @ImgArr;
my $TextOver;
my $dn;
my $dn2;
DoMain();
sub DoMain
{
$mw = MainWindow->new;
$mw->title("Clear up PDF! ver. 0.06");
my $menu = $mw->Menu(-type => 'menubar');
$mw->configure(-menu => $menu);
$menu->cascade(-label => 'Datei',
-underline => 0,
);
my $menu_datei = $menu->Menu(-tearoff => 0,
-menuitems => [
[
'command' => 'erste PDF Datei öffnen',
-command => \&datei_oeffnen1,
-accelerator => 'F2',
-underline => 6,
],
[
'command' => 'zweite PDF Datei öffnen',
-command => \&datei_oeffnen2,
-accelerator => 'F3',
-underline => 6,
],
[
'command' => 'Datei speichern unter',
-command => \&datei_speichern_unter,
-accelerator => 'F12',
-underline => 16,
]
]
);
$menu->entryconfigure('Datei', -menu => $menu_datei);
$first_pdf = $mw->Scrolled("HList",
-itemtype => 'image',
-selectmode => 'browse',
-browsecmd => sub {OnClick($first_pdf) },
-height => 50,
-width => 20,
-scrollbars => 'osoe',
)->pack(-side => 'left',
-expand => 1,
-fill => 'both'
);
$goal_pdf = $mw->Scrolled("HList",
-itemtype => 'image',
-selectmode => 'browse',
-browsecmd => sub { OnClick($goal_pdf)},
-height => 50,
-width => 20,
-scrollbars => 'osoe',
)->pack(-side => 'right',
-expand => 1,
-fill => 'both'
);
#$TextOver=$first_pdf->ItemStyle("imagetext", -textanchor => "n");
$second_pdf = $mw->Scrolled("HList",
-itemtype => 'image',
-selectmode => 'browse',
-browsecmd => sub { OnClick($second_pdf)},
-height => 50,
-width => 20,
-scrollbars => 'osoe',
)->pack(-side => 'right',
-expand => 1,
-fill => 'both');
$dnd_token = $first_pdf->DragDrop(-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub { StartDrag1($dnd_token) },
);
$dnd_token2 = $second_pdf->DragDrop(-event => '<B1-Motion>',
-sitetypes => ['Local'],
-startcommand => sub { StartDrag2($dnd_token2) },
);
$goal_pdf->DropSite(-droptypes => ['Local'],
-dropcommand => [ \&Drop, $goal_pdf, $dnd_token ],
);
$goal_pdf->DropSite(-droptypes => ['Local'],
-dropcommand => [ \&Drop, $goal_pdf, $dnd_token2 ],
);
$goal_pdf->bind("<Delete>", \&Delete);
MainLoop;
}
sub datei_oeffnen1
{
my $typen = [
['Alle PDF Dateien', ['.pdf']],
['Alle Dateien', '*' ],
];
use File::Path;
mkpath "$programmpfad/images/1" if !-d "$programmpfad/images/1";
$dn = $mw->getOpenFile(-filetypes => $typen);
my $cmd = "\"$gs_prog\" -q -dBATCH -dMaxBitmap=300000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=\"$programmpfad/images/1/seite%d.jpg\" \"$dn\" -c quit";
system "$cmd";
print "Ghost ist fertig\n";
# load data for resource list
my $i=0;
for my $fn (glob ("\"$programmpfad/images/1/*.jpg\"")) {
my $SrcImg=$mw->Photo(-file => $fn);
my $DstImg=$mw->Photo();
my $w=$SrcImg->width();
my $h=$SrcImg->height();
my $f=int($h/200);
$DstImg->copy($SrcImg, -subsample => ($f, $f)); # rescales image to one size
$i++;
push @ImgArr, $DstImg;
print "$i:$fn:$w $h\n";
$SrcImg->delete();
my $e=$first_pdf->addchild("", -data => $fn,);
$first_pdf->itemCreate($e, 0,
-itemtype => "imagetext",
-image => $DstImg,
-text => $fn,
-style => $TextOver,
);
}
}
sub datei_oeffnen2
{
my $typen = [
['Alle PDF Dateien', ['.pdf']],
['Alle Dateien', '*' ],
];
use File::Path;
mkpath "$programmpfad/images/2" if !-d "$programmpfad/images/2";
$dn2 = $mw->getOpenFile(-filetypes => $typen);
my $cmd = "\"$gs_prog\" -q -dBATCH -dMaxBitmap=300000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile=\"$programmpfad/images/2/seite%d.jpg\" \"$dn2\" -c quit";
system "$cmd";
# load data for resource list
my $i=0;
for my $fn (glob ("\"$programmpfad/images/2/*.jpg\"")) {
my $SrcImg=$mw->Photo(-file => $fn);
my $DstImg=$mw->Photo();
my $w=$SrcImg->width();
my $h=$SrcImg->height();
my $f=int($h/200);
$DstImg->copy($SrcImg, -subsample => ($f, $f)); # rescales image to one size
$i++;
push @ImgArr, $DstImg;
print "$i:$fn:$w $h\n";
$SrcImg->delete();
my $e=$second_pdf->addchild("", -data => $fn,);
$second_pdf->itemCreate($e, 0,
-itemtype => "imagetext",
-image => $DstImg,
-text => $fn,
-style => $TextOver,
);
}
}
sub StartDrag1 {
my($token) = @_;
my $w = $token->parent; # $w is the source listbox
my $e = $w->XEvent;
my $idx = $w->nearest($e->y); # get the listbox entry under cursor
#print "y:", $e->y, "\n";
if (defined $idx) {
# Configure the dnd token to show the listbox entry
my $opt=$token->configure();
$token->configure(-image => $ImgArr[$idx]);
$token->configure(-text => $w->info("data", $idx));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
#print "$token\n";
}
sub StartDrag2 {
my($token) = @_;
my $w = $token->parent; # $w is the source listbox
my $e = $w->XEvent;
my $idx = $w->nearest($e->y); # get the listbox entry under cursor
#print "y:", $e->y, "\n";
if (defined $idx) {
# Configure the dnd token to show the listbox entry
my $opt=$token->configure();
$token->configure(-image => $ImgArr[$idx]);
$token->configure(-text => $w->info("data", $idx));
# Show the token
my($X, $Y) = ($e->X, $e->Y);
$token->MoveToplevelWindow($X, $Y);
$token->raise;
$token->deiconify;
$token->FindSite($X, $Y, $e);
#print "$token\n";
}
}
# Accept a drop and insert a new item in the destination listbox.
sub Drop
{
my($hlist, $dnd2) = @_;
my $img = $dnd2->cget(-image);
my $text = $dnd2->cget(-text);
print "Text $text\n";
my $fn="";
my $e=$hlist->addchild("", -data => $fn);
$hlist->itemCreate($e, 0,
-itemtype => "imagetext",
-image => $img,
-text => $text,
-style => $TextOver,
);
$hlist->see($e);
}
sub Delete {
my ($w) = @_;
my @sl=$goal_pdf->info("selection");
print "got delete: $w, selected: @sl\n";
$goal_pdf->delete("entry", @sl) if ($#sl >= 0);
}
sub OnClick {
my $hlist =shift;
my $e = $hlist->XEvent;
my $entry = $hlist->nearest($e->y);
my $data=$hlist->info("data", $entry);
my $val=$hlist->entrycget($entry, "-image");
my $d=$hlist->entrycget($entry, "-data");
my @l=$hlist->info("children");
my $i=$entry-1;
my $h=$ImgArr[$i]->height();
my $t=$ImgArr[$i]->type();
my $f=$ImgArr[$i]->cget("-file");
}
}
sub datei_speichern_unter
{
my $typen = [
['Alle PDF Dateien', ['.pdf']],
['Alle Dateien', '*' ],
];
my $dn3 = $mw->getSaveFile(-filetypes => $typen);
if (defined ($dn3))
{
my @new_pdf_pages = $goal_pdf->Get(-text);
print "array @new_pdf_pages";
#print $dn3;
#prFile("$dn3");
#prDocDir('C:/Dokumente und Einstellungen/Administrator/Desktop/Privat/');
#prDoc("$dn");
#prDoc("$dn2");
#prEnd();
#$pdf->savePDFExtract(PDFPages=>"1 3-6", PDFDoc=>$dn, PDFPages=>"1 3-6", PDFDoc=>$dn2, PDFCache=>"C:\\" )|| die print "ouch2";
#$pdf->savePDFExtract(PDFCache=>"C:\\" )|| die print "ouch3";
}
}