Exemple #1
0
        public ActionResult MergeDocuments(string InsideBrowser, string OptimizeResources)
        {
            Stream stream1 = new FileStream(ResolveApplicationDataPath("HTTP Succinctly.pdf"), FileMode.Open, FileAccess.Read);
            Stream stream2 = new FileStream(ResolveApplicationDataPath("HTTP Succinctly.pdf"), FileMode.Open, FileAccess.Read);

            //Load the documents as streams
            PdfLoadedDocument doc1 = new PdfLoadedDocument(stream1);
            PdfLoadedDocument doc2 = new PdfLoadedDocument(stream2);

            object[]    dobj = { doc1, doc2 };
            PdfDocument doc  = new PdfDocument();

            if (OptimizeResources == "OptimizeResources")
            {
                PdfMergeOptions mergeOption = new PdfMergeOptions();
                mergeOption.OptimizeResources = true;
                PdfDocument.Merge(doc, mergeOption, dobj);
            }
            else
            {
                PdfDocument.Merge(doc, dobj);
            }

            PdfDocument.Merge(doc, dobj);

            if (InsideBrowser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }
        //method to get menu for restaurant
        private void PopulateMenuTab(JObject json)
        {
            if (json["Menus"].Type == JTokenType.Null || string.IsNullOrEmpty(json["Menus"].ToString()) || !json["Menus"].Any())
            {
                pdfViewerControl.IsVisible = false;
                MenuLabel.Text             = "No Menus Currently Available.";
            }
            else
            {
                int menuLength = json["Menus"].Count();
                //Create a new PDF document
                PdfDocument document = new PdfDocument();
                var         listMenu = new List <byte[]>();

                for (int i = 0; i < menuLength; i++)
                {
                    //Provide the PDF document URL in the below overload.
                    var pdfUrl = json["Menus"][i]["StorageUrl"].ToString();

                    try
                    {
                        //Returns the PDF document from the given URL
                        var documenStream = DownloadPdfStream(pdfUrl);
                        listMenu.Add(documenStream);
                    }
                    catch (WebException wex)
                    {
                        if (wex.Source != null)
                        {
                            Console.WriteLine("WebException source: {0}", wex.Source);
                        }
                        throw;
                    }
                }

                PdfMergeOptions mergeOptions = new PdfMergeOptions
                {
                    //Enable Optimize Resources
                    OptimizeResources = true
                };

                //Merge the documents
                PdfDocumentBase.Merge(document, mergeOptions, listMenu.ToArray());

                //Save the PDF document to stream
                MemoryStream stream = new MemoryStream();

                document.Save(stream);

                //Close the documents
                document.Close(true);

                pdfViewerControl.LoadDocument(stream);
            }
        }
Exemple #3
0
        public ActionResult MergeDocuments(string InsideBrowser, string OptimizeResources)
        {
            string basePath = _hostingEnvironment.WebRootPath;
            string dataPath = string.Empty;

            dataPath = basePath + @"/PDF/";

            //Read the file
            FileStream file1 = new FileStream(dataPath + "HTTP Succinctly.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            FileStream file2 = new FileStream(dataPath + "HTTP Succinctly.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            //Load the documents as streams
            PdfLoadedDocument doc1 = new PdfLoadedDocument(file1);
            PdfLoadedDocument doc2 = new PdfLoadedDocument(file2);

            object[]    dobj = { doc1, doc2 };
            PdfDocument doc  = new PdfDocument();

            if (OptimizeResources == "OptimizeResources")
            {
                PdfMergeOptions mergeOption = new PdfMergeOptions();
                mergeOption.OptimizeResources = true;
                PdfDocument.Merge(doc, mergeOption, dobj);
            }
            else
            {
                PdfDocument.Merge(doc, dobj);
            }

            MemoryStream stream = new MemoryStream();

            //Save the PDF document
            doc.Save(stream);

            stream.Position = 0;

            //Close the PDF document
            doc.Close(true);
            doc1.Close(true);
            doc2.Close(true);

            //Download the PDF document in the browser.
            FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");

            fileStreamResult.FileDownloadName = "MergedPDF.pdf";
            return(fileStreamResult);
        }
Exemple #4
0
        private void btnMerge_Click(object sender, System.EventArgs e)
        {
            string[] paths = { txtDoc1.Tag.ToString(), txtDoc2.Tag.ToString() };

            PdfDocument doc;

            if (this.optimizeResources.Checked)
            {
                PdfMergeOptions mergeOption = new PdfMergeOptions();
                mergeOption.OptimizeResources = true;
                doc = PdfDocument.Merge(paths, mergeOption);
            }
            else
            {
                doc = PdfDocument.Merge(paths);
            }

            doc.Save("Sample.pdf");

            //Message box confirmation to view the created PDF document.
            if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                //Launching the PDF file using the default Application.[Acrobat Reader]
#if NETCORE
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf")
                {
                    UseShellExecute = true
                };
                process.Start();
#else
                System.Diagnostics.Process.Start("Sample.pdf");
#endif
                this.Close();
            }
            else
            {
                // Exit
                this.Close();
            }
        }