Exemple #1
0
        private string convert(String path, List <String> imgList, Size size)
        {
            String filePath = String.Empty;

            while (String.IsNullOrEmpty(filePath) || System.IO.File.Exists(filePath))
            {
                string guid = Guid.NewGuid().ToString();
                filePath = System.IO.Path.Combine(path, guid + ".pptx");
            }

            try
            {
                using (PresentationDocument presentationDocument = PresentationDocument.Create(filePath, PresentationDocumentType.Presentation))
                {
                    PresentationPart presentationPart = presentationDocument.AddPresentationPart();
                    presentationPart.Presentation = new Presentation();
                    insert(presentationPart, imgList);
                    presentationDocument.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                throw new Exception("fail to convert image to pptx");
            }

            return(filePath);
        }
 public PointContext()
 {
     Stream         = new MemoryStream();
     Doc            = PresentationDocument.Create(Stream, PresentationDocumentType.Presentation, true);
     P              = Doc.AddPresentationPart();
     P.Presentation = new Presentation();
 }
Exemple #3
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            PresentationDocument document = PresentationDocument.Create("test.pptx", PresentationDocumentType.Presentation);
        }
Exemple #4
0
        public static PresentationDocument CreateBlankPPT(string filepath)
        {
            PresentationDocument presentationDoc  = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation, true);
            PresentationPart     presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new Presentation();

            CreatePresentationParts(presentationPart);
            return(presentationDoc);
        }
Exemple #5
0
        /// <summary>
        /// 创建ppt
        /// </summary>
        /// <param name="filepath">保存地址</param>
        public static void CreatePresentation(string filepath)
        {
            PresentationDocument presentationDoc  = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
            PresentationPart     presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new Presentation();

            CreatePresentationParts(presentationPart);
            presentationDoc.Close();
        }
Exemple #6
0
 public void P003_PptxCreation_Stream()
 {
     using (MemoryStream ms = new MemoryStream())
         using (PresentationDocument doc = PresentationDocument.Create(ms, PresentationDocumentType.Presentation))
         {
             CreatePresentation(doc);
             OpenXmlValidator v = new OpenXmlValidator(FileFormatVersions.Office2013);
             var errs           = v.Validate(doc);
             Assert.Equal(0, errs.Count());
         }
 }
        public static PresentationDocument CreatePresentationDocument(string FilePath, PPTXSetting FileSettings, PPTXSlideLayoutGroup SlideLayouts)
        {
            var presentationDoc = PresentationDocument.Create(FilePath, PresentationDocumentType.Presentation);
            PresentationPart presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new Presentation();

            DefaultPresentationPart.CreatePresentationPart(presentationPart, FileSettings, SlideLayouts);

            return(presentationDoc);
        }
 public PowerPointDocument(string filename)
 {
     _filename = filename;
     using (
         PresentationDocument ppDoc = PresentationDocument.Create(_filename,
                                                                  PresentationDocumentType.Presentation)
         )
     {
         var pp = new PowerPointTemplate();
         pp.CreateParts(ppDoc);
     }
 }
Exemple #9
0
        public void P003_PptxCreation_Stream()
        {
            using (var ms = new MemoryStream())
                using (var doc = PresentationDocument.Create(ms, PresentationDocumentType.Presentation))
                {
                    CreatePresentation(doc);

                    var v    = new OpenXmlValidator(FileFormatVersions.Office2013);
                    var errs = v.Validate(doc);

                    Assert.Empty(errs);
                }
        }
        public static void CreatePresentation(string filepath)
        {
            // Create a presentation at a specified file path. The presentation document type is pptx, by default.
            PresentationDocument presentationDoc  = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
            PresentationPart     presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new Presentation();

            CreatePresentationParts(presentationPart);

            // Close the presentation handle
            presentationDoc.Close();
        }
        public byte[] GetDocument(PresentationGenerationData data)
        {
            using (var mem = new MemoryStream())
            {
                using (var package =
                           PresentationDocument.Create(mem, PresentationDocumentType.Presentation, true))
                {
                    CreateParts(package, data);
                }

                return(mem.ToArray());
            }
        }
Exemple #12
0
        public void DirectWriteTest()
        {
            var doc = WordprocessingDocument.Create(@"files\testOpenXmlLib.docx", OpenXmlPackage.DocumentType.Document);

            var part = doc.MainDocumentPart;

            const string docXml =
                @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
    <w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body>
</w:document>";

            var stream = part.GetStream();
            var buf    = (new UTF8Encoding()).GetBytes(docXml);

            stream.Write(buf, 0, buf.Length);


            doc.Close();


            var presentation     = PresentationDocument.Create(@"files\testOpenXmlLib.pptx", OpenXmlPackage.DocumentType.Document);
            var presentationPart = presentation.PresentationPart;

            var slide = presentationPart.AddSlidePart();

            string presentationXml =
                @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
    <p:presentation xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" 
        xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" 
        xmlns:p=""http://schemas.openxmlformats.org/presentationml/2006/main"" saveSubsetFonts=""1"">
  <p:sldIdLst>
    <p:sldId id=""256"" r:id=""" + slide.RelIdToString + @"""/>
  </p:sldIdLst>
</p:presentation>";

            stream = presentationPart.GetStream();
            buf    = (new UTF8Encoding()).GetBytes(presentationXml);
            stream.Write(buf, 0, buf.Length);

            string slideXml =
                @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<p:sld xmlns:a=""http://schemas.openxmlformats.org/drawingml/2006/main"" xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" xmlns:p=""http://schemas.openxmlformats.org/presentationml/2006/main"">
</p:sld>";

            stream = slide.GetStream();
            buf    = (new UTF8Encoding()).GetBytes(slideXml);
            stream.Write(buf, 0, buf.Length);

            presentation.Close();
        }
        public static void CreatePresentation()
        {
            string filepath = $@"{Constants.Locations.DesktopPath}\{Constants.FTP.PptFile}";
            // Create a presentation at a specified file path. The presentation document type is pptx, by default.
            PresentationDocument presentationDoc  = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
            PresentationPart     presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new P.Presentation();

            CreatePresentationParts(presentationPart);

            //Close the presentation handle
            presentationDoc.Close();
        }
        public void CreatePresentation(string filepath)
        {
            // Create a presentation at a specified file path. The presentation document type is pptx, by default.
            PresentationDocument presentationDoc  = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
            PresentationPart     presentationPart = presentationDoc.AddPresentationPart();

            presentationPart.Presentation = new Presentation();
            CreatePresentationParts(presentationPart);


            //Close the presentation handle

            //presentationDoc.SaveAs(@"C:\\Users\\Public\\Music\\info.pptx");
            // presentationDoc.Save();
            presentationDoc.Close();
            //InsertNewSlide(presentationDoc, 1, "Hello, my name is Sumit");
        }
        /// <summary>
        /// create pptx
        /// using OpenSDK
        /// </summary>
        /// <param name="svgDocs"></param>
        /// <param name="stream"></param>
        public static void CreatePowerPointXStreamBySvgs(List <SvgDocument> svgDocs, Stream stream)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (PresentationDocument presentationDoc = PresentationDocument.Create(ms, PresentationDocumentType.Presentation, true))
                {
                    PresentationPart presentationPart = presentationDoc.AddPresentationPart();
                    presentationPart.Presentation = new Presentation();
                    CreatePresentationTempalte(presentationPart);

                    CreateImageSlideParts(presentationPart, svgDocs);
                    DeleteSlide(presentationPart, 0);
                    presentationDoc.Close();
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.WriteTo(stream);
                }
            }
        }
Exemple #16
0
        private WeakReference RunPresentation(OpenXmlValidator validator)
        {
            using (var ms = new MemoryStream())
            {
                using (var presentation = PresentationDocument.Create(ms, PresentationDocumentType.Presentation))
                {
                    var presentationPart = presentation.AddPresentationPart();
                    presentationPart.Presentation = new Presentation.Presentation();
                }

                using (var presentation = PresentationDocument.Open(ms, false))
                {
                    validator.Validate(presentation);

                    return(new WeakReference(presentation));
                }
            }
        }
        public void SaveDocument(PresentationGenerationData data, string filePath, string fileName)
        {
            using (var mem = new MemoryStream())
            {
                using (var package =
                           PresentationDocument.Create(mem, PresentationDocumentType.Presentation, true))
                {
                    CreateParts(package, data);
                }

                mem.Position = 0;

                using (var file = new FileStream($"{filePath}\\{fileName}", FileMode.CreateNew, FileAccess.Write))
                {
                    mem.CopyTo(file);
                }
            }
        }
Exemple #18
0
        private static void processFile(String InputFile)
        {
            // copy processing file
            ProcessingFile procFile = new ProcessingFile(InputFile);

            //make output file name
            if (ChoosenOutputFile == null)
            {
                if (InputFile.Contains("."))
                {
                    ChoosenOutputFile = InputFile.Remove(InputFile.LastIndexOf(".")) + ".pptx";
                }
                else
                {
                    ChoosenOutputFile = InputFile + ".pptx";
                }
            }

            //open the reader
            using (StructuredStorageReader reader = new StructuredStorageReader(procFile.File.FullName))
            {
                // parse the ppt document
                PowerpointDocument ppt = new PowerpointDocument(reader);

                // detect document type and name
                OpenXmlPackage.DocumentType outType = Converter.DetectOutputType(ppt);
                string conformOutputFile            = Converter.GetConformFilename(ChoosenOutputFile, outType);

                // create the pptx document
                PresentationDocument pptx = PresentationDocument.Create(conformOutputFile, outType);

                //start time
                DateTime start = DateTime.Now;
                TraceLogger.Info("Converting file {0} into {1}", InputFile, conformOutputFile);

                // convert
                Converter.Convert(ppt, pptx);

                // stop time
                DateTime end  = DateTime.Now;
                TimeSpan diff = end.Subtract(start);
                TraceLogger.Info("Conversion of file {0} finished in {1} seconds", InputFile, diff.TotalSeconds.ToString(CultureInfo.InvariantCulture));
            }
        }
        private static MemoryStream CreatePresentationDocumentWithDataReference()
        {
            var stream = new MemoryStream();

            using (var ppt = PresentationDocument.Create(stream, PresentationDocumentType.Presentation))
            {
                var mainPart  = ppt.AddPresentationPart();
                var slide     = new Slide();
                var slidePart = mainPart.AddNewPart <SlidePart>();
                var dataPart  = new MediaDataPart(ppt.InternalOpenXmlPackage, MediaDataPartType.Mp3);
                var dataPartReferenceRelationship = DataPartReferenceRelationship.Create(slidePart, dataPart, MediaReferenceRelationship.RelationshipTypeConst, "rId2");
                slidePart.AddDataPartReferenceRelationship(dataPartReferenceRelationship);
                slide.Save(slidePart);
                mainPart.Presentation = new Presentation.Presentation(slide);
                mainPart.Presentation.Save();
            }

            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }
Exemple #20
0
        private static Stream PresentationDocumentToStream(PresentationDocument pptxDoc)
        {
            MemoryStream mem = new MemoryStream();

            using (var resultDoc = PresentationDocument.Create(mem, pptxDoc.DocumentType))
            {
                // copy parts from source document to new document
                foreach (var part in pptxDoc.Parts)
                {
                    OpenXmlPart targetPart = resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId); // that's recursive :-)
                }

                resultDoc.Package.Flush();
            }
            //resultDoc.Package.Close(); // must do this (or using), or the zip won't get created properly

            mem.Position = 0;

            return(mem);
        }
Exemple #21
0
        public static OpenXmlMemoryStreamDocument CreatePresentationDocument()
        {
            var stream = new MemoryStream();

            using var doc = PresentationDocument.Create(stream, DocumentFormat.OpenXml.PresentationDocumentType.Presentation);
            doc.AddPresentationPart();
            XNamespace ns = "http://schemas.openxmlformats.org/presentationml/2006/main";
            XNamespace relationshipsns = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
            XNamespace drawingns       = "http://schemas.openxmlformats.org/drawingml/2006/main";

            doc.PresentationPart.PutXDocument(new XDocument(
                                                  new XElement(ns + "presentation",
                                                               new XAttribute(XNamespace.Xmlns + "a", drawingns),
                                                               new XAttribute(XNamespace.Xmlns + "r", relationshipsns),
                                                               new XAttribute(XNamespace.Xmlns + "p", ns),
                                                               new XElement(ns + "sldMasterIdLst"),
                                                               new XElement(ns + "sldIdLst"),
                                                               new XElement(ns + "notesSz", new XAttribute("cx", "6858000"), new XAttribute("cy", "9144000")))));
            doc.Close();
            return(new OpenXmlMemoryStreamDocument(stream));
        }
Exemple #22
0
        public static OpenXmlPackage CreatePackage(this FileInfo file, bool overwiteIfExist)
        {
            if (file.Exists)
            {
                throw new InvalidOperationException(string.Format("The file {0} exists!!", file.FullName));
            }

            if (IsWordprocessingFile(file))
            {
                return(WordprocessingDocument.Create(file.FullName, WordprocessingDocumentType.Document));
            }
            else if (IsSpreadsheetFile(file))
            {
                return(SpreadsheetDocument.Create(file.FullName, SpreadsheetDocumentType.Workbook));
            }
            else if (IsPresentationFile(file))
            {
                return(PresentationDocument.Create(file.FullName, PresentationDocumentType.Presentation));
            }
            else
            {
                throw new Exception("Not Supported Document Type!");
            }
        }
        public void CreateWithNoAutoSaveTest()
        {
            this.MyTestInitialize(TestContext.GetCurrentMethod());
            var testfiles = CopyTestFiles("bvt")
                            .Where(fi => fi.IsOpenXmlFile());

            OpenXmlPackage createdPackage = null;

            foreach (var testfile in testfiles)
            {
                string newlyCreatedName = null;
                if (testfile.IsWordprocessingFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".docx");
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".xlsx");
                }
                else if (testfile.IsPresentationFile())
                {
                    newlyCreatedName = Path.Combine(TestUtil.TestResultsDirectory, Guid.NewGuid().ToString() + ".pptx");
                }

                Log.Comment("Opening source package in readOnly mode...");
                var sourcePackage = testfile.OpenPackage(false);
                Log.Comment("Creating new package with overload that no autoSave option");
                if (testfile.IsWordprocessingFile())
                {
                    createdPackage = WordprocessingDocument.Create(newlyCreatedName, WordprocessingDocumentType.Document);
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    createdPackage = SpreadsheetDocument.Create(newlyCreatedName, SpreadsheetDocumentType.Workbook);
                }
                else if (testfile.IsPresentationFile())
                {
                    createdPackage = PresentationDocument.Create(newlyCreatedName, PresentationDocumentType.Presentation);
                }
                else
                {
                    Log.Fail("Unexpected document type passed in.");
                }
                if (createdPackage != null)
                {
                    Log.Comment("Feeding main part with DOM operations...");
                    duplicteMainPart(sourcePackage, createdPackage);
                    Log.Comment("Closing package...");
                    createdPackage.Close();
                }

                Log.Warning("Reopening...Expecting null for root element of main part as AutoSave default value is true thus DOM changes will be saved.");
                if (testfile.IsWordprocessingFile())
                {
                    createdPackage = WordprocessingDocument.Open(newlyCreatedName, false);
                }
                else if (testfile.IsSpreadsheetFile())
                {
                    createdPackage = SpreadsheetDocument.Open(newlyCreatedName, false);
                }
                else if (testfile.IsPresentationFile())
                {
                    createdPackage = PresentationDocument.Open(newlyCreatedName, false);
                }
                else
                {
                    Log.Fail("Unexpected document type passed in.");
                }
                var mainpart = createdPackage.MainPart();
                var dom      = mainpart.RootElement;
                if (dom != null)
                {
                    Log.Pass("Root element of main part is not null just as expected.");
                }
                else
                {
                    Log.Fail("Null root element of main part. Something must be wrong.");
                }

                createdPackage.Close();
                FileInfo fz = new FileInfo(newlyCreatedName);
                if (fz.Exists)
                {
                    fz.Delete();
                }
            }
        }