none
Getting a corrupted Powerpoint file (OOXML and C#) please check my code below RRS feed

  • Question

  • using (PresentationDocument presentationDoc = PresentationDocument.Open(mtemplate_location, true))//open this file as editable
                {

                    // Get the relationship ID of the first slide.
                    PresentationPart part = presentationDoc.PresentationPart;
                    SlidePart sld = (SlidePart)part.GetPartById("rId2");
                    CommonSlideData commonSlideData1 = sld.Slide.CommonSlideData;
                    ShapeTree shapeTree1 = commonSlideData1.ShapeTree;
                    
                    NonVisualGroupShapeProperties nonVisualGroupShapeProperties1 = new NonVisualGroupShapeProperties();
                    NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" };
                    NonVisualGroupShapeDrawingProperties nonVisualGroupShapeDrawingProperties1 = new NonVisualGroupShapeDrawingProperties();
                    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();

                    nonVisualGroupShapeProperties1.Append(nonVisualDrawingProperties1);
                    nonVisualGroupShapeProperties1.Append(nonVisualGroupShapeDrawingProperties1);
                    nonVisualGroupShapeProperties1.Append(applicationNonVisualDrawingProperties1);

                    GroupShapeProperties groupShapeProperties1 = new GroupShapeProperties();

                    A.TransformGroup transformGroup1 = new A.TransformGroup();
                    A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
                    A.Extents extents1 = new A.Extents() { Cx = 0L, Cy = 0L };
                    A.ChildOffset childOffset1 = new A.ChildOffset() { X = 0L, Y = 0L };
                    A.ChildExtents childExtents1 = new A.ChildExtents() { Cx = 0L, Cy = 0L };

                    transformGroup1.Append(offset1);
                    transformGroup1.Append(extents1);
                    transformGroup1.Append(childOffset1);
                    transformGroup1.Append(childExtents1);

                    groupShapeProperties1.Append(transformGroup1);


                    //*********************************************************************
                    //Adding Dots
                    //*********************************************************************
                    dot_font_size = 700;
                    dot_font_color = "000000";
                    dot_label_font_type = "Arial";


                    if (analysis_type == "RM_Quest_Comp_Seg") //Dots are segments
                    {
                        //segment_labels_colors <Key= s_code, string = segment_dot_label | segment_color | segment_name | segment_legend_label>
                        foreach (var segment2 in segment_labels_colors)
                        {
                            //get segments to add data points
                            segment_array = segment2.Value.Split('|');

                            dot_color = segment_array[1];
                            //dot_label = "";  // Respondent maps do not use dot labels... segment_array[0];


                            for (int w = 0; w <= 999; w++)
                            {
                                //check we get data for the current segment_id
                                if (segment2.Key == pm_data_point[w, 4]) //Make sure the value belongs to the segment
                                {
                                    //inverted p and a values to draw dots correctly
                                    a = pm_data_point[w, 5];
                                    p = pm_data_point[w, 6];
                                    d = pm_data_point[w, 7];


                                    Shape shape1 = new Shape();

                                    NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
                                    NonVisualDrawingProperties nonVisualDrawingProperties2 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Oval 3" };
                                    NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new NonVisualShapeDrawingProperties();
                                    ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties2 = new ApplicationNonVisualDrawingProperties();

                                    nonVisualShapeProperties1.Append(nonVisualDrawingProperties2);
                                    nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
                                    nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties2);

                                    ShapeProperties shapeProperties1 = new ShapeProperties();

                                    A.Transform2D transform2D1 = new A.Transform2D();

                                    //Adjust the dot position according to the dot size = dominance
                                    if (d <= 1)
                                    {
                                        if (p <= 1) pleasure = 4544289L;
                                        if (p >= 9) pleasure = 8685615L;
                                        if (a <= 1) arousal = 5842400L;
                                        if (a >= 9) arousal = 1624015L;
                                    }
                                  

                                    dominance = 25000L * d; // 225000L biggest dot size


                                    A.Offset offset2 = new A.Offset() { X = pleasure, Y = arousal }; //Dot position
                                    A.Extents extents2 = new A.Extents() { Cx = dominance, Cy = dominance }; //Dot size

                                    transform2D1.Append(offset2);
                                    transform2D1.Append(extents2);

                                    A.PresetGeometry presetGeometry1 = new A.PresetGeometry() { Preset = A.ShapeTypeValues.Ellipse };
                                    A.AdjustValueList adjustValueList1 = new A.AdjustValueList();

                                    presetGeometry1.Append(adjustValueList1);

                                    A.SolidFill solidFill10 = new A.SolidFill();

                                    //Dot color
                                    A.RgbColorModelHex rgbColorModelHex1 = new A.RgbColorModelHex() { Val = dot_color };

                                    solidFill10.Append(rgbColorModelHex1);

                                    A.Outline outline1 = new A.Outline();
                                    A.NoFill noFill1 = new A.NoFill();

                                    outline1.Append(noFill1);

                                    shapeProperties1.Append(transform2D1);
                                    shapeProperties1.Append(presetGeometry1);
                                    shapeProperties1.Append(solidFill10);
                                    shapeProperties1.Append(outline1);

                                    ShapeStyle shapeStyle1 = new ShapeStyle();

                                    A.LineReference lineReference1 = new A.LineReference() { Index = (UInt32Value)2U };

                                    A.SchemeColor schemeColor10 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };
                                    A.Shade shade1 = new A.Shade() { Val = 50000 };

                                    schemeColor10.Append(shade1);

                                    lineReference1.Append(schemeColor10);

                                    A.FillReference fillReference1 = new A.FillReference() { Index = (UInt32Value)1U };
                                    A.SchemeColor schemeColor11 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };

                                    fillReference1.Append(schemeColor11);

                                    A.EffectReference effectReference1 = new A.EffectReference() { Index = (UInt32Value)0U };
                                    A.SchemeColor schemeColor12 = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };

                                    effectReference1.Append(schemeColor12);

                                    A.FontReference fontReference1 = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };
                                    A.SchemeColor schemeColor13 = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 };

                                    fontReference1.Append(schemeColor13);

                                    shapeStyle1.Append(lineReference1);
                                    shapeStyle1.Append(fillReference1);
                                    shapeStyle1.Append(effectReference1);
                                    shapeStyle1.Append(fontReference1);

                                    TextBody textBody1 = new TextBody();
                                    A.BodyProperties bodyProperties1 = new A.BodyProperties() { Wrap = A.TextWrappingValues.None, RightToLeftColumns = false, Anchor = A.TextAnchoringTypeValues.Center };
                                    A.ListStyle listStyle1 = new A.ListStyle();

                                    A.Paragraph paragraph1 = new A.Paragraph();
                                    A.ParagraphProperties paragraphProperties1 = new A.ParagraphProperties() { Alignment = A.TextAlignmentTypeValues.Center };

                                    //Dot Label size, color and font name
                                    A.Run run1 = new A.Run();
                                    A.RunProperties runProperties1 = new A.RunProperties() { Language = "en-US", Dirty = false, FontSize = dot_font_size, SmartTagClean = false };
                                    //Kumimoji = true, Language = "en-US", FontSize = dot_font_size, Bold = false, Italic = false, Underline = A.TextUnderlineValues.None, Strike = A.TextStrikeValues.NoStrike, Capital = A.TextCapsValues.None, NormalizeHeight = false, Baseline = 0, Dirty = false, SpellingError = true, SmartTagClean = false

                                    A.SolidFill solidFill13 = new A.SolidFill();
                                    A.RgbColorModelHex rgbColorModelHex179 = new A.RgbColorModelHex() { Val = dot_font_color }; //color of dot label
                                    solidFill13.Append(rgbColorModelHex179);

                                    A.LatinFont latinFont11 = new A.LatinFont() { Typeface = dot_label_font_type, PitchFamily = 34, CharacterSet = 0 }; //dot label font type
                                    A.ComplexScriptFont complexScriptFont11 = new A.ComplexScriptFont() { Typeface = dot_label_font_type, PitchFamily = 34, CharacterSet = 0 }; //dot label font type

                                    runProperties1.Append(solidFill13);
                                    runProperties1.Append(latinFont11);
                                    runProperties1.Append(complexScriptFont11);

                                    //Add dot labels
                                    A.Text text1 = new A.Text();
                                    if (show_dot_labels == true)
                                    {
                                        text1.Text = dot_lables_resp_id[w].Trim();
                                    }
                                    else
                                    {
                                        text1.Text = "";
                                    }

                                    run1.Append(runProperties1);
                                    run1.Append(text1);

                                    A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties() { Language = "en-US", Dirty = false };

                                    paragraph1.Append(paragraphProperties1);
                                    paragraph1.Append(run1);
                                    paragraph1.Append(endParagraphRunProperties1);

                                    textBody1.Append(bodyProperties1);
                                    textBody1.Append(listStyle1);
                                    textBody1.Append(paragraph1);

                                    shape1.Append(nonVisualShapeProperties1);
                                    shape1.Append(shapeProperties1);
                                    shape1.Append(shapeStyle1);
                                    shape1.Append(textBody1);

                                    shapeTree1.Append(shape1);

                                }
                            }

                        }
                    }

                                  
                               
                    //Save presentation
                    presentationDoc.Close();

                   
                    //Insert New Slide into Presentaion Document (It executes the code from the link above)
                    this.CopySlideIntoPresentation(mtemplate_location, mfinal_presentation_location);

                    shapeTree1.Remove();

                    //Copy template each time a map is created. Overides it if exists
                    string fileToCopy = "pm_template_with_adj.pptx";
                    string sourceFile = System.IO.Path.Combine(mtemplate_path, fileToCopy);
                    System.IO.File.Copy(sourceFile, mtemplate_location, true);
                }


            }
            catch (Exception Ex)
            {
                merror = Ex.Message;
                return false;
            }
            return true;
        }
    Tuesday, July 24, 2012 1:04 PM

All replies

  • Hi Manka1972,

    Thanks for posting in the MSDN Forum.

    Do you catch any exception in your snippet?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, July 25, 2012 2:58 AM
    Moderator
  • Yes, the last 3-4 lines of my code are supposed to catch any exception, but nothing is caught. The Try is above the first line as shown below:

     try
      {
        using (PresentationDocument presentationDoc = PresentationDocument.Open(mtemplate_location, true))

    Any suggestions so far?

    Thanks

    Wednesday, July 25, 2012 9:37 AM