diff --git a/Example/Example02.Splitting/Example02.ps1 b/Example/Example02.Splitting/Example02.ps1 index c9dfbc9..06619fc 100644 --- a/Example/Example02.Splitting/Example02.ps1 +++ b/Example/Example02.Splitting/Example02.ps1 @@ -9,3 +9,9 @@ Split-PDF -FilePath "$PSScriptRoot\SampleToSplit.pdf" -OutputFolder "$PSScriptRo # Split PDF using bookmark titles Split-PDF -FilePath "$PSScriptRoot\SampleToSplit.pdf" -OutputFolder "$PSScriptRoot\Output" -Bookmark 'Chapter 1','Chapter 2' +# Show a dry run without creating files +Split-PDF -FilePath "$PSScriptRoot\SampleToSplit.pdf" -OutputFolder "$PSScriptRoot\Output" -SplitCount 1 -WhatIf + +# Overwrite existing files if needed +Split-PDF -FilePath "$PSScriptRoot\SampleToSplit.pdf" -OutputFolder "$PSScriptRoot\Output" -SplitCount 1 -OutputName 'part' -Force + diff --git a/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs b/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs index 80816ca..00b919f 100644 --- a/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs +++ b/Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs @@ -6,7 +6,7 @@ namespace PSWritePDF.Cmdlets; - [Cmdlet(VerbsCommon.Split, "PDF")] +[Cmdlet(VerbsCommon.Split, "PDF", SupportsShouldProcess = true)] public class CmdletSplitPDF : PSCmdlet { private const string SplitCountParameterSet = "SplitCount"; @@ -42,6 +42,11 @@ public class CmdletSplitPDF : PSCmdlet [Parameter(ParameterSetName = BookmarkParameterSet)] public SwitchParameter IgnoreProtection { get; set; } + [Parameter(ParameterSetName = SplitCountParameterSet)] + [Parameter(ParameterSetName = PageRangeParameterSet)] + [Parameter(ParameterSetName = BookmarkParameterSet)] + public SwitchParameter Force { get; set; } + protected override void ProcessRecord() { if (!File.Exists(FilePath)) @@ -58,6 +63,11 @@ protected override void ProcessRecord() try { + if (!ShouldProcess(FilePath, $"Split into '{OutputFolder}'")) + { + return; + } + using var reader = new PdfReader(FilePath); if (IgnoreProtection) { @@ -65,7 +75,7 @@ protected override void ProcessRecord() } using var document = new PdfDocument(reader); - var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName); + var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName, Force.IsPresent); IList documents; if (ParameterSetName == SplitCountParameterSet) @@ -91,6 +101,8 @@ protected override void ProcessRecord() { doc.Close(); } + + WriteObject(splitter.OutputFiles, true); } catch (Exception ex) { diff --git a/Sources/PSWritePDF/Support/PdfSequentialSplitter.cs b/Sources/PSWritePDF/Support/PdfSequentialSplitter.cs index f16511d..d0a0d8c 100644 --- a/Sources/PSWritePDF/Support/PdfSequentialSplitter.cs +++ b/Sources/PSWritePDF/Support/PdfSequentialSplitter.cs @@ -10,19 +10,33 @@ internal sealed class PdfSequentialSplitter : PdfSplitter { private readonly string _destinationFolder; private readonly string _outputName; + private readonly bool _overwrite; private int _order; + public IList OutputFiles { get; } - public PdfSequentialSplitter(PdfDocument pdfDocument, string destinationFolder, string outputName) : base(pdfDocument) + public PdfSequentialSplitter(PdfDocument pdfDocument, string destinationFolder, string outputName, bool overwrite) : base(pdfDocument) { _destinationFolder = destinationFolder; _outputName = outputName; + _overwrite = overwrite; _order = 0; + OutputFiles = new List(); } protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange) { var name = $"{_outputName}{_order++}.pdf"; var path = Path.Combine(_destinationFolder, name); + if (File.Exists(path)) + { + if (!_overwrite) + { + throw new IOException($"File '{path}' already exists. Use -Force to overwrite."); + } + File.Delete(path); + } + + OutputFiles.Add(path); return new PdfWriter(path); } diff --git a/Tests/Split-PDF.Tests.ps1 b/Tests/Split-PDF.Tests.ps1 index e81b520..673453f 100644 --- a/Tests/Split-PDF.Tests.ps1 +++ b/Tests/Split-PDF.Tests.ps1 @@ -22,24 +22,47 @@ Describe 'Split-PDF' { $output = Join-Path $PSScriptRoot 'Output' 'Parts' New-Item -Path $output -Force -ItemType Directory | Out-Null $file = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' - Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -OutputName 'part' - (Get-ChildItem -Path $output -Filter 'part*.pdf').Count | Should -BeGreaterThan 1 + $files = Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -OutputName 'part' + $files.Count | Should -BeGreaterThan 1 + $files | ForEach-Object { Test-Path $_ | Should -BeTrue } } It 'splits pdf by page ranges' { $output = Join-Path $PSScriptRoot 'Output' 'Ranges' New-Item -Path $output -Force -ItemType Directory | Out-Null $file = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' - Split-PDF -FilePath $file -OutputFolder $output -PageRange '1' - (Get-ChildItem -Path $output -Filter 'OutputDocument*.pdf').Count | Should -Be 1 + $files = Split-PDF -FilePath $file -OutputFolder $output -PageRange '1' + $files.Count | Should -Be 1 + $files | ForEach-Object { Test-Path $_ | Should -BeTrue } } It 'splits pdf by bookmarks' { $output = Join-Path $PSScriptRoot 'Output' 'Bookmarks' New-Item -Path $output -Force -ItemType Directory | Out-Null $file = Join-Path $PSScriptRoot 'Input' 'Bookmarked.pdf' - Split-PDF -FilePath $file -OutputFolder $output -Bookmark 'Chapter 1','Chapter 2' - (Get-ChildItem -Path $output -Filter 'OutputDocument*.pdf').Count | Should -Be 2 + $files = Split-PDF -FilePath $file -OutputFolder $output -Bookmark 'Chapter 1','Chapter 2' + $files.Count | Should -Be 2 + $files | ForEach-Object { Test-Path $_ | Should -BeTrue } + } + + It 'performs a dry run with WhatIf' { + $output = Join-Path $PSScriptRoot 'Output' 'DryRun' + New-Item -Path $output -Force -ItemType Directory | Out-Null + $file = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' + $files = Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -WhatIf + (Get-ChildItem -Path $output -Filter '*.pdf').Count | Should -Be 0 + $files | Should -BeNullOrEmpty + } + + It 'overwrites existing files with Force' { + $output = Join-Path $PSScriptRoot 'Output' 'Force' + New-Item -Path $output -Force -ItemType Directory | Out-Null + $file = Join-Path $PSScriptRoot 'Input' 'SampleToSplit.pdf' + $first = Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -OutputName 'part' + $second = Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -OutputName 'part' + $second | Should -BeNullOrEmpty + $forced = Split-PDF -FilePath $file -OutputFolder $output -SplitCount 1 -OutputName 'part' -Force + $forced.Count | Should -BeGreaterThan 0 } AfterAll {