We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose word : Getting one page of a word document

Hello,

I am using aspose word 13.9, to save a certain page from a word document as image, i was using this code:

ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
pageIndex = pageIndex-1;
options.setPageIndex(pageIndex);
options.setResolution(200);
wordDocument.save(imageStream, options);

But now i need to save a certain page from a word document as a document.
But i am not able to find an option setPageIndex inside DocSaveOptions.

Can you advise please,

Thank you

Hi Karine,


Thanks for your inquiry. In order to save all pages in Word document to separate JPEG image files, please use the following code snippet:
Document doc = new Document(“C:\Temp\HeaderFooter+Coordinates.docx”);

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setPageCount(1);

// Save each page of the document as Jpeg.
for (int i = 0; i < doc.getPageCount(); i++)
{
options.setPageIndex(i);
doc.save(“C:\Temp\out_” + i + “.jpg”, options);
}

I hope, this helps.

Best regards,

Hello
Thank you for your reply,

It seems i didn’t clarify my request,

I need to save a certain page for example page 12 from a word document as a separate word document not as image.

I need to do something like:

Document doc = new Document(“C:\Temp\HeaderFooter+Coordinates.docx”);

DocSaveOptions options = new DocSaveOptions(SaveFormat.DOC);

//how to set in the options the page index taht i need to save as separate doc file??
doc.save(“C:\Temp\onePageDoc.doc”, options);

Hi Karine,


Thanks for the additional information. We are working over your query and will get back to you soon.

Best regards,

Thank you,
I will be waiting the solution,
Do you have an idea on when you can get back to me?
Because we are working on a project and we need to set a delivery date.

Thank you again for your help,

Hi Karine,


Thanks for being patient. Please see the attached classes and try executing the following code:
Document doc = new Document(“C:\Temp\in.doc”);
LayoutCollector layoutCollector = new LayoutCollector(doc);
doc.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
Document pageDoc = splitter.GetDocumentOfPage(12);
pageDoc.save(“C:\Temp\out.doc”);
I hope, this helps.

Best regards,

hello,
thanks for your reply,

i have tried the following:

LayoutCollector layoutCollector = new LayoutCollector(wordDocument);
wordDocument.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
Document pageDoc = splitter.GetDocumentOfPage(pageIndex);
pageDoc.getCount();(this returns 1);
pageDoc.save(“c:/new.doc”);

the output document new.doc contains the whole document i was trying to read only one page from,
am i using it wrong? or am i missing anything?


Hi Karine,


Thanks for your inquiry. Please attach a sample Word document you’re getting this problem with here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

please find attached a sample of a simple word document i am working on,
thank you,

Hi Karine,


Thanks for your inquiry. I have attached a new version of “pagesplitter”. I hope, this helps.

Best regards,

Thank you for this new version of PageSplitter,
i have tried it on the document i sent earlier and it worked perfectly,
but I’m afraid it doesn’t work on just any document,
please find attached a document i have been testing on and i got the following exception:
java.lang.ClassCastException: com.aspose.words.Table cannot be cast to com.aspose.words.Paragraph
is there any way to solve this problem?

Thanks again,

Hi Karine,


Thanks for your inquiry. We are working over your query and will get back to you soon.

Best regards,

Hi Karine,


Thanks for being patient. Please try the attached “SectionSplitter.java” class. I hope, this helps.

Best regards,

Hi Awais,

Thanks for your reply and the effort put into this matter,
Is there any example on how to use this class?

Thanks in advance,

Hi Karine,


Thanks for your inquiry. Please use “SectionSplitter.java” along with “PageNumberFinder.java” and “DocumentPageSplitter.java” classes which I sent to you earlier (please refer to my previous replies). Also, you can find the sample usage here. I hope, this helps.

Best regards,

Thank you for your reply,
The new package seems to be working fine,
But i’m afraid there is still one problem,
the original document is 23 pages, when i try to switch pages,
The returning document is 28 pages, and if i do the same action again,
The document becomes of 34 pages,
Any advice?

Thank you,

Hi Karine,


Thanks for your inquiry. Please share complete source code (in runnable form) to be able to reproduce this issue on our end. We will investigate the issue on our end and provide you more information.

Best regards,

LayoutCollector layoutCollector = new LayoutCollector(wordDocument);
wordDocument.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
Document pageDoc = splitter.getDocumentOfPage(pageIndex);
DocSaveOptions options = new DocSaveOptions(SaveFormat.DOC);
pageDoc.save(imageStream, options);
//or even the output document in:
// pageDoc.save(“c:/new.doc”);

Hi Karine,


Thanks for your inquiry. But, while using the latest version of Aspose.Words for Java 14.4.0, I managed to successfully extract 6th page of ecm.docx for example.
Document doc = new Document(“C:\Temp\ecm.docx”);
LayoutCollector layoutCollector = new LayoutCollector(doc);
doc.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
Document pageDoc = splitter.getDocumentOfPage(6);
DocSaveOptions options = new DocSaveOptions(SaveFormat.DOC);
pageDoc.save(“C:\Temp\out.doc”, options);
What do you mean you’re getting problems when switching pages? Please provide complete steps so that we can reproduce this issue on our end.

Best regards,

Hello,

In my case i have the previously attached document,
I need to switch any 2 pages in that document, the application requires this business feature,
so i’m using the code to loop on all the pages in that document, and write the output to an array of bytes as follows:

LayoutCollector layoutCollector = new LayoutCollector(wordDocument);
wordDocument.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
Document pageDoc = splitter.getDocumentOfPage(pageIndex);
DocSaveOptions options = new DocSaveOptions(SaveFormat.DOC);
pageDoc.save(imageStream, options);
byte[] array = imageStream.toByteArray();

then every time add the bytes to a vector:
vectBytes<byte[]>.add(bytes);

then write the output bytes:
for (byte[] b : vectBytes) {
System.arraycopy(b,0,allBytes,0, b.length);
}

so if i have a document of 4 pages i need to switch the first 2:
i read the document by this order of page indexes: 2, 1, 3, 4, then do the above and get a new document with the first 2 pages switched,

but the problem in this case is: pages are added the document for exp:
if have a document of 4 pages and i need to switch the first 2,
when the process is over the first 2 pages are switched, but the document becomes of 6 pages,
some of texts are stretched and they do not fit in the page anymore, therefor a new page is added with a few lines or even empty sometimes,
if you need anymore information do not hesitate to contact me,

thanks in advance for your help,