コード例 #1
0
        public ActionResult ConvertTest()
        {
            var m = new TemplateViewModel
            {
                Template = "Hello",
                Title    = "none",
                FileName = "htmltest.pdf"
            };

            ViewData.Model = m;

            var str = ToHtml($"~/Views/Templates/{m.Template}.cshtml", ViewData);

            var converter = new PdfConverter(@"D:\pdfs");
            var data      = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(m)));

            var args = new Args($"{Request.Url.Scheme}://{Request.Url.Authority}/convert/display?data={data}")
            {
                title      = m.Title,
                outputname = m.FileName
            };

            var result = converter.Convert(args);

            if (result.success)
            {
                var file = System.IO.File.ReadAllBytes(result.data);

                return(File(file, "application/pdf", m.FileName));
            }
            else
            {
                return(Content(result.error));
            }
        }
コード例 #2
0
        public void ConvertTest()
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (!_testEverything)
            {
                return;
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse
            // ReSharper disable CSharpWarnings::CS0162
            // ReSharper disable HeuristicUnreachableCode
            PdfConverter.SecondsToWait = 4;
            var target   = new PdfConverter();
            var expected = target.Convert(PdfPath, TempXpsPath);

            Assert.AreEqual(true, expected);
            Assert.IsTrue(File.Exists(TempXpsPath));

            var xpsFileInfo     = new FileInfo(XpsPath);
            var tempXpsFileInfo = new FileInfo(XpsPath);

            Assert.IsTrue(xpsFileInfo.Length * 1.1 > tempXpsFileInfo.Length);
            Assert.IsTrue(xpsFileInfo.Length * 0.9 < tempXpsFileInfo.Length);
            // ReSharper restore HeuristicUnreachableCode
            // ReSharper restore CSharpWarnings::CS0162
        }
コード例 #3
0
        protected Stream GenerateDocument(params object[] data)
        {
            var file = Path.Combine(DocumentFolder, TemplateFile);

            if (!File.Exists(file))
            {
                throw new IOException("Can't find template document: " + TemplateFile);
            }
            var ext = Path.GetExtension(TemplateFile);
            var cms = ChunkedMemoryStream.Create();

            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                fs.CopyTo(cms);
            }
            cms.Position = 0;
            using (var document = Factory.Open(cms, ext))
            {
                if (data != null)
                {
                    foreach (var d in data)
                    {
                        document.Process(d);
                    }
                }
            }
            cms.Position = 0;
            return(ToPdf ? PdfConverter.Convert(cms, ext, true) : cms);
        }
コード例 #4
0
        public ActionResult Convert(TemplateViewModel m)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { error = "invalid request " }));
            }

            var data = JsonConvert.DeserializeObject <ExpandoObject>(m.Data);

            ViewData.Model = data;

            var str = ToHtml($"~/Views/Templates/{m.Template}.cshtml", ViewData);

            var converter = new PdfConverter(@"D:\pdfs");
            var name      = $"{Guid.NewGuid()}.pdf";

            var args = new Args(str)
            {
                title      = m.Title,
                outputname = m.FileName
            };

            var result = converter.Convert(args);

            if (result.success)
            {
                var file = System.IO.File.ReadAllBytes(result.data);

                return(File(file, "application/pdf", name));
            }
            else
            {
                return(Content(result.error));
            }
        }
コード例 #5
0
        public Stream Execute <TInput>(ISerialization <TInput> input, Argument <TInput> argument)
        {
            var file = Path.Combine(DocumentFolder, argument.File);

            if (!File.Exists(file))
            {
                throw new FileNotFoundException("Can't locate file: {0}. Check if correct file is specified.".With(argument.File));
            }

            var cms = ChunkedMemoryStream.Create();

            using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                fs.CopyTo(cms);
            }
            cms.Position = 0;
            var ext = Path.GetExtension(argument.File);

            using (var document = TemplaterFactory.Open(cms, ext))
            {
                if (argument.GetSources != null)
                {
                    foreach (var source in argument.GetSources)
                    {
                        var found = GetDomain.GetData(source);
                        document.Process(found);
                    }
                }
                if (argument.SearchSources != null)
                {
                    foreach (var source in argument.SearchSources)
                    {
                        var found = SearchDomain.FindData <TInput>(input, source);
                        document.Process(found);
                    }
                }
                var specification =
                    (from a in (argument.SearchSources ?? new SearchDomainObject.Argument <TInput> [0])
                     where a.Specification != null
                     select a.Specification)
                    .FirstOrDefault();
                if (specification != null)
                {
                    dynamic filter = input.Deserialize <TInput, dynamic>(specification);
                    document.Process(filter);
                }
            }
            cms.Position = 0;
            return(argument.ToPdf ? PdfConverter.Convert(cms, ext, true) : cms);
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            var converter = new PdfConverter(new PdfTools());

            converter.PhaseChanged    += Converter_PhaseChanged;
            converter.ProgressChanged += Converter_ProgressChanged;
            converter.Finished        += Converter_Finished;
            converter.Warning         += Converter_Warning;
            converter.Error           += Converter_Error;

            var doc = new PdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Landscape,
                    PaperSize   = PaperKind.A4,
                },
                Objects =
                {
                    new PdfPage()
                    {
                        PagesCount  = true,
                        HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices iaculis. Ut et odio viverra, molestie lectus nec, venenatis turpis. Nulla quis euismod nisl. Duis scelerisque eros nec dui facilisis, sit amet porta odio varius. Praesent vitae sollicitudin leo. Sed vitae quam in massa eleifend porta. Aliquam pulvinar orci dapibus porta laoreet. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed commodo tortor eget dolor hendrerit dapibus.
                                        Vivamus lorem diam, vulputate at ultrices quis, tristique eu nunc. Sed bibendum hendrerit leo. Nulla nec risus turpis. Vivamus at tortor felis. Donec eget posuere libero. Pellentesque erat nunc, molestie eget gravida vitae, eleifend a eros. Integer in tortor sed elit aliquam vehicula eget a erat. Vivamus nisi augue, venenatis ut commodo vel, congue id neque. Curabitur convallis dictum semper. Nulla accumsan urna aliquet, mattis dolor molestie, fermentum metus. Quisque at nisi non augue tempor commodo et pretium orci.
                                        Quisque blandit libero ut laoreet venenatis. Morbi sit amet quam varius, euismod dui et, volutpat felis. Sed nec ante vel est convallis placerat. Morbi mollis pretium tempor. Aliquam luctus eu justo vitae tristique. Sed in elit et elit sagittis pharetra sed vitae velit. Proin eget mi facilisis, scelerisque justo in, ornare urna. Aenean auctor ante ex, eget mattis neque pretium id. Aliquam ut risus leo. Vivamus ullamcorper et mauris in vehicula. Maecenas tristique interdum tempus. Etiam mattis lorem eget odio faucibus, in rhoncus nisi ultrices. Etiam at convallis nibh. Suspendisse tincidunt velit arcu, a volutpat nulla euismod sed.
                                        Aliquam mollis placerat blandit. Morbi in nibh urna. Donec nisl enim, tristique id tincidunt sed, pharetra non mi. Morbi viverra arcu vulputate risus dignissim efficitur. Vivamus dolor eros, finibus et porttitor a, pellentesque a lectus. Integer pellentesque maximus velit sit amet sollicitudin. Nulla a elit eget augue pretium luctus quis eu metus. Aenean nec dui id nibh tempor dapibus. Pellentesque dignissim ullamcorper mauris, vitae pharetra turpis sodales sit amet. Etiam et bibendum neque.
                                        Nulla gravida sit amet velit eu aliquet. Etiam sit amet elit leo. Sed nec arcu tincidunt, placerat turpis quis, laoreet nulla. Aenean neque est, fringilla non nulla in, laoreet vehicula nunc. Etiam vel nisl sit amet lectus pellentesque eleifend. Etiam sed nisi dolor. Mauris quis tincidunt ex. Aliquam porta mattis tempor. Maecenas fringilla bibendum elementum. Vestibulum quis tempus libero, vitae cursus neque. Suspendisse lectus risus, lacinia consectetur enim quis, ullamcorper porta tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                        WebSettings ={ DefaultEncoding                   = "utf-8"                   },
                        PdfHeader   = { FontSize        =9, Right = "Page [page] of [toPage]", Line = true },
                        PdfFooter   = { FontSize        =9, Right = "Page [page] of [toPage]" }
                    }
                }
            };

            byte[] pdf = converter.Convert(doc);

            if (!Directory.Exists("Files"))
            {
                Directory.CreateDirectory("Files");
            }

            using (FileStream stream = new FileStream(@"Files\" + DateTime.UtcNow.Ticks.ToString() + ".pdf", FileMode.Create))
            {
                stream.Write(pdf, 0, pdf.Length);
            }

            Console.ReadKey();
        }
コード例 #7
0
        public void ConvertTest()
        {
            PdfConverter.SecondsToWait = 4;
            var target   = new PdfConverter();
            var expected = target.Convert(PdfPath, TempXpsPath);

            Assert.AreEqual(true, expected);
            Assert.IsTrue(File.Exists(TempXpsPath));

            var xpsFileInfo     = new FileInfo(XpsPath);
            var tempXpsFileInfo = new FileInfo(XpsPath);

            Assert.IsTrue(xpsFileInfo.Length * 1.1 > tempXpsFileInfo.Length);
            Assert.IsTrue(xpsFileInfo.Length * 0.9 < tempXpsFileInfo.Length);
        }
コード例 #8
0
ファイル: Form1.cs プロジェクト: brahmb/.NETExcelLibrary
        private void btnRun_Click(object sender, System.EventArgs e)
        {
            Workbook workbook = new Workbook();

            workbook.LoadFromFile(@"..\..\..\..\..\..\Data\DataTableSample.xls");

            PdfConverter         pdfConverter = new PdfConverter(workbook);
            PdfDocument          pdfDocument  = new PdfDocument();
            PdfConverterSettings settings     = new PdfConverterSettings();

            settings.EmbedFonts       = true;
            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);

            pdfDocument.SaveToFile("sample.pdf");
            pdfDocument.Close();
            ExcelDocViewer("sample.pdf");
        }
コード例 #9
0
        private void ConvertToPdf()
        {
            if (SelectedInspection == null)
            {
                return;
            }

            if (SelectedInspection.DateTimePlanned > DateTime.Now)
            {
                MessageBox.Show("Deze inspectie is nog niet voltooid!");
                return;
            }

            var converter = new PdfConverter {
                IncludeAnswers = true
            };

            converter.Convert(_selectedInspection);
        }
コード例 #10
0
        private void button1_Click(object sender, EventArgs e)
        {
            // load Excel file
            Workbook workbook = new Workbook();

            workbook.LoadFromFile("D:\\MyExcel.xlsx");
            // Set PDF template
            PdfDocument pdfDocument = new PdfDocument();

            pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
            pdfDocument.PageSettings.Width       = 970;
            pdfDocument.PageSettings.Height      = 850;
            //Convert Excel to PDF using the template above
            PdfConverter         pdfConverter = new PdfConverter(workbook);
            PdfConverterSettings settings     = new PdfConverterSettings();

            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);
            // Save and preview PDF
            pdfDocument.SaveToFile("MyPDF.pdf");
            System.Diagnostics.Process.Start("MyPDF.pdf");
        }
コード例 #11
0
        public void ConvertTest()
        {
            if (!RunPdfConverterTests)
            {
                return;
            }
            // ReSharper disable CSharpWarnings::CS0162
            // ReSharper disable HeuristicUnreachableCode
            PdfConverter.SecondsToWaitForAdobeReaderExit = 15;
            var target   = new PdfConverter();
            var expected = target.Convert(PdfPath, TempXpsPath);

            Assert.AreEqual(true, expected);
            Assert.IsTrue(File.Exists(TempXpsPath));

            var xpsFileInfo     = new FileInfo(XpsPath);
            var tempXpsFileInfo = new FileInfo(XpsPath);

            Assert.IsTrue(xpsFileInfo.Length * 1.1 > tempXpsFileInfo.Length);
            Assert.IsTrue(xpsFileInfo.Length * 0.9 < tempXpsFileInfo.Length);
            // ReSharper restore HeuristicUnreachableCode
            // ReSharper restore CSharpWarnings::CS0162
        }
コード例 #12
0
        public void ConvertTestWithBadRequirements()
        {
            var target = new PdfConverter();

            Assert.AreEqual(false, target.Convert("", ""));
        }
コード例 #13
0
		private void btnRun_Click(object sender, System.EventArgs e)
		{
			Workbook workbook = new Workbook();

            workbook.LoadFromFile(@"..\..\..\..\..\..\Data\DataTableSample.xls");

            PdfConverter pdfConverter = new PdfConverter(workbook);
            PdfDocument pdfDocument = new PdfDocument();
            PdfConverterSettings settings = new PdfConverterSettings();
            settings.EmbedFonts = true;
            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);

            pdfDocument.SaveToFile("sample.pdf");
            pdfDocument.Close();
            ExcelDocViewer("sample.pdf");
		}