Split PDF into Pages using iTextSharp

//Get's Each Page and the Bytes from the page //Returns a dictionary with page number as key and pdf //bytes as value. To write to disk, just iterate and use File.WriteAllBytes public static Dictionary<int, byte[]> GetPageBytesFromPDF(byte[] PDFBytes) { var _reader = new PdfReader(PDFBytes); var results = new ConcurrentDictionary<int, byte[]>(); Parallel.For(1, _reader.NumberOfPages, (i) => { using (var ms = new MemoryStream()) { var source = new iTextSharp.text.Document(_reader.GetPageSizeWithRotation(i)); var copyProvider = new PdfCopy(source, ms); source.Open(); var page = copyProvider.GetImportedPage(_reader, i); copyProvider.AddPage(page); source.Close(); results.AddOrUpdate(i, ms.ToArray(), (pg, cont) => { return ms.ToArray(); }); } }); return results.ToDictionary(a => a.Key, a => a.Value); }
Get's Each Page and the Bytes from the page
Returns a dictionary with page number as key and pdf
bytes as value. To write to disk, just iterate and use File.WriteAllBytes

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.