-
Hi all, are we able to split PDFs by a given page range? |
Beta Was this translation helpful? Give feedback.
Answered by
zlangner
Mar 9, 2022
Replies: 2 comments 1 reply
-
Yes. I'm not sure what exactly you mean so I'm going to assume you have a known page range that you want to extract from a provided pdf stream.
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
topcat30
-
Legend thankyou. Ill give it a crack this arvo
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows
…________________________________
From: Zach Langner ***@***.***>
Sent: Thursday, March 10, 2022 8:19:18 AM
To: UglyToad/PdfPig ***@***.***>
Cc: topcat30 ***@***.***>; Author ***@***.***>
Subject: Re: [UglyToad/PdfPig] Split PDF (Discussion #423)
Yes. I'm not sure what exactly you mean so I'm going to assume you have a known page range that you want to extract from a provided pdf stream.
using System.Collections.Generic;
using System.IO;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Writer;
namespace MySplitter
{
public static class PdfPigExtensions
{
public static MemoryStream ExtractPages(Stream inputStream, int firstPage, int lastPage)
{
var ms = new MemoryStream();
using (var destDoc = new PdfDocumentBuilder(ms, disposeStream: false))
using (var srcDoc = PdfDocument.Open(inputStream))
{
int numPages = srcDoc.NumberOfPages;
// copy the specific page range from source into the new destination
srcDoc.CopyPagesTo(firstPage, lastPage, destDoc);
}
ms.Position = 0;
return ms;
}
public static List<PdfPageBuilder> CopyPagesTo(this PdfDocument srcDoc, int pageFrom, int pageTo, PdfDocumentBuilder destDoc)
{
var newPages = new List<PdfPageBuilder>();
for (int i = pageFrom; i <= pageTo; i++)
{
// Get the page from the external document...
var page = destDoc.AddPage(srcDoc, i);
// and copy to the resulting PDF
newPages.Add(page);
}
return newPages;
}
}
}
—
Reply to this email directly, view it on GitHub<#423 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AHHGIUXZXZTMGGAI3ASVIL3U7EIVNANCNFSM5PAADA3Q>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. I'm not sure what exactly you mean so I'm going to assume you have a known page range that you want to extract from a provided pdf stream.