PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 4:50 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Aug 31, 2017 6:30 am 
Offline

Joined: Thu Aug 31, 2017 6:27 am
Posts: 3
Hello i created merge script with PdfSharp. How can reverse the merge order in the end like : page 3, page 2 and page 1, etc. ?

Code:
Add-Type -Path C:\assemblies\PdfSharp.dll                       
$MYName =  Get-Date -format "MMMM yyyy"


Function Merge-PDF {           
    Param($path, $filename)                       
           
    $output = New-Object PdfSharp.Pdf.PdfDocument           
    $PdfReader = [PdfSharp.Pdf.IO.PdfReader]           
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]                       
           
    foreach($i in (gci $path *.pdf -Recurse)) {           
        $input = New-Object PdfSharp.Pdf.PdfDocument           
        $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import)           
        $input.Pages | %{$output.AddPage($_)}           
    }                       
           
    $output.Save($filename)           
}

Merge-PDF -path c:\fso -filename C:\WEB\Apache24\htdocs\WEBDams\Data\CHQ\HQ\1\$MYName.pdf
Write-Host "Файлът е създаден и качен на сайта!"
Start-Sleep -s 4


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 31, 2017 7:51 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Use a for loop that runs from pages.Count - 1 down to 0 and add the pages in the reverse order right from the start.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 31, 2017 8:00 am 
Offline

Joined: Thu Aug 31, 2017 6:27 am
Posts: 3
Thomas Hoevel wrote:
Hi!

Use a for loop that runs from pages.Count - 1 down to 0 and add the pages in the reverse order right from the start.

:)
I dont understand very well. Can i use the foreach in this line somehow:
Code:
$input.Pages | %{$output.AddPage($_)}


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 31, 2017 8:28 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
The name is PDFsharp because it was developed with C#. I am not a PowerShell guru.

In C# I can use "Reverse()" if I want to stick to foreach:
Code:
var reversedList = pdfRenderer.PdfDocument.Pages.PagesArray.Reverse();
foreach (var page in reversedList)
{
    // ...
}

I don't know how that translates to PowerShell. OTOH PowerShell may have other options I am completely unaware of.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 31, 2017 1:45 pm 
Offline

Joined: Thu Aug 31, 2017 6:27 am
Posts: 3
Thomas Hoevel wrote:
The name is PDFsharp because it was developed with C#. I am not a PowerShell guru.

In C# I can use "Reverse()" if I want to stick to foreach:
Code:
var reversedList = pdfRenderer.PdfDocument.Pages.PagesArray.Reverse();
foreach (var page in reversedList)
{
    // ...
}

I don't know how that translates to PowerShell. OTOH PowerShell may have other options I am completely unaware of.


I just figured out. In Powershell you can use the Sort-Object Aliases - "sort". Like this:
Code:
gci $path *.pdf -Recurse|sort -Descending

Thank you for finding time to help. :)

Final code:

Code:
Add-Type -Path C:\assemblies\PdfSharp.dll                       
$MYName =  Get-Date -format "MMMM yyyy"


Function Merge-PDF {           
    Param($path, $filename)                       
           
    $output = New-Object PdfSharp.Pdf.PdfDocument           
    $PdfReader = [PdfSharp.Pdf.IO.PdfReader]           
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]                       
           
    foreach($i in (gci $path *.pdf -Recurse|sort -Descending)) {           
        $input = New-Object PdfSharp.Pdf.PdfDocument           
        $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import)         

        for ($index = 0; $index -lt $input.PageCount; $index++) {
            $page = $input.Pages[$index]
            $null = $output.AddPage($page)

        }      
    }                       
           
    $output.Save($filename)           
}

Merge-PDF -path c:\fso -filename C:\WEB\Apache24\htdocs\WEBDams\Data\CHQ\HQ\1\$MYName.pdf
Write-Host "Файлът е създаден и качен на сайта!"
Start-Sleep -s 4


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 140 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group