private void ButtonToPDF_Click(object sender, RoutedEventArgs e)
        {
            string filename_pdf = TempFile.GenerateTempFilename("pdf");

            using (PdfDocument doc = new PdfDocument())
            {
                PdfPage page   = doc.Pages.Add();
                SizeF   bounds = page.GetClientSize();

                string filename_rtf = SaveToRTF();
                string text         = File.ReadAllText(filename_rtf);

                PdfMetafile             metafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);
                PdfMetafileLayoutFormat format   = new PdfMetafileLayoutFormat();

                // Allow the text to flow multiple pages without any breaks.
                format.SplitTextLines = true;
                format.SplitImages    = true;

                // Draw the image.
                metafile.Draw(page, 0, 0, format);

                doc.Save(filename_pdf);
            }

            Process.Start(filename_pdf);
        }
Beispiel #2
0
        public void ConvertToPdf(string source, string destination)
        {
            try
            {
                var doc    = new PdfDocument();
                var page   = doc.Pages.Add();
                var bounds = page.GetClientSize();
                var reader = new StreamReader(source, Encoding.ASCII);
                var text   = reader.ReadToEnd();
                reader.Close();

                var imageMetafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);
                var format        = new PdfMetafileLayoutFormat {
                    SplitTextLines = true
                };
                imageMetafile.Draw(page, 0, 0, format);

                doc.Save(destination);
                doc.Close(true);
            }
            catch (Exception ex)
            {
                FileLogger.SetLog(string.Format(ExceptionConstants.ConvertToPdf, source, ex.Message));
            }
        }
Beispiel #3
0
        public ActionResult PdfConformance(string Browser)
        {
            //Create a new document with PDF/A standard.
            PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A1B);

            //Add a page
            PdfPage page = doc.Pages.Add();

            //Create Pdf graphics for the page
            PdfGraphics g = page.Graphics;

            SizeF bounds = page.GetClientSize();

            //Read the RTF document
            StreamReader reader = new StreamReader(ResolveApplicationDataPath("PDF_A.rtf"), System.Text.Encoding.ASCII);
            string       text   = reader.ReadToEnd();

            reader.Close();

            //Convert it as metafile.
            PdfMetafile             metafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);
            PdfMetafileLayoutFormat format   = new PdfMetafileLayoutFormat();

            //Allow the text to flow multiple pages without any breaks.
            format.SplitTextLines = true;

            //Draw the image.
            metafile.Draw(page, 0, 0, format);


            //Stream the output to the browser.
            if (Browser == "Browser")
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }
            else
            {
                return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
            }
        }