Iterating over presentation elements

Hi-



Does Aspose Slides provide a way to iterate over all the objects in a presentation in order to get access to the fonts objects are using?



My goal is to change a font in the presentation to a supported font if a unsupported font is found.



Thanks!

I tried the following w/o success.



Presentation pptPresentation = new Presentation(in);

Fonts f = pptPresentation.getFonts();



for (int i = 0; i
FontEntity fe = f.get(i);



String fontName = fe.getFontName();

fe.setFontName(“Wingdings”);

}

pptPresentation.save(out, com.aspose.slides.export.SaveFormat.PPT, pdfOptions);

Hi,


Please follow the link below that will help you to access the text in presentation slides texframes on portion level. You can then use the getFontindex() property to get the font index used in particular portion and can verify the font name using the returned index in FontEntity collection. You can also set new font index to portion text as well. Please share if I may help you further in this regard.

Aspose.Total for Java|Documentation


Many Thanks,

I tred the code and was able to iterate over the elements, but setting the font didn’t work. My code:



if (paras != null) {

int parasCount = paras.size();

for (int paraIdx = 0; paraIdx
Paragraph para = paras.get(paraIdx);

// print the text on console

Portions p = para.getPortions();



// added this code to set font to index 4, which is arial



for (Portion portion : p) {

portion.setFontIndex(4);

}

}



Thanks,

Dave





pptPresentation.save(out, com.aspose.slides.export.SaveFormat.PDF, pdfOptions);

Hi Dave,


Please share the complete sample code reproducing the issue along with source presentation. I will carry out investigation on my end to help you out further.

Many Thanks,

Code below & ppt attached. Note the fonts in the pdf end up being Times. None are Arial.





com.aspose.slides.export.PdfOptions pdfOptions = new com.aspose.slides.export.PdfOptions();

// Set Jpeg Quality

pdfOptions.setJpegQuality(60);

// Define behavior for metafiles

pdfOptions.setSaveMetafilesAsPng(true);

// Set Text Compression level

pdfOptions.setTextCompression(com.aspose.slides.export.PdfTextCompression.FLATE);

// Define the PDF standard

pdfOptions.setCompliance(com.aspose.slides.export.PdfCompliance.PDF_15);

File thingFolder = LC3Environment.getThingDirectory(thingID, domain);

out = new FileOutputStream(new File(thingFolder, thingFile.getName() + “.pdf”));

// Instantiate a Presentation object that represents a PPT file

if (thingFile.getName().toLowerCase().endsWith(“ppt”)) {

Presentation pptPresentation = new Presentation(in);

Fonts f = pptPresentation.getFonts();



int arial = -1;

for (int i = 0; i
FontEntity fe = f.get(i);



String fontName = fe.getFontName();

if (fe.getFontName().equalsIgnoreCase(“arial”)) {

arial = i;

// break;

}

}



int lastSlidePosition = pptPresentation.getSlides().getLastSlidePosition();

for (int pos = 1; pos <= lastSlidePosition; pos++) {

Slide sld = pptPresentation.getSlideByPosition(pos);

// iterate all shapes

int shapesCount = sld.getShapes().size();

for (int shpIdx = 0; shpIdx < shapesCount; shpIdx++) {

com.aspose.slides.Shape shp = sld.getShapes().get(shpIdx);

// Get the paragraphs from textholder or textframe



Paragraphs paras = null;



// Check if shape holds a textholder

if (shp.getPlaceholder() != null && shp.isTextHolder() == true) {

// Get the place holder as an Object instance

Object obj = shp.getPlaceholder();

// First type of place holder. It is TextHolder

if (obj instanceof TextHolder) {

// Cast object into TextHolder object

TextHolder txtHolder = (TextHolder) obj;

paras = txtHolder.getParagraphs();

// iterateParagraphs(paras, portionList);

} else if (obj instanceof Placeholder) // Second type of place holder(Shape)

{

// Cast the object into Placeholder object

Placeholder placeHolder = (Placeholder) obj;

// getShapeRef() returns the Shape object which contains real properties of a Placeholder.

paras = placeHolder.getShapeRef().getTextFrame().getParagraphs();

// iterateParagraphs(paras, portionList);

}

} else {

if (shp.getTextFrame() != null) {

paras = shp.getTextFrame().getParagraphs();

}// if

}// else

// Print the text on Console

if (paras != null) {

int parasCount = paras.size();

for (int paraIdx = 0; paraIdx < parasCount; paraIdx++) {

Paragraph para = paras.get(paraIdx);

// print the text on console

Portions p = para.getPortions();

for (Portion portion : p) {

portion.setFontIndex(4);

}

}

}// end if

}// end for



}// end for

pptPresentation.save(out, com.aspose.slides.export.SaveFormat.PDF, pdfOptions);

Hi Dave,


I have used your following code using Aspose.Slides for Java 2.8.0 and have not been able to observe the issue. I have been able to generate the PDF with “Times New Roman” and “Arial” fonts separately. For your kind reference, I have shared the two separate PDFs with “Times New Roman” and “Arial” fonts. Please download the shared version in the attachments from here and if there is still issue please share with us.

public static void ChangeFont()throws Exception
{
com.aspose.slides.export.PdfOptions pdfOptions = new com.aspose.slides.export.PdfOptions();
// Set Jpeg Quality
pdfOptions.setJpegQuality(60);
// Define behavior for metafiles
pdfOptions.setSaveMetafilesAsPng(true);
// Set Text Compression level
pdfOptions.setTextCompression(com.aspose.slides.export.PdfTextCompression.FLATE);
// Define the PDF standard
pdfOptions.setCompliance(com.aspose.slides.export.PdfCompliance.PDF_15);
//File thingFolder = LC3Environment.getThingDirectory(thingID, domain);
//out = new FileOutputStream(new File(thingFolder, thingFile.getName() + “.pdf”));
// Instantiate a Presentation object that represents a PPT file
//if (thingFile.getName().toLowerCase().endsWith(“ppt”))
//{
String path=“C:/Users/Mudassir/Downloads/demo.ppt/”;
//Presentation pptPresentation = new Presentation(in);
com.aspose.slides.Presentation pptPresentation = new Presentation(path+“demo.ppt”);
Fonts f = pptPresentation.getFonts();

int arial = -1;
for (int i = 0; i<f.size();i++)
{
com.aspose.slides.FontEntity fe = f.get(i);

String fontName = fe.getFontName();
if (fe.getFontName().equalsIgnoreCase(“arial”))
{
arial = i;
// break;
}
//}
}

int lastSlidePosition = pptPresentation.getSlides().getLastSlidePosition();
for (int pos = 1; pos <= lastSlidePosition; pos++)
{
Slide sld = pptPresentation.getSlideByPosition(pos);
// iterate all shapes
int shapesCount = sld.getShapes().size();
for (int shpIdx = 0; shpIdx < shapesCount; shpIdx++)
{
com.aspose.slides.Shape shp = sld.getShapes().get(shpIdx);
// Get the paragraphs from textholder or textframe

Paragraphs paras = null;

// Check if shape holds a textholder
if (shp.getPlaceholder() != null && shp.isTextHolder() == true)
{
// Get the place holder as an Object instance
Object obj = shp.getPlaceholder();
// First type of place holder. It is TextHolder
if (obj instanceof TextHolder)
{
// Cast object into TextHolder object
TextHolder txtHolder = (TextHolder) obj;
paras = txtHolder.getParagraphs();
// iterateParagraphs(paras, portionList);
}
else if (obj instanceof Placeholder) // Second type of place holder(Shape)
{
// Cast the object into Placeholder object
Placeholder placeHolder = (Placeholder) obj;
// getShapeRef() returns the Shape object which contains real properties of a Placeholder.
paras = placeHolder.getShapeRef().getTextFrame().getParagraphs();
// iterateParagraphs(paras, portionList);
}
}
else
{
if (shp.getTextFrame() != null)
{
paras = shp.getTextFrame().getParagraphs();
}// if
}// else
// Print the text on Console
if (paras != null)
{
int parasCount = paras.size();
for (int paraIdx = 0; paraIdx < parasCount; paraIdx++)
{
Paragraph para = paras.get(paraIdx);
// print the text on console
Portions p = para.getPortions();
for (Portion portion : p)
{
portion.setFontIndex(arial);
// portion.setFontIndex(0);
}
}
}// end if
}// end for

}// end for
pptPresentation.save(path+“ArialPdf.pdf”, com.aspose.slides.export.SaveFormat.PDF, pdfOptions);
}

Many Thanks,

Hi -

Where can I get slides 2.8, doesn’t seem to have been released.

I’m using 2.7.0.


Dave

Hi Dave,

I have already shared the link to Aspose.Slides for Java 2.8.0 in my provious post. Please download Aspose.Slides for Java 2.8.0 in Jar File.rar shared here.

Many Thanks,

I tried 2.8 without success. All the fonts are Times. Note, I’m running on a Mac.

Hi Dave,


I have created an issue with ID SLIDESJAVA-33141 in our issue tracking system to further investigate and resolve the issue if it is with Aspose.Slides for Java iin Mac environment. But I would request you to kindly confirm that whether “Arial” font is installed in your Mac Os or not. This can also be the cause of issue. I will share the further information with you as soon as it is shared by our development team.

Many Thanks,

I’d like to get a clarification on how the slides code flows.



I’ve seen two use cases.



1. I’ve seen fonts replaced with Times New Roman in the backend slides code. This is without using the test font replacement code.



2. I’ve seen an exception when a font is missing.



Question - What case does aspose replace a font and in what case is an exception thrown?



Thanks!

Hi,


In my opinion, Aspose.Slides for Java looks for desired font in installed place. If the font is missing then it replaces the font with Arial if it is normal font. if default font is missing then it throws exception. This is some thing that I have observed in Windows environment. However, in case of Linux or Mac, I have observed that if any True Type font that has been used in presentation is missing then Aspose.Slides throws exception for this. We are investigating your issue and will share the further information with you once it is resolved.

Many Thanks,

The last reply to this was not definitive. Can I get a reply as to exactly how it works?

Thanks!

Hi,


I have observed in my previous observation that in Windows environment the things worked fine for me. I may suggest you to please try copying all the True Type fonts from Windows in your desired environment using the guide lines shared here. This thing worked for one of customers and if your issue seems related to shared link, things may work for you as well.

Now, coming to previous post explanation. Aspose.Slides substitute the missing fonts with Arial. If the default font is even not present then exception will be raised. Please try the guidelines share above and might be things start to work on your end.

Many Thanks,

Thanks for the reply. We have installed the fonts.

Our observations have been that if a font is missing we get Times New Roman.

One more question. Is there a way to prevent the substitution and get the exception thrown if a font is missing?


Dave

Hi Dave,


In my view, if default fonts like Arial and Times New Roman are not present along with actually used fonts, the exception will be thrown in that case. I don’t feel there is any other alternate to substitution available.

Many Thanks,

The issues you have found earlier (filed as SLIDESJAVA-33172) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

@otmfit,

One of these articles will be helpful:
Default Fonts
Font Replacement
Font Substitution
Fallback Font

Please, check the following article:
Getting Warning Callbacks for Fonts Substitution in Aspose.Slides