Finally i have solved my problem. The PPTX i got was developed in mac os. So what i did is i just opened a working pptx file
. And copied all the contents of not working pptx into working pptx
and saved it by the name of not working pptx.
The document cannot be opened because there is an invalid part with an unexpected content type
-
29-06-2022 - |
Domanda
I am getting a error while opening using a presentation (PPTX files) creation code. Code i am using is given below:
public static void UpdatePPT()
{
const string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main";
const string drawingmlNamespace = "http://schemas.openxmlformats.org/drawingml/2006/main";
string fileName = Server.MapPath("~/PPT1.pptx"); //path of pptx file
using (PresentationDocument pptPackage = PresentationDocument.Open(fileName, true))
{
} // Using pptPackage
}
and the error i am getting is:
"The document cannot be opened because there is an invalid part with an unexpected content type.
[Part Uri=/ppt/printerSettings/printerSettings1.bin],
[Content Type=application/vnd.openxmlformats-officedocument.presentationml.printerSettings],
[Expected Content Type=application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings]."
error occurs at using (PresentationDocument pptPackage = PresentationDocument.Open(fileName, true))
Code works fine for many PPTX files. But it is throwing this error on some files. I am not able to find any solution. Thanks for your help.
Soluzione 2
Altri suggerimenti
Old post, but I ran in to the same problem. I solved it programatically. Means:
My code runs using (var document = PresentationDocument.Open(fileName, true))
If this run into a exception I have a document like described. Then I call FixPowerpoint()
method and do the other stuff after again.
Here is the method to share (using System.IO.Packaging
):
private static void FixPowerpoint(string fileName)
{
//Opening the package associated with file
using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
{
//Uri of the printer settings part
var binPartUri = new Uri("/ppt/printerSettings/printerSettings1.bin", UriKind.Relative);
if (wdPackage.PartExists(binPartUri))
{
//Uri of the presentation part which contains the relationship
var presPartUri = new Uri("/ppt/presentation.xml", UriKind.RelativeOrAbsolute);
var presPart = wdPackage.GetPart(presPartUri);
//Getting the relationship from the URI
var presentationPartRels =
presPart.GetRelationships().Where(a => a.RelationshipType.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (presentationPartRels != null)
{
//Delete the relationship
presPart.DeleteRelationship(presentationPartRels.Id);
}
//Delete the part
wdPackage.DeletePart(binPartUri);
}
wdPackage.Close();
}
}