This demo illustrates how to Extract Pages from a Pdf document, based on user selection. Using Extract method in PdfFileEditor class of Aspose.Pdf.Kit component, developers can extract any number of pages from a PDF document and save those pages as a new PDF document. Fore more information, please visit Extract Pages from a PDF Document. Click Extract to see how demo extracts chosen pages from a pdf document enabling user to download results. Pages chosen from Pdf file. page 1 of filepage 2 of filepage 3 of filepage 4 of filepage 5 of file pages from Pdf file1 into a new Pdf file.
Click Extract to see how demo extracts chosen pages from a pdf document enabling user to download results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<%@ Page language="c#" Codebehind="Extract.aspx.cs" AutoEventWireup="false" Inherits="Aspose.Pdf.Kit.Demos.Extract" MasterPageFile="~/tpl/Demo.Master" Title="Extract Pages from Pdf - Aspose.Pdf.Kit Demos" %> <asp:Content runat="server" ContentPlaceHolderID="MainContent"> <table width="90%" align="center" cellspacing="0" cellpadding="0" border="0"> <tr> <td width="19"><img src="../../../../Common/images/heading_lft.jpg" alt="" width="19" height="41" /></td> <td width="100%" class="demos-heading-bg"><h2 class="demos-heading-bg"> Extract Pages from PDF - Aspose.Pdf.Kit</h2></td> <td width="19"><img src="../../../../Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td> </tr> <tr> </tr> </table> <p class="MsoNormal"><font size=2 face="Arial" ><span style="color:black" >This demo illustrates how to <b> Extract </b> Pages from a Pdf document, based on user selection. <br><br>Using <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffileeditor.extract_overloads.html> Extract </a> method in <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffileeditor.html> PdfFileEditor</a> class of Aspose.Pdf.Kit component, developers can extract any number of pages from a PDF document and save those pages as a new PDF document. <br><br> Fore more information, please visit <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/extract-pages-from-a-pdf-document.html>Extract Pages from a PDF Document. </a><br><br><br></span> <p class="componentDescriptionTxt"> Click <b>Extract</b> to see how demo extracts chosen pages from a <a href="./resources/Aspose.Pdf.Kit.pdf">pdf document</a> enabling user to download results. </p> <asp:Label id="Label2" runat="server" >Pages chosen from Pdf file.</asp:Label><br /> <asp:CheckBoxList id="CheckBoxList1" CssClass="genericList" runat="server"> <asp:ListItem Value="1" Selected="True">page 1 of file</asp:ListItem> <asp:ListItem Value="2">page 2 of file</asp:ListItem> <asp:ListItem Value="3">page 3 of file</asp:ListItem> <asp:ListItem Value="4">page 4 of file</asp:ListItem> <asp:ListItem Value="5">page 5 of file</asp:ListItem> </asp:CheckBoxList> <asp:Button id="Button1" runat="server" ToolTip="Click to insert" Text="Extract"></asp:Button> pages from Pdf file1 into a new Pdf file. </asp:Content>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Aspose.Pdf.Kit.Demos { /// <summary> /// Summary of Extract . /// </summary> /// public partial class Extract : BasePage { //protected System.Web.UI.HtmlControls.HtmlGenericControl P1; protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1; //protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button1; //protected System.Web.UI.HtmlControls.HtmlGenericControl Font1; private void Page_Load(object sender, System.EventArgs e) { } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { //get number of page chosen from pdf file1 int[] pages; ArrayList pageList = new ArrayList(); for (int i=0; i<CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { pageList.Add(i+1); } } if(pageList.Count > 0) { pages = new int[pageList.Count]; pageList.CopyTo(pages); } else { return; } //input pdf file from which pages will be extracted string inputfile = GetResource("Aspose.Pdf.Kit.pdf"); // create an object of ExtractDemo class ExtractDemo extr = new ExtractDemo(); // call extract method of ExtractDemo class to extract pages from inputfile extr.Extract(inputfile,pages,Response); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
using System; using System.IO; using System.Web; using Aspose.Pdf.Kit; namespace Aspose.Pdf.Kit.Demos { /// <summary> /// Summary of ExtractDemo. /// </summary> public class ExtractDemo { public ExtractDemo() { #if SITE_BUILD // NOTE that in production you would want to call // new License().SetLicense("path-to-license-file") try { Aspose.Demos.Common.WebOperationsBridge.InitLicense(new License()); }catch { ; } #endif } public void Extract(string inputfile,int[] pages,HttpResponse response) { //read input Pdf documents FileStream inStream = File.Open(inputfile,FileMode.Open,FileAccess.Read,FileShare.Read); //new a PdfFileEditor object PdfFileEditor pdfEditor = new PdfFileEditor(); //generate new Pdf file output stream. MemoryStream outStream = new MemoryStream(); //Call Extract method of PdfFileEditor object to extract pages from input stream pdfEditor.Extract(inStream,pages,outStream); // create a byte array that will hold the output pdf byte[] outBuf = outStream.GetBuffer(); // specify the duration of time before a page,cached on a browser expires response.Expires = 0; // Specify the property to buffer the output page response.Buffer = true; // Erase any buffered HTML output response.ClearContent(); //Add a new HTML header and value to the response sent to the client response.AddHeader("content-disposition","inline; filename=" + "output.pdf"); // Specify the HTTP content type for response as Pdf response.ContentType = "application/pdf"; // Write specified information of current HTTP output to Byte array response.BinaryWrite(outBuf); // close the output stream outStream.Close(); //end the processing of the current page to ensure that no other HTML content is sent response.End(); } } }