Ejemplo n.º 1
0
        internal static void Test()
        {
            const string outputFolderName = "MergeOps";
            var          outputFolder     = StandardLibraryMethods.CombinePaths(TestStatics.OutputFolderPath, outputFolderName);

            IoMethods.DeleteFolder(outputFolder);
            Directory.CreateDirectory(outputFolder);

            var inputTestFiles = StandardLibraryMethods.CombinePaths(TestStatics.InputTestFilesFolderPath, outputFolderName);
            var wordDocx       = StandardLibraryMethods.CombinePaths(inputTestFiles, "word.docx");
            var pdf            = StandardLibraryMethods.CombinePaths(inputTestFiles, "pdf.pdf");

            MergeStatics.Init();
            var singleTestRow      = new PseudoTableRow(1).ToSingleElementArray();
            var testRows           = new[] { new PseudoTableRow(1), new PseudoTableRow(2), new PseudoTableRow(3) };
            var singleRowTree      = MergeStatics.CreatePseudoTableRowTree(singleTestRow);
            var pseudoTableRowTree = MergeStatics.CreatePseudoTableRowTree(testRows);

            var explanations = new List <Tuple <String, String> >();

            // Single row to merge against

            // Word files

            const string singleRowWordDoc = "SingleRowMsWordDoc" + FileExtensions.WordDocx;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, singleRowWordDoc))) {
                using (var word = File.OpenRead(wordDocx))
                    CreateMsWordDoc(singleRowTree, false, word, outputFile);
                explanations.Add(Tuple.Create(singleRowWordDoc, "Should be {0} with only one page, and FullName merged in the upper left.".FormatWith(wordDocx)));
            }

            const string singleRowWordDocAsPdf = "SingleRowMsWordDoc" + FileExtensions.Pdf;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, singleRowWordDocAsPdf)))
                CreatePdfFromMsWordDoc(singleRowTree, false, wordDocx, outputFile);
            explanations.Add(
                Tuple.Create(singleRowWordDocAsPdf, "Should be {0} with only one page, FullName merged in the upper left, saved as a PDF.".FormatWith(wordDocx)));

            //Excel
            const string singleRowExcel = "SingleRowExcel" + FileExtensions.ExcelXlsx;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, singleRowExcel)))
                CreateExcelWorkbook(singleRowTree, GetExcelSupportedMergeFields(singleRowTree), outputFile);
            explanations.Add(
                Tuple.Create(
                    singleRowExcel,
                    "An Excel file with the first row frozen and bold with the merge field names. Note that only supported field types may be dispalyed. One more row with data should be present."));

            // Pdf
            const string singleRowPdf = "SingleRowPdf" + FileExtensions.Pdf;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, singleRowPdf)))
                CreatePdf(singleRowTree, false, pdf, outputFile);
            explanations.Add(Tuple.Create(singleRowPdf, "Should be {0} with only one page, FullName filled in and 'Test' displayed.".FormatWith(pdf)));

            // Multiple rows to merge against

            // Word files
            const string multipleRowsWordDoc = "MultipleRowMsWordDoc" + FileExtensions.WordDocx;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, multipleRowsWordDoc))) {
                using (var word = File.OpenRead(wordDocx))
                    CreateMsWordDoc(pseudoTableRowTree, false, word, outputFile);
                explanations.Add(Tuple.Create(multipleRowsWordDoc, "Should be {0} with three pages, and FullName merged in the upper left.".FormatWith(wordDocx)));
            }

            const string multipleRowsWordDocAsPdf = "MultipleRowMsWordDoc" + FileExtensions.Pdf;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, multipleRowsWordDocAsPdf)))
                CreatePdfFromMsWordDoc(pseudoTableRowTree, false, wordDocx, outputFile);
            explanations.Add(
                Tuple.Create(multipleRowsWordDocAsPdf, "Should be {0} with three pages, FullName merged in the upper left, saved as a PDF.".FormatWith(wordDocx)));

            // Excel
            const string multipleRowExcel = "MultipleRowExcel" + FileExtensions.ExcelXlsx;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, multipleRowExcel)))
                CreateExcelWorkbook(pseudoTableRowTree, GetExcelSupportedMergeFields(pseudoTableRowTree), outputFile);
            explanations.Add(
                Tuple.Create(
                    multipleRowExcel,
                    "An Excel file with the first row frozen and bold with the merge field names. Note that only supported field types may be dispalyed. Three more row with data should be present."));

            // Pdf
            const string multipleRowPdf = "MultipleRowPdf" + FileExtensions.Pdf;

            using (var outputFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, multipleRowPdf)))
                CreatePdf(pseudoTableRowTree, false, pdf, outputFile);
            explanations.Add(Tuple.Create(multipleRowPdf, "Should be {0} with three pages, FullName filled in and 'Test' displayed.".FormatWith(pdf)));

            TestStatics.OutputReadme(outputFolder, explanations);
        }
Ejemplo n.º 2
0
        internal static void Test()
        {
            const string outputFolderName = "PdfOpsTests";
            var          outputFolder     = EwlStatics.CombinePaths(TestStatics.OutputFolderPath, outputFolderName);

            IoMethods.DeleteFolder(outputFolder);
            Directory.CreateDirectory(outputFolder);

            var inputTestFiles   = EwlStatics.CombinePaths(TestStatics.InputTestFilesFolderPath, "PdfOps");
            var onePagePdfPath   = EwlStatics.CombinePaths(inputTestFiles, "onepage.pdf");
            var twoPagePdfPath   = EwlStatics.CombinePaths(inputTestFiles, "twopage.pdf");
            var threePagePdfPath = EwlStatics.CombinePaths(inputTestFiles, "threepage.pdf");

            var explanations = new List <Tuple <String, String> >();

            //ConcatPdfs

            using (var onePage = File.OpenRead(onePagePdfPath)) {
                const string concatOnePdf = "ConcatOne.pdf";
                using (var concatFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, concatOnePdf)))
                    ConcatPdfs(onePage.ToCollection(), concatFile);
                explanations.Add(Tuple.Create(concatOnePdf, "This file should be exactly the same as {0}.".FormatWith(onePagePdfPath)));

                resetFileStream(onePage);
                using (var twoPage = File.OpenRead(twoPagePdfPath)) {
                    const string concatTwoPdfs = "ConcatTwo.pdf";
                    using (var concatFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, concatTwoPdfs)))
                        ConcatPdfs(new[] { onePage, twoPage }, concatFile);
                    explanations.Add(
                        Tuple.Create(concatTwoPdfs, "This file should look like {0} immediately followed by {1}.".FormatWith(onePagePdfPath, twoPagePdfPath)));

                    resetFileStream(onePage, twoPage);
                    using (var threePage = File.OpenRead(threePagePdfPath)) {
                        const string concatThreePdfs = "ConcatThree.pdf";
                        using (var concatFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, concatThreePdfs)))
                            ConcatPdfs(new[] { onePage, twoPage, threePage }, concatFile);
                        explanations.Add(
                            Tuple.Create(
                                concatThreePdfs,
                                "This file should look like {0} immediately followed by {1} immediately followed by {2}.".FormatWith(
                                    onePagePdfPath,
                                    twoPagePdfPath,
                                    threePagePdfPath)));
                    }
                }
            }

            //CreateBookmarkedPdf

            using (var onePage = new MemoryStream()) {
                File.OpenRead(onePagePdfPath).CopyTo(onePage);
                const string bookmarkOnePdf = "BookmarkOne.pdf";
                const string bookmarkTitle  = "Bookmark 1";
                using (var bookmarkFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, bookmarkOnePdf)))
                    CreateBookmarkedPdf(Tuple.Create(bookmarkTitle, onePage).ToCollection(), bookmarkFile);
                explanations.Add(
                    Tuple.Create(bookmarkOnePdf, "This should be {0} labeled with one bookmark named {1}.".FormatWith(onePagePdfPath, bookmarkTitle)));

                using (var twoPage = new MemoryStream()) {
                    File.OpenRead(twoPagePdfPath).CopyTo(twoPage);
                    const string bookmarkTwoPdf      = "BookmarkTwo.pdf";
                    const string firstBookmarkTitle  = "First bookmark";
                    const string secondBookmarkTitle = "Second bookmark";
                    using (var bookmarkFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, bookmarkTwoPdf)))
                        CreateBookmarkedPdf(new[] { Tuple.Create(firstBookmarkTitle, onePage), Tuple.Create(secondBookmarkTitle, twoPage) }, bookmarkFile);
                    explanations.Add(
                        Tuple.Create(
                            bookmarkTwoPdf,
                            "This should be {0} labeled with bookmark named {1} followed by {2} with the title of {3}.".FormatWith(
                                onePagePdfPath,
                                firstBookmarkTitle,
                                twoPagePdfPath,
                                secondBookmarkTitle)));

                    using (var threePage = new MemoryStream()) {
                        File.OpenRead(threePagePdfPath).CopyTo(threePage);
                        const string bookmarkThreePdf   = "BookmarkThree.pdf";
                        const string thirdBookmarkTItle = "Third bookmark";
                        using (var bookmarkFile = File.OpenWrite(EwlStatics.CombinePaths(outputFolder, bookmarkThreePdf))) {
                            CreateBookmarkedPdf(
                                new[]
                            {
                                Tuple.Create(firstBookmarkTitle, onePage), Tuple.Create(secondBookmarkTitle, twoPage), Tuple.Create(thirdBookmarkTItle, threePage)
                            },
                                bookmarkFile);
                        }
                        explanations.Add(
                            Tuple.Create(
                                bookmarkThreePdf,
                                "This should be {0} labeled with bookmark named {1} followed by {2} with the title of {3} followed by {4} with the title of {5}.".FormatWith(
                                    onePagePdfPath,
                                    firstBookmarkTitle,
                                    twoPagePdfPath,
                                    secondBookmarkTitle,
                                    threePagePdfPath,
                                    thirdBookmarkTItle)));
                    }
                }
            }


            TestStatics.OutputReadme(outputFolder, explanations);
        }