Beispiel #1
0
        public void WorkWithEncryptedDocument(SaveFormat saveFormat)
        {
            //ExStart
            //ExFor:OdtSaveOptions.#ctor(String)
            //ExSummary:Shows how to load and change odt/ott encrypted document.
            Document doc = new Document(MyDir + "Encrypted" +
                                        FileFormatUtil.SaveFormatToExtension(saveFormat),
                                        new LoadOptions("@sposeEncrypted_1145"));

            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.MoveToDocumentEnd();
            builder.Writeln("Encrypted document after changes.");

            // Saving document using new instance of OdtSaveOptions
            doc.Save(ArtifactsDir + "OdtSaveOptions.WorkWithEncryptedDocument" +
                     FileFormatUtil.SaveFormatToExtension(saveFormat), new OdtSaveOptions("@sposeEncrypted_1145"));
            //ExEnd

            // Check that document is still encrypted with a password
            FileFormatInfo docInfo =
                FileFormatUtil.DetectFileFormat(ArtifactsDir + "OdtSaveOptions.WorkWithEncryptedDocument" + FileFormatUtil.SaveFormatToExtension(saveFormat));

            Assert.IsTrue(docInfo.IsEncrypted);
        }
Beispiel #2
0
        public void ExportTextBoxAsSvg(SaveFormat saveFormat, bool isTextBoxAsSvg)
        {
            string[] dirFiles;

            Document doc = new Document(MyDir + "HtmlSaveOptions.ExportTextBoxAsSvg.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions(saveFormat);

            saveOptions.ExportTextBoxAsSvg = isTextBoxAsSvg;

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportTextBoxAsSvg" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);

            switch (saveFormat)
            {
            case SaveFormat.Html:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;

            case SaveFormat.Epub:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;

            case SaveFormat.Mhtml:     // ToDo: Check results of this assert

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.IsEmpty(dirFiles);
                return;
            }
        }
Beispiel #3
0
        public void SaveDocumentEncryptedWithAPassword(SaveFormat saveFormat)
        {
            //ExStart
            //ExFor:OdtSaveOptions.#ctor(SaveFormat)
            //ExFor:OdtSaveOptions.Password
            //ExFor:OdtSaveOptions.SaveFormat
            //ExSummary:Shows how to encrypted your odt/ott documents with a password.
            Document doc = new Document(MyDir + "Document.docx");

            OdtSaveOptions saveOptions = new OdtSaveOptions(saveFormat);

            saveOptions.Password = "******";

            // Saving document using password property of OdtSaveOptions
            doc.Save(ArtifactsDir + "OdtSaveOptions.SaveDocumentEncryptedWithAPassword" +
                     FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
            //ExEnd

            // Check that all documents are encrypted with a password
            FileFormatInfo docInfo = FileFormatUtil.DetectFileFormat(
                ArtifactsDir + "OdtSaveOptions.SaveDocumentEncryptedWithAPassword" +
                FileFormatUtil.SaveFormatToExtension(saveFormat));

            Assert.IsTrue(docInfo.IsEncrypted);
        }
Beispiel #4
0
        public void ExportOfficeMath(SaveFormat saveFormat, HtmlOfficeMathOutputMode outputMode)
        {
            Document doc = new Document(MyDir + "OfficeMath.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions();

            saveOptions.OfficeMathOutputMode = outputMode;

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportToHtmlUsingImage" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
        }
Beispiel #5
0
        public void ExportPageMargins(SaveFormat saveFormat)
        {
            Document doc = new Document(MyDir + "HtmlSaveOptions.ExportPageMargins.docx");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions
            {
                SaveFormat        = saveFormat,
                ExportPageMargins = true
            };

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportPageMargins" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);
        }
Beispiel #6
0
        public void DetectFileFormat_SaveFormatToLoadFormat()
        {
            //ExStart
            //ExFor:FileFormatUtil.SaveFormatToLoadFormat(SaveFormat)
            //ExSummary:Shows how to use the FileFormatUtil class and to convert a SaveFormat enumeration into the corresponding LoadFormat enumeration.
            // Define the SaveFormat enumeration to convert.
            SaveFormat saveFormat = SaveFormat.Html;
            // Convert the SaveFormat enumeration to LoadFormat enumeration.
            LoadFormat loadFormat = FileFormatUtil.SaveFormatToLoadFormat(saveFormat);

            Console.WriteLine("The converted LoadFormat is: " + FileFormatUtil.LoadFormatToExtension(loadFormat));
            //ExEnd

            Assert.AreEqual(".html", FileFormatUtil.SaveFormatToExtension(saveFormat));
            Assert.AreEqual(".html", FileFormatUtil.LoadFormatToExtension(loadFormat));
        }
Beispiel #7
0
        public void DetectFileFormat_EnumConversions()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(Stream)
            //ExFor:FileFormatUtil.LoadFormatToExtension(LoadFormat)
            //ExFor:FileFormatUtil.ExtensionToSaveFormat(String)
            //ExFor:FileFormatUtil.SaveFormatToExtension(SaveFormat)
            //ExFor:FileFormatUtil.LoadFormatToSaveFormat(LoadFormat)
            //ExFor:Document.OriginalFileName
            //ExFor:FileFormatInfo.LoadFormat
            //ExSummary:Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
            // Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format.
            // These are both times where you might need extract the file format as it's not visible
            FileStream
                docStream = File.OpenRead(
                MyDir + "Document.FileWithoutExtension");     // The file format of this document is actually ".doc"
            FileFormatInfo info = FileFormatUtil.DetectFileFormat(docStream);

            // Retrieve the LoadFormat of the document.
            LoadFormat loadFormat = info.LoadFormat;

            // Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations.
            //
            // Method #1
            // Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension.
            String fileExtension = FileFormatUtil.LoadFormatToExtension(loadFormat);
            // Now convert this extension into the corresponding SaveFormat enumeration
            SaveFormat saveFormat = FileFormatUtil.ExtensionToSaveFormat(fileExtension);

            // Method #2
            // Convert the LoadFormat enumeration directly to the SaveFormat enumeration.
            saveFormat = FileFormatUtil.LoadFormatToSaveFormat(loadFormat);

            // Load a document from the stream.
            Document doc = new Document(docStream);

            // Save the document with the original file name, " Out" and the document's file extension.
            doc.Save(
                ArtifactsDir + "Document.WithFileExtension" + FileFormatUtil.SaveFormatToExtension(saveFormat));
            //ExEnd

            Assert.AreEqual(".doc", FileFormatUtil.SaveFormatToExtension(saveFormat));
        }
        public void ExportTextBoxAsSvg(SaveFormat saveFormat, bool isTextBoxAsSvg)
        {
            string[] dirFiles;

            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            Shape textbox = builder.InsertShape(ShapeType.TextBox, 300, 100);

            builder.MoveTo(textbox.FirstParagraph);
            builder.Write("Hello world!");

            HtmlSaveOptions saveOptions = new HtmlSaveOptions(saveFormat);

            saveOptions.ExportTextBoxAsSvg = isTextBoxAsSvg;

            doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportTextBoxAsSvg" + FileFormatUtil.SaveFormatToExtension(saveFormat), saveOptions);

            switch (saveFormat)
            {
            case SaveFormat.Html:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.That(dirFiles, Is.Empty);
                return;

            case SaveFormat.Epub:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.That(dirFiles, Is.Empty);
                return;

            case SaveFormat.Mhtml:

                dirFiles = Directory.GetFiles(ArtifactsDir, "HtmlSaveOptions.ExportTextBoxAsSvg.001.png", SearchOption.AllDirectories);
                Assert.That(dirFiles, Is.Empty);
                return;
            }
        }
Beispiel #9
0
        public void SaveToDetectedFileFormat()
        {
            //ExStart
            //ExFor:FileFormatUtil.DetectFileFormat(Stream)
            //ExFor:FileFormatUtil.LoadFormatToExtension(LoadFormat)
            //ExFor:FileFormatUtil.ExtensionToSaveFormat(String)
            //ExFor:FileFormatUtil.SaveFormatToExtension(SaveFormat)
            //ExFor:FileFormatUtil.LoadFormatToSaveFormat(LoadFormat)
            //ExFor:Document.OriginalFileName
            //ExFor:FileFormatInfo.LoadFormat
            //ExFor:LoadFormat
            //ExSummary:Shows how to use the FileFormatUtil methods to detect the format of a document.
            // Load a document from a file that is missing a file extension, and then detect its file format.
            using (FileStream docStream = File.OpenRead(MyDir + "Word document with missing file extension"))
            {
                FileFormatInfo info       = FileFormatUtil.DetectFileFormat(docStream);
                LoadFormat     loadFormat = info.LoadFormat;

                Assert.AreEqual(LoadFormat.Doc, loadFormat);

                // Below are two methods of converting a LoadFormat to its corresponding SaveFormat.
                // 1 -  Get the file extension string for the LoadFormat, then get the corresponding SaveFormat from that string:
                string     fileExtension = FileFormatUtil.LoadFormatToExtension(loadFormat);
                SaveFormat saveFormat    = FileFormatUtil.ExtensionToSaveFormat(fileExtension);

                // 2 -  Convert the LoadFormat directly to its SaveFormat:
                saveFormat = FileFormatUtil.LoadFormatToSaveFormat(loadFormat);

                // Load a document from the stream, and then save it to the automatically detected file extension.
                Document doc = new Document(docStream);

                Assert.AreEqual(".doc", FileFormatUtil.SaveFormatToExtension(saveFormat));

                doc.Save(ArtifactsDir + "File.SaveToDetectedFileFormat" + FileFormatUtil.SaveFormatToExtension(saveFormat));
            }
            //ExEnd
        }
        public void MergingTableCellsDynamically(string value1, string value2, string resultDocumentName)
        {
            string artifactPath = ArtifactsDir + resultDocumentName +
                                  FileFormatUtil.SaveFormatToExtension(SaveFormat.Docx);
            string goldPath = GoldsDir + resultDocumentName + " Gold" +
                              FileFormatUtil.SaveFormatToExtension(SaveFormat.Docx);

            Document doc = new Document(MyDir + "ReportingEngine.MergingTableCellsDynamically.docx");

            List <ClientTestClass> clients = new List <ClientTestClass>
            {
                new ClientTestClass
                {
                    Name         = "John Monrou",
                    Country      = "France",
                    LocalAddress = "27 RUE PASTEUR"
                },
                new ClientTestClass
                {
                    Name         = "James White",
                    Country      = "New Zealand",
                    LocalAddress = "14 Tottenham Court Road"
                },
                new ClientTestClass
                {
                    Name         = "Kate Otts",
                    Country      = "New Zealand",
                    LocalAddress = "Wellington 6004"
                }
            };

            BuildReport(doc, new object[] { value1, value2, clients }, new [] { "value1", "value2", "clients" });

            doc.Save(artifactPath);

            Assert.IsTrue(DocumentHelper.CompareDocs(artifactPath, goldPath));
        }
Beispiel #11
0
        public void Encrypt(SaveFormat saveFormat)
        {
            //ExStart
            //ExFor:OdtSaveOptions.#ctor(SaveFormat)
            //ExFor:OdtSaveOptions.Password
            //ExFor:OdtSaveOptions.SaveFormat
            //ExSummary:Shows how to encrypt a saved ODT/OTT document with a password, and then load it using Aspose.Words.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Writeln("Hello world!");

            // Create a new OdtSaveOptions, and pass either "SaveFormat.Odt",
            // or "SaveFormat.Ott" as the format to save the document in.
            OdtSaveOptions saveOptions = new OdtSaveOptions(saveFormat);

            saveOptions.Password = "******";

            string extensionString = FileFormatUtil.SaveFormatToExtension(saveFormat);

            // If we open this document with an appropriate editor,
            // it will prompt us for the password we specified in the SaveOptions object.
            doc.Save(ArtifactsDir + "OdtSaveOptions.Encrypt" + extensionString, saveOptions);

            FileFormatInfo docInfo = FileFormatUtil.DetectFileFormat(ArtifactsDir + "OdtSaveOptions.Encrypt" + extensionString);

            Assert.IsTrue(docInfo.IsEncrypted);

            // If we wish to open or edit this document again using Aspose.Words,
            // we will have to provide a LoadOptions object with the correct password to the loading constructor.
            doc = new Document(ArtifactsDir + "OdtSaveOptions.Encrypt" + extensionString,
                               new LoadOptions("@sposeEncrypted_1145"));

            Assert.AreEqual("Hello world!", doc.GetText().Trim());
            //ExEnd
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            // Create document and save it in all available formats.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Writeln("Hello from Aspose.Words!!!");

            foreach (SaveFormat sf in Enum.GetValues(typeof(SaveFormat)))
            {
                if (sf != SaveFormat.Unknown)
                {
                    try
                    {
                        doc.Save(string.Format("out{0}", FileFormatUtil.SaveFormatToExtension(sf)), sf);
                        Console.WriteLine("Saving {0}\t\t[OK]", sf);
                    }
                    catch
                    {
                        Console.WriteLine("Saving {0}\t\t[FAILED]", sf);
                    }
                }
            }
        }