winforms - How to split every pages of a pdf into multiple pdf files in C# (using iTextSharp)? -
i trying convert every pages of pdf separate pdf files. have given range of 6 create 6 separate pdf files.
using system; using itextsharp.text; using itextsharp.text.pdf; using system.windows.forms; using system.io; namespace learning.spitpdfapp { public partial class mainform : form { public mainform() { initializecomponent(); } private void splitpdfbutton_click(object sender, eventargs e) { mainform objmainform = new mainform(); objmainform.extractpage(sourcetextbox.text, destinationtextbox.text); } public void extractpage(string sourcepath, string outputpath) { int startpage = 1; pdfreader objreader = new pdfreader(sourcepath+".pdf"); int endpage = 6; document objdocument = new document(objreader.getpagesizewithrotation(startpage)); objdocument.open(); (int index = startpage; index <= endpage; index++) { pdfcopy pdfcopyprovider = new pdfcopy(objdocument, new filestream(outputpath+""+index+".pdf", filemode.create)); pdfimportedpage importedpage = pdfcopyprovider.getimportedpage(objreader, index); pdfcopyprovider.addpage(importedpage); } objdocument.close(); objreader.close(); messagebox.show(@"splitting successful!"); } } }
but throwing null reference pointer exception. can't figure out problem i've caused.
any appreciated. in advance.
this extracts page without exception.
public void extractpage(string sourcepdfpath, string outputpdfpath, int pagenumber) { pdfreader reader = null; document document = null; pdfcopy pdfcopyprovider = null; pdfimportedpage importedpage = null; try { // intialize new pdfreader instance contents of source pdf file: reader = new pdfreader(sourcepdfpath); // capture correct size , orientation page: document = new document(reader.getpagesizewithrotation(pagenumber)); // initialize instance of pdfcopyclass source // document , output file stream: pdfcopyprovider = new pdfcopy(document, new system.io.filestream(outputpdfpath, system.io.filemode.create)); document.open(); // extract desired page number: importedpage = pdfcopyprovider.getimportedpage(reader, pagenumber); pdfcopyprovider.addpage(importedpage); document.close(); reader.close(); } catch (exception ex) { throw ex; } }
Comments
Post a Comment