Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Example/Example02.Splitting/Example02.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

16 changes: 14 additions & 2 deletions Sources/PSWritePDF/Cmdlets/CmdletSplitPDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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))
Expand All @@ -58,14 +63,19 @@ protected override void ProcessRecord()

try
{
if (!ShouldProcess(FilePath, $"Split into '{OutputFolder}'"))
{
return;
}

using var reader = new PdfReader(FilePath);
if (IgnoreProtection)
{
reader.SetUnethicalReading(true);
}

using var document = new PdfDocument(reader);
var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName);
var splitter = new PdfSequentialSplitter(document, OutputFolder, OutputName, Force.IsPresent);
IList<PdfDocument> documents;

if (ParameterSetName == SplitCountParameterSet)
Expand All @@ -91,6 +101,8 @@ protected override void ProcessRecord()
{
doc.Close();
}

WriteObject(splitter.OutputFiles, true);
}
catch (Exception ex)
{
Expand Down
16 changes: 15 additions & 1 deletion Sources/PSWritePDF/Support/PdfSequentialSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string>();
}

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);
}

Expand Down
35 changes: 29 additions & 6 deletions Tests/Split-PDF.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading