Skip to content

Commit

Permalink
Merged Unstable branch, watermark implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TsimpDim committed Jul 17, 2017
2 parents 04d3558 + 8daa835 commit 478e27a
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 46 deletions.
8 changes: 4 additions & 4 deletions PDF_Merger/PDF_Merger_WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\arrow-up.png" />
<Resource Include="Resources\arrow-down.png" />
<Resource Include="Resources\Triangle_DOWN_512.png" />
<Resource Include="Resources\Triangle_UP_512.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\delete.png" />
<Resource Include="Resources\X_512.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\PDF_Merger_icon_3.png" />
<Resource Include="Resources\PDF_Merger_Icon.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Binary file added PDF_Merger/Resources/PDF_Merger_Icon.ico
Binary file not shown.
Binary file removed PDF_Merger/Resources/PDF_Merger_icon_3.png
Binary file not shown.
Binary file added PDF_Merger/Resources/Triangle_DOWN_512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PDF_Merger/Resources/Triangle_UP_512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PDF_Merger/Resources/X_512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed PDF_Merger/Resources/arrow-down.png
Binary file not shown.
Binary file removed PDF_Merger/Resources/arrow-up.png
Binary file not shown.
Binary file removed PDF_Merger/Resources/delete.png
Binary file not shown.
17 changes: 12 additions & 5 deletions PDF_Merger/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PDF_Merger"
mc:Ignorable="d"
Title="PDF Merger" Height="450" Width="900" Icon="/Resources/PDF_Merger_icon_3.png">

Title="PDF Merger" Height="450" Width="900" Icon="/Resources/PDF_Merger_Icon.ico" WindowStyle="ToolWindow">

<DockPanel>

<StackPanel DockPanel.Dock="Left">

<Button Content="Open Files" Click="Explorer_Button_Click" Width="250" Height="25" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderBrush="#FF4AA5FF" Margin="0 1 0 0" />
<Button Content="Merge!" Click="MergePDF" Width="250" Height="25" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Background="White" BorderBrush="#FF4AA5FF" Margin="0 1 0 0"/>


<StackPanel HorizontalAlignment="Right" Margin="0 20">
<Button Width="50" Click="Move_Up" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderBrush="#FF4AA5FF">
<Image Height="20" Width="20" Source="/Resources/arrow-up.png"/>
<Image Height="20" Width="20" Source="/Resources/TRIANGLE_UP_512.png"/>
</Button>
<Button Width="50" Click="Move_Down" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderBrush="#FF4AA5FF">
<Image Height="20" Width="20" Source="/Resources/arrow-down.png"/>
<Image Height="20" Width="20" Source="/Resources/TRIANGLE_DOWN_512.png"/>
</Button>
<Button Width="50" Click="Delete_Button" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderBrush="#FF4AA5FF">
<Image Height="20" Width="20" Source="/Resources/delete.png"/>
<Image Height="20" Width="20" Source="/Resources/X_512.png"/>
</Button>
</StackPanel>

Expand All @@ -32,6 +32,13 @@
<Label Content="Options :" Margin="0 80 0 0"/>
<CheckBox Content="Open .pdf At Merge" Checked="CheckBox_Checked_file" Unchecked="CheckBox_Unchecked_file" />
<CheckBox Content="Open .pdf Location At Merge" Checked="CheckBox_Checked_dir" Unchecked="CheckBox_Unchecked_dir"/>
<CheckBox Content="Add Watermark" Checked="CheckBox_Checked_wtrmk" Unchecked="CheckBox_Unchecked_wtrmk"/>
<TextBox Name="WtrmkTextbox" Visibility="Hidden" MaxLength="20" >Watermark</TextBox>
<ComboBox Name="dropdown" Visibility="Hidden" Background="#FFC7C7C7" BorderBrush="#FF595757">
<ComboBoxItem IsSelected="True">Under Content</ComboBoxItem>
<ComboBoxItem>Over Content</ComboBoxItem>
</ComboBox>

</StackPanel>

<StackPanel VerticalAlignment="Bottom" Name="progBcont" Margin="0 6" Visibility="Hidden">
Expand Down
157 changes: 120 additions & 37 deletions PDF_Merger/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace PDF_Merger
public partial class MainWindow : Window
{
int i = 0;
ObservableCollection<File_class> AddedPDFs = new ObservableCollection<File_class>(); //All the added PDFs are added here
bool open_file_after_merge, open_dir_after_merge;
ObservableCollection<_File> AddedPDFs = new ObservableCollection<_File>(); //All the added PDFs are added here
bool open_file_after_merge, open_dir_after_merge, add_wtrmk;
string pdfname, endfloc;

public MainWindow()
Expand All @@ -41,7 +41,7 @@ private void Explorer_Button_Click(object sender, RoutedEventArgs e)

foreach (string newfile in openDlg.FileNames)//For every file he chose
{
AddedPDFs.Add(new File_class { toMerge = true, file_id = i, file_path = newfile });//Add the new pdf to the array
AddedPDFs.Add(new _File { toMerge = true, file_id = i, file_path = newfile });//Add the new pdf to the array
i++;//Iterate the id
}

Expand Down Expand Up @@ -86,8 +86,6 @@ private void MergePDF(object sender, RoutedEventArgs e)
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();



}
}

Expand All @@ -101,48 +99,117 @@ private void CreateMergedPdf(object sender, DoWorkEventArgs e)
{
using (FileStream stream = new FileStream(pdfname, FileMode.Create))
{

Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
PdfReader reader = null;
int i = 0;

Document pdfDoc = new Document(PageSize.A4);
PdfCopy pdf = new PdfCopy(pdfDoc, stream);
pdfDoc.Open();
try
{
document.Open();
foreach (_File file in AddedPDFs)
{
(sender as BackgroundWorker).ReportProgress(i++);

int i = 0;
reader = new PdfReader(file.file_path);
pdf.AddDocument(reader);
reader.Close();
}
}
catch (Exception)
{
if (reader != null)
{
reader.Close();
}
}
finally
{
if (document != null)
{
document.Close();
}
}
}


FinalizePDF();
(sender as BackgroundWorker).ReportProgress(i++);

}

private void AddWatermark(string pdfloc)
{
PdfReader reader = new PdfReader(pdfloc);

foreach (File_class newpdf in AddedPDFs)
using (FileStream fs = new FileStream("_wtrmked.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
int pageCount = reader.NumberOfPages;

PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);

for (int i = 1; i <= pageCount; i++)
{

(sender as BackgroundWorker).ReportProgress(i++);
Rectangle rect = reader.GetPageSize(i);

if (newpdf.toMerge)

PdfContentByte cb = null;
if (this.Dispatcher.Invoke(() => dropdown.SelectedValue.ToString() == "Under Content"))
{
cb = stamper.GetUnderContent(i);
}
else
{
cb = stamper.GetOverContent(i);
}

PdfReader reader = new PdfReader(newpdf.file_path);
// Tell the cb that the next commands should be "bound" to this new layer
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);

pdf.AddDocument(reader);
this.Dispatcher.Invoke(() => progBtxt.Text = "Merging file #" + newpdf.file_id + "..."); //Dispatcher.Invoke since UI is on seperate thread
PdfGState gState = new PdfGState();
gState.FillOpacity = 0.25f;
cb.SetGState(gState);

}
cb.SetColorFill(BaseColor.BLACK);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, this.Dispatcher.Invoke(() => WtrmkTextbox.Text), rect.Width / 2, rect.Height / 2, 45f);
cb.EndText();

// Close the layer
cb.EndLayer();
}
}
reader.Close();
}

if (pdfDoc != null)
pdfDoc.Close();
private void FinalizePDF()
{
string from = AppDomain.CurrentDomain.BaseDirectory + @"\" + pdfname;
this.Dispatcher.Invoke(() => progBtxt.Text = "Moving file...");

string from = AppDomain.CurrentDomain.BaseDirectory + @"\" + pdfname;
this.Dispatcher.Invoke(() => progBtxt.Text = "Moving file...");
if (File.Exists(endfloc))
{
File.Delete(endfloc);
}
File.Move(from, endfloc); //Move from .exe path to desired path
(sender as BackgroundWorker).ReportProgress(i++);
if (add_wtrmk)
{
this.Dispatcher.Invoke(() => progBtxt.Text = "Adding watermark...");

AddWatermark(from);
string wtrmked_path = AppDomain.CurrentDomain.BaseDirectory + @"\_wtrmked.pdf";

File.Replace(wtrmked_path, from, null); // Replace the bare file with the watermaked one
}




if (File.Exists(endfloc))
{
File.Delete(endfloc);
}
File.Move(from, endfloc); //Move from .exe path to desired path

}//End of stream


if (open_dir_after_merge)
Expand All @@ -157,12 +224,8 @@ private void CreateMergedPdf(object sender, DoWorkEventArgs e)

this.Dispatcher.Invoke(() => progBtxt.Text = "Merge complete");
System.Windows.MessageBox.Show("Merge Complete", "Done!");


}



private void ChangeInclusion(object sender, MouseButtonEventArgs e)
{
var clicked = filelist.SelectedIndex;
Expand All @@ -172,7 +235,6 @@ private void ChangeInclusion(object sender, MouseButtonEventArgs e)
}



//Checkboxes
//Open file after merge
private void CheckBox_Checked_file(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -205,6 +267,28 @@ private void Handler_dir(CheckBox checkBox)
open_dir_after_merge = checkBox.IsChecked.Value;
}

//Add watermark to file
private void CheckBox_Unchecked_wtrmk(object sender, RoutedEventArgs e)
{
Handler_wtrmk(sender as CheckBox);

WtrmkTextbox.Visibility = Visibility.Hidden;
dropdown.Visibility = Visibility.Hidden;
}

private void CheckBox_Checked_wtrmk(object sender, RoutedEventArgs e)
{
Handler_wtrmk(sender as CheckBox);

WtrmkTextbox.Visibility = Visibility.Visible;
dropdown.Visibility = Visibility.Visible;

}

private void Handler_wtrmk(CheckBox checkBox)
{
add_wtrmk = checkBox.IsChecked.Value;
}


private void Move_Up(object sender, RoutedEventArgs e)//Move up pdf for insertion
Expand All @@ -217,7 +301,6 @@ private void Handler_dir(CheckBox checkBox)


/*Make sure the file_id's keep increasing order
if (AddedPDFs[selectedIndex - 1].file_id < AddedPDFs[selectedIndex].file_id)
{
int tmp = itemToMoveUp.file_id;
Expand Down Expand Up @@ -286,7 +369,7 @@ private void DeleteFile()


//FILE CLASS
public class File_class : INotifyPropertyChanged //The class under which we save the files the user chooses
public class _File : INotifyPropertyChanged //The class under which we save the files the user chooses
{
private bool _tomerge;
private int _file_id;
Expand Down Expand Up @@ -336,4 +419,4 @@ private void NotifyPropertyChanged([CallerMemberName] String propertyName = "boo
}

}
}
}

0 comments on commit 478e27a

Please sign in to comment.