Free Support Forum - aspose.com

How to export one page to PDF

Hello, I have question how to export to PDF one page only. I know how to export whole section but I’m interested in exporting one page only.

Post processing PDF after export will be a weak solution - User is selecting page and I will need to parse whole section find it and match it to select that desired page.

Thanks in advance for any help.

Hi Artur,


Thank you for writing to Aspose Support team.

You can use the PdfSaveOptions.PageIndex and PageCount properties to export only a single page to pdf. For example, if you want to export the second page, please use the following sample code to achieve this.

Sample Code:

// Load the document into Aspose.Note.
Document oneFile = new Document(“Aspose.one”);

// Initialize PdfSaveOptions object
PdfSaveOptions opts = new PdfSaveOptions();

// Set page index
opts.PageIndex = 1;

// Set page count
opts.PageCount = 1;

dataDir = dataDir + “SaveRangeOfPagesAsPDF_out_.pdf”;

// Save the document as PDF
oneFile.Save(dataDir, opts);

Thanks for Your help, I have one more question - how can I get page index from page object ?

Context:
I’m iterating through pages in section and I want to save each page as separate PDF.
Let’s assume that I’m iterating in random order so my reference to current page is only by page object.

Hi Artur,


Page itself does not contain the index and it won’t be possible to achieve it like this. However, you can enumerate through collection of pages and compare object reference, like this:

var index = 0;
foreach (var page in document)
{
if (object.ReferenceEquals(page, myPage))
{
document.Save(“out.pdf”, new PdfSaveOptions() { PageIndex = index, PageCount = 1 });
break;
}

++index;
}

You can also iterate pages “in random order” in your own collection which is not possible otherwise as these pages are ordered in Document. So he can use this code sample inside his loop. Of course, this will be inefficiently due to nested loop. You user can fill your own mapping, let’s say, IDictionary<object, int index>, before “looping in random order”, and get an index from this mapping.