Exemplo n.º 1
0
        private void btnImport_Click(object sender, System.EventArgs e)
        {
            PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(txtTemplate1.Tag.ToString());
            PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(txtTemplate2.Tag.ToString());
            PdfDocument       doc    = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();
                if ((template.Width < page.GetClientSize().Width) || (template.Height < page.GetClientSize().Height))
                {
                    g.DrawPdfTemplate(template, new PointF((page.GetClientSize().Width - template.Width) / 2, (page.GetClientSize().Height - template.Height) / 2));
                }

                else
                {
                    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
                }

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }

            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();
            }
        }
        public ActionResult OverlayDocuments(string InsideBrowser)
        {
            string basePath = _hostingEnvironment.WebRootPath;
            string dataPath = string.Empty;

            dataPath = basePath + @"/PDF/";

            string dataPath1 = dataPath + "BorderTemplate.pdf";
            string dataPath2 = dataPath + "SourceTemplate.pdf";

            Stream            stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read);
            FileStream        file    = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read);
            PdfLoadedDocument ldDoc1  = new PdfLoadedDocument(file);
            PdfLoadedDocument ldDoc2  = new PdfLoadedDocument(stream1);
            PdfDocument       doc     = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }
            MemoryStream stream = new MemoryStream();

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

            stream.Position = 0;

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

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

            fileStreamResult.FileDownloadName = "Overlay.pdf";
            return(fileStreamResult);
        }
        public ActionResult OverlayDocuments(string InsideBrowser)
        {
            string dataPath1, dataPath2;

            dataPath1 = ResolveApplicationDataPath("BorderTemplate.pdf");
            dataPath2 = ResolveApplicationDataPath("SourceTemplate.pdf");

            Stream            stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read);
            FileStream        file    = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read);
            PdfLoadedDocument ldDoc1  = new PdfLoadedDocument(file);
            PdfLoadedDocument ldDoc2  = new PdfLoadedDocument(stream1);
            PdfDocument       doc     = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }

            if (InsideBrowser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }
        /// <summary>
        /// Create a simple PDF document
        /// </summary>
        /// <returns>Return the created PDF document as stream</returns>
        public MemoryStream CreatePdfDocument()
        {
            string dataPath1 = ResolveApplicationPath("border-template.pdf");
            string dataPath2 = ResolveApplicationPath("source-template.pdf");

            Stream            stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read);
            FileStream        file    = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read);
            PdfLoadedDocument ldDoc1  = new PdfLoadedDocument(file);
            PdfLoadedDocument ldDoc2  = new PdfLoadedDocument(stream1);
            PdfDocument       doc     = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }
            MemoryStream stream = new MemoryStream();

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

            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;

            return(stream);
        }
        private async void GeneratePDF_Click(object sender, RoutedEventArgs e)
        {
            Stream            borderTemplateStream = typeof(OverlayDocuments).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.BorderTemplate.pdf");
            Stream            sourceTemplateStream = typeof(OverlayDocuments).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.SourceTemplate.pdf");
            PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(borderTemplateStream);
            PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(sourceTemplateStream);
            PdfDocument       doc    = new PdfDocument();

            for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i)
            {
                PdfPage     page = doc.Pages.Add();
                PdfGraphics g    = page.Graphics;

                PdfPageBase lpage    = ldDoc2.Pages[i];
                PdfTemplate template = lpage.CreateTemplate();
                if ((template.Width < page.GetClientSize().Width) || (template.Height < page.GetClientSize().Height))
                {
                    g.DrawPdfTemplate(template, new PointF((page.GetClientSize().Width - template.Width) / 2, (page.GetClientSize().Height - template.Height) / 2));
                }

                else
                {
                    g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
                }

                lpage    = ldDoc1.Pages[0];
                template = lpage.CreateTemplate();

                g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize());
            }
            MemoryStream stream = new MemoryStream();
            await doc.SaveAsync(stream);

            doc.Close(true);
            Save(stream, "OverlayDocument.pdf");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Load Pdf document from disk
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile("../../../../../../Data/PDFTemplate_N.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Create a new Pdf
            PdfDocument newPdf = new PdfDocument();

            //Remove all the margins
            newPdf.PageSettings.Margins.All = 0;

            //Set the page size of new Pdf
            newPdf.PageSettings.Width  = page.Size.Width;
            newPdf.PageSettings.Height = page.Size.Height / 2;

            //Add a new page
            PdfPageBase newPage = newPdf.Pages.Add();

            PdfTextLayout format = new PdfTextLayout();

            format.Break  = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;

            //Draw the page in the new page
            page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);

            //Save the Pdf document
            string output = "SplitAPageIntoMultipage_out.pdf";

            newPdf.SaveToFile(output);

            //Launch the document
            PDFDocumentViewer(output);
        }
Exemplo n.º 7
0
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf";

            string output = "ResetPageSize.pdf";

            //open pdf document
            PdfDocument originalDoc = new PdfDocument(input);

            //set margins
            PdfMargins margins = new PdfMargins(0);

            //create a new pdf document
            using (PdfDocument newDoc = new PdfDocument())
            {
                float scale = 0.8f;
                for (int i = 0; i < originalDoc.Pages.Count; i++)
                {
                    PdfPageBase page = originalDoc.Pages[i];

                    //use same scale to set width and height
                    float width  = page.Size.Width * scale;
                    float height = page.Size.Height * scale;

                    //add new page with expected width and height
                    PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height), margins);
                    newPage.Canvas.ScaleTransform(scale, scale);

                    //copy content of original page into new page
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
                }
                //save pdf document
                newDoc.SaveToFile(output);
            }

            //Launch the Pdf file.
            PDFDocumentViewer(output);
        }