示例#1
0
        static void Main(string[] args)
        {
            if (args.Length != 1 && args.Length != 2)
            {
                Console.WriteLine("Usage:\n\tEmfToPdf <input.emf> [output.pdf]\n");
                return;
            }

            var inputEmfFilePath = args[0];
            var outputFilepath   = args.Length == 2 ? args[1] : Path.ChangeExtension(inputEmfFilePath, ".pdf");

            using (var pdfDocument = new PdfDocument())
            {
#if true
                var page = pdfDocument.Pages.Add();
#else
                var section = pdfDocument.Sections.Add();
                section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
                var page = section.Pages.Add();
#endif
                var graphics = page.Graphics;

                var format = new PdfMetafileLayoutFormat
                {
                    SplitImages    = true,
                    SplitTextLines = true,
                };

                var metafile = new PdfMetafile(inputEmfFilePath);
                metafile.Draw(page, PointF.Empty, format);

                pdfDocument.Save(outputFilepath);
            }
        }
        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);
        }
示例#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));
            }
        }