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
9 changes: 5 additions & 4 deletions Example/Example01.HelloWorld/Example01_MoreOptions.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Import-Module .\PSWritePDF.psd1 -Force
Import-Module .\PSWritePDF.psd1 -Force

New-PDF -MarginLeft 520 { #520 -MarginRight 20 -MarginTop 20 -MarginBottom 20 -PageSize B4 -Rotate {
New-PDF -PageSize B4 -Rotate -FilePath "$PSScriptRoot\Example01_MoreOptions.pdf" -Show {
New-PDFOptions -MarginLeft 520 -MarginRight 20 -MarginTop 20 -MarginBottom 20 -PassThru | Out-Null
New-PDFText -Text 'Test ', 'Me', 'Oooh' -FontColor BLUE, YELLOW, RED
New-PDFList {
New-PDFListItem -Text 'Test'
New-PDFListItem -Text '2nd'
}
} -FilePath "$PSScriptRoot\Example01_MoreOptions.pdf" -Show
}

$Document = Get-PDF -FilePath "$PSScriptRoot\Example01_MoreOptions.pdf"
$Details = Get-PDFDetails -Document $Document
$Details | Format-List
$Details.Pages | Format-Table

Close-PDF -Document $Document
Close-PDF -Document $Document
25 changes: 22 additions & 3 deletions Sources/PSWritePDF/Cmdlets/CmdletNewPDFOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@
namespace PSWritePDF.Cmdlets;

[Cmdlet(VerbsCommon.New, "PDFOptions")]
public class CmdletNewPDFOptions : PSCmdlet {
public class CmdletNewPDFOptions : PSCmdlet
{
[Parameter] public double? MarginLeft { get; set; }
[Parameter] public double? MarginRight { get; set; }
[Parameter] public double? MarginTop { get; set; }
[Parameter] public double? MarginBottom { get; set; }
[Parameter] public SwitchParameter PassThru { get; set; }

protected override void ProcessRecord() {
protected override void ProcessRecord()
{
var document = SessionState.PSVariable.GetValue("Document") as Document;
if (document != null) {
if (document != null)
{
PdfOptions.Apply(document, (float?)MarginLeft, (float?)MarginRight, (float?)MarginTop, (float?)MarginBottom);
if (PassThru)
{
WriteObject(document);
}
}
else if (PassThru)
{
var options = new PdfDocumentOptions
{
MarginLeft = (float?)MarginLeft,
MarginRight = (float?)MarginRight,
MarginTop = (float?)MarginTop,
MarginBottom = (float?)MarginBottom
};
WriteObject(options);
}
}
}
5 changes: 5 additions & 0 deletions Sources/PSWritePDF/Cmdlets/CmdletNewPDFPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[Cmdlet(VerbsCommon.New, "PDFPage")]
public class CmdletNewPDFPage : PSCmdlet {
[Parameter(Position = 0)]
public ScriptBlock? PageContent { get; set; }

Check warning on line 11 in Sources/PSWritePDF/Cmdlets/CmdletNewPDFPage.cs

View workflow job for this annotation

GitHub Actions / Windows PowerShell 7

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

[Parameter] public double? MarginLeft { get; set; }
[Parameter] public double? MarginRight { get; set; }
Expand All @@ -16,6 +16,7 @@
[Parameter] public double? MarginBottom { get; set; }
[Parameter] public PdfPageSize? PageSize { get; set; }
[Parameter] public SwitchParameter Rotate { get; set; }
[Parameter] public SwitchParameter PassThru { get; set; }

protected override void ProcessRecord() {
var pdfDocument = SessionState.PSVariable.GetValue("PdfDocument") as iTextPdf.PdfDocument;
Expand All @@ -26,5 +27,9 @@
var document = PdfPage.AddPage(pdfDocument, size, Rotate.IsPresent, (float?)MarginLeft, (float?)MarginRight, (float?)MarginTop, (float?)MarginBottom);
SessionState.PSVariable.Set("Document", document);
PageContent?.Invoke();
if (PassThru)
{
WriteObject(document);
}
}
}
11 changes: 11 additions & 0 deletions Sources/PSWritePDF/Support/PdfDocumentOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace PSWritePDF;

public class PdfDocumentOptions
{
public float? MarginLeft { get; set; }
public float? MarginRight { get; set; }
public float? MarginTop { get; set; }
public float? MarginBottom { get; set; }
}
24 changes: 24 additions & 0 deletions Tests/AdditionalCmdlets.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ Describe 'Additional cmdlets' {
$default = Get-Variable -Name DefaultFont -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
$default | Should -Not -BeNullOrEmpty
}

It 'New-PDFPage -PassThru returns document' {
$file = Join-Path $PSScriptRoot 'Output' 'page.pdf'
New-PDF -FilePath $file {
$doc = New-PDFPage -PassThru {
New-PDFText -Text 'Hi'
}
$doc | Should -BeOfType 'iText.Layout.Document'
}
}

It 'New-PDFOptions -PassThru returns document when available' {
$file = Join-Path $PSScriptRoot 'Output' 'optionsdoc.pdf'
New-PDF -FilePath $file {
$doc = New-PDFOptions -MarginLeft 15 -PassThru
$doc | Should -BeOfType 'iText.Layout.Document'
}
}

It 'New-PDFOptions -PassThru returns options object when document missing' {
$options = New-PDFOptions -MarginLeft 10 -PassThru
$options | Should -BeOfType 'PSWritePDF.PdfDocumentOptions'
$options.MarginLeft | Should -Be 10
}
}
Loading