I am getting error while saving the doc to a file stream in a pdf format…
Please help me out
Code is
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.Pdf);
Error is Invalid save format
Hi Hafsa,
Hey there, okay i am attaching the file here…
But to tell you, my file won’t be definite in my application as i am making an application for users to print… where their provided file will be converted into pdf before printing… so the file can be any doc file… here i was testing with this sample file…
Hi Hafsa,
Document doc = new Document(getMyDir() + “template.doc”);ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.PDF);
Hey there, can you please answer all of my solutions to the following problems one by one. Thank you
1. While changing the orientation of slides, i am using the below code but the orientation is not being changed.
private static IPresentation setOrientationSlides(IPresentation pres,String orient) {
String CurrentOrient="";
ISlideSize h=pres.getSlideSize();
double H= h.getSize().getHeight();
double W= h.getSize().getWidth();
if (H>W)
{
CurrentOrient=“Portrait”;
}
else
{
CurrentOrient=“Landscape”;
}
if (!CurrentOrient.equals(orient))
{
pres.getSlideSize().setType(SlideSizeType.Custom);
pres.getSlideSize().getSize().setSize(H,W);;
}
return pres;
}
2. While changing the papersize of pdf document, i am using the below function but no change in the papersize is accounted. It remains the same.
private static com.aspose.pdf.Document SetPaperSizePdf(com.aspose.pdf.Document pdf1,String ps) {
if (ps.equals(“A0”))
{
pdf1.getPageInfo().setWidth(PageSize.A0_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A0_HEIGHT);
}if (ps.equals(“A1”))
{
pdf1.getPageInfo().setWidth(PageSize.A1_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A1_HEIGHT);
}if (ps.equals(“A2”))
{
pdf1.getPageInfo().setWidth(PageSize.A2_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A2_HEIGHT);
}if (ps.equals(“A4”))
{
pdf1.getPageInfo().setWidth(PageSize.A4_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A4_HEIGHT);
}if (ps.equals(“A5”))
{
pdf1.getPageInfo().setWidth(PageSize.A5_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A5_HEIGHT);
}if (ps.equals(“A6”))
{
pdf1.getPageInfo().setWidth(PageSize.A6_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A6_HEIGHT);
}
if (ps.equals(“Ledger”))
{
pdf1.getPageInfo().setWidth(PageSize.LEDGER_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LETTER_HEIGHT);
}
if (ps.equals(“Legal”))
{
pdf1.getPageInfo().setWidth(PageSize.LEGAL_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LEGAL_HEIGHT);
}
if (ps.equals(“Letter”))
{
pdf1.getPageInfo().setWidth(PageSize.LETTER_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LETTER_HEIGHT);
}
return pdf1;
}
3. Similarly, the margins of the pdf are not changed by the following code.
private static com.aspose.pdf.Document SetMarginsPdf(com.aspose.pdf.Document pdf1, String margins) {
StringTokenizer tokenizer = new StringTokenizer(margins, “,”);
double pmLeft = Double.parseDouble(tokenizer.nextToken());
double pmRight = Double.parseDouble(tokenizer.nextToken());
double pmTop = Double.parseDouble(tokenizer.nextToken());
double pmBottom = Double.parseDouble(tokenizer.nextToken());
com.aspose.pdf.MarginInfo marginInfo = new com.aspose.pdf.MarginInfo();
marginInfo.setTop((float) ConvertUtil.inchToPoint(pmTop));
marginInfo.setBottom((float) ConvertUtil.inchToPoint(pmBottom));
marginInfo.setLeft((float) ConvertUtil.inchToPoint(pmLeft));
marginInfo.setRight((float) ConvertUtil.inchToPoint(pmRight));
pdf1.getPageInfo().setMargin(marginInfo);
return pdf1;
}
4. Orientation of the pdf is also not changed by this code.
private static com.aspose.pdf.Document SetOrientationPdf(com.aspose.pdf.Document pdf1, String orient)
{
if (orient.equals(“Portrait”))
{
///do nothing
}
if (orient.equals(“Lanscape”))
{
pdf1.getPageInfo().setLandscape(true);
}
return pdf1;
}
5. Is there any way to take a aspose.words.document file and then save specific pages and delete the rest of the other ones. Means that we can give a range in aspose words document. While searching, I read that word document is a flow document where you cannot detect the next page. Help me with this.
Please answer all questions separately, Thankyou so much.
Hafsa:
5. Is there any way to take a aspose.words.document file and then save specific pages and delete the rest of the other ones. Means that we can give a range in aspose words document. While searching, I read that word document is a flow document where you cannot detect the next page. Help me with this.
Document doc = new Document("C:\\Temp\\in.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);
Okay i am waiting for the reply of these all queries. Please do hurry as I have 15 days of my trial left and I have to complete my service to see if Aspose works for me or not.
Thank you to the Aspose team.
Hi Hafsa,
Can you please also attach your PowerPoint presentation and PDF file for investigation?
Best Regards,
Hey there my pdf or powerpoint document wont be any particular because i am making a print service and the user can give any document of any size.
Hafsa101:
2. While changing the papersize of pdf document, i am using the below function but no change in the papersize is accounted. It remains the same.
private static com.aspose.pdf.Document SetPaperSizePdf(com.aspose.pdf.Document pdf1,String ps) {
if (ps.equals("A0"))
{
pdf1.getPageInfo().setWidth(PageSize.A0_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A0_HEIGHT);
}if (ps.equals("A1"))
{
pdf1.getPageInfo().setWidth(PageSize.A1_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A1_HEIGHT);
}if (ps.equals("A2"))
{
pdf1.getPageInfo().setWidth(PageSize.A2_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A2_HEIGHT);
}if (ps.equals("A4"))
{
pdf1.getPageInfo().setWidth(PageSize.A4_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A4_HEIGHT);
}if (ps.equals("A5"))
{
pdf1.getPageInfo().setWidth(PageSize.A5_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A5_HEIGHT);
}if (ps.equals("A6"))
{
pdf1.getPageInfo().setWidth(PageSize.A6_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.A6_HEIGHT);
}
if (ps.equals("Ledger"))
{
pdf1.getPageInfo().setWidth(PageSize.LEDGER_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LETTER_HEIGHT);
}
if (ps.equals("Legal"))
{
pdf1.getPageInfo().setWidth(PageSize.LEGAL_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LEGAL_HEIGHT);
}
if (ps.equals("Letter"))
{
pdf1.getPageInfo().setWidth(PageSize.LETTER_WIDTH);
pdf1.getPageInfo().setHeight(PageSize.LETTER_HEIGHT);
}
return pdf1;
}
Hafsa101:
3. Similarly, the margins of the pdf are not changed by the following code.
private static com.aspose.pdf.Document SetMarginsPdf(com.aspose.pdf.Document pdf1, String margins) {
StringTokenizer tokenizer = new StringTokenizer(margins, ",");
double pmLeft = Double.parseDouble(tokenizer.nextToken());
double pmRight = Double.parseDouble(tokenizer.nextToken());
double pmTop = Double.parseDouble(tokenizer.nextToken());
double pmBottom = Double.parseDouble(tokenizer.nextToken());
com.aspose.pdf.MarginInfo marginInfo = new com.aspose.pdf.MarginInfo();
marginInfo.setTop((float) ConvertUtil.inchToPoint(pmTop));
marginInfo.setBottom((float) ConvertUtil.inchToPoint(pmBottom));
marginInfo.setLeft((float) ConvertUtil.inchToPoint(pmLeft));
marginInfo.setRight((float) ConvertUtil.inchToPoint(pmRight));
pdf1.getPageInfo().setMargin(marginInfo);
return pdf1;
}
Hafsa101:
4. Orientation of the pdf is also not changed by this code.
private static com.aspose.pdf.Document SetOrientationPdf(com.aspose.pdf.Document pdf1, String orient)
{
if (orient.equals("Portrait"))
{
///do nothing
}
if (orient.equals("Lanscape"))
{
pdf1.getPageInfo().setLandscape(true);
}
return pdf1;
}
Hi Hafsa,
For PowerPoint presentations, you can use the following code.
pres.getSlideSize().setOrientation(SlideOrienation.Portrait);
You can also use getOrientation method to get orientation of a presentation.
Best Regards,
Hey first of all, the SectionSplitter.java file was giving error of java.lang.iterable error for the foreach loop. I changed the loop to simple for loop but still the error is there as when i try to access a page, there comes a NullPointerException in SectionSplitter.java
I am attaching my modified SectionSplitter.java file and the code i am using is
Document pageDoc = splitter.getDocumentOfPage(arr[j]);
Doc2.appendDocument(pageDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
One more thing i want to ask is that is there a way to append the document in another document starting with a new page…
Hey i was able to run my section splitter and all the get page of document code by making some changes with the foreach loops.
Now the problem i am facing is that, the function is grabbing pages according to the sections, not the page break…
E.g
If i have this above attached document (Test1.doc)
and i try to get its page 2 by Document pageDoc = splitter.getDocumentOfPage(2);
then the resultant document is Result1.doc…
I have attached this document too.
You will see that the function gets the document from the first paragraph section of Page 2 to the ending of the last mentioned paragraph of page 3. This is wrong, my desired result is the attached document by name Result-Wanted.doc
Thus tell me a way to completely cut the document by page breaks, not the starting or ending sections…
And also can you tell me a way to delete a desired indexed page from a document. Thankyou!
More my updated classes of
SectionSplitter, PageNumberFinder, DocumentPageSplitter are attached int he zip file
Hi Hafsa,
Document doc = new Document(getMyDir() + “Test1.doc”);Document finalDoc = (Document)doc.deepClone(true);
finalDoc.removeAllChildren();PageFinder finder = new PageFinder(doc);
finder.splitNodesAcrossPages(true);
ArrayList pageSections = finder.retrieveAllNodesOnPages(2, 2, NodeType.SECTION);for (Section section : (Iterable<Section>) pageSections)
finalDoc.appendChild(finalDoc.importNode(section, true));finalDoc.save(getMyDir() + “out.doc”);
According to the 4th query , you said to use the setPageSize Method to change orientation. the thing is that , I can interchange the width to height and same…
But when my original document is in Portrait and i interchange the width and height for landscape mode. the error is that the data is lost…
I have explained my problem through attached example documents.
please review them
and my code is
private static com.aspose.pdf.Document SetOrientationPdf(com.aspose.pdf.Document pdf1, String orient)
{
double height=pdf1.getPageInfo().getHeight();
double width=pdf1.getPageInfo().getWidth();
String currentOrient="";
if (height>width)
{
currentOrient=“Portrait”;
}
else
{
currentOrient=“Landscape”;
}
if (!orient.equals(currentOrient))
{
com.aspose.pdf.PageCollection pageCollection = pdf1.getPages();
for (int i=1;i<=pageCollection.size();i++)
{
com.aspose.pdf.Page pdfPage = pageCollection.get_Item(i);
pdfPage.setPageSize(height,width);
}
}
return pdf1;
}
Hi there thankyou for the concern,
i checked your code and its giving these error
at Test.PageSplitter.visitParagraphEnd(PageSplitter.java:125)
at com.aspose.words.Paragraph.zzY(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Paragraph.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Section.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Document.accept(Unknown Source)
at Test.PageFinder.splitNodesAcrossPages(PageFinder.java:165)
at Test.Test1.main(Test1.java:154)
And i had to change the files a bit for the loops from foreach to for loops. Because they give java lang object iteration error…
Hafsa101: According to the 4th query, you said to use the
setPageSize
method to change orientation. However, when my original document is in Portrait and I interchange the width and height for landscape mode, the data is lost.I have explained my problem through attached example documents.
Please review them.
My code is:private static com.aspose.pdf.Document SetOrientationPdf(com.aspose.pdf.Document pdf1, String orient) { double height = pdf1.getPageInfo().getHeight(); double width = pdf1.getPageInfo().getWidth(); String currentOrient = ""; if (height > width) { currentOrient = "Portrait"; } else { currentOrient = "Landscape"; } if (!orient.equals(currentOrient)) { com.aspose.pdf.PageCollection pageCollection = pdf1.getPages(); for (int i = 1; i <= pageCollection.size(); i++) { com.aspose.pdf.Page pdfPage = pageCollection.get_Item(i); pdfPage.setPageSize(height, width); } } return pdf1; }
Hi Hafsa,
Thanks for sharing the details.
I have tested the scenario and I am able to reproduce the same problem. For the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34640. We will investigate this issue in detail and will keep you updated on the status of a correction.
Apologies for the inconvenience.
Hi there… Anything done for fixing my issue of getting a specific page from the Word document… ???
Hey is there any thing done yet for issue of pdf document size changing