Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                using (Library lib = new Library())
                {
                    using (Document doc = new Document(textBox1.Text))
                    {
                        using (PrintUserParams userParams = new PrintUserParams())
                        {
                            userParams.NCopies = 1;

                            if (userParams.PosePrintDialog(doc))
                            {
                                userParams.PrintParams.ShrinkToFit = true;
                                userParams.ShrinkToFit             = userParams.PrintParams.ShrinkToFit;
                                userParams.PaperHeight             = PrintUserParams.UseMediaBox;
                                userParams.PaperWidth = PrintUserParams.UseMediaBox;

                                // If you un-comment the file path (binding) below, PDFL will print via the platform print driver to disk.
                                // The type of file produced when doing so (e.g., PS, PCL, PCLXL) depends upon the type of printer/driver.
                                //userParams.OutFileName = String.Format("{0}\\{1}.prn", finfo.Directory.FullName, System.IO.Path.GetFileNameWithoutExtension(finfo.FullName));

                                doc.Print(userParams, null, null);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception when printing...{0}{1}", Environment.NewLine, ex.Message);
            }
        }
Ejemplo n.º 2
0
        public static void PrintOnePDF(FileInfo finfo, SamplePrintCancel cancel, SamplePrintProgress progress)
        {
            try
            {
                progress.PrintingStarted = false;   // Used to keep the Print Progress form hidden until printing begins

                //
                // This implements the core prep/actions related to printing (from DLE)
                // In order to support GUI refresh they MUST occur on a background thread.
                // Notifications related to Cancel and Print Progress all occur via callback, elsewhere.
                //

                using (Library lib = new Library())
                    using (Document doc = new Document(finfo.FullName))
                    {
                        using (PrintUserParams userParams = new PrintUserParams())
                        {
                            userParams.NCopies = 1;

                            if (userParams.PosePrintDialog(doc))
                            {
                                userParams.PrintParams.ShrinkToFit = true;
                                userParams.ShrinkToFit             = userParams.PrintParams.ShrinkToFit;
                                userParams.PaperHeight             = PrintUserParams.UseMediaBox;
                                userParams.PaperWidth = PrintUserParams.UseMediaBox;

                                // If you un-comment the file path (binding) below, DLE will print via the platform print driver to disk.
                                // The type of file produced when doing so (e.g., PS, PCL, PCLXL) depends upon the type of printer/driver.
                                //userParams.OutFileName = String.Format("{0}\\{1}.prn", finfo.Directory.FullName, System.IO.Path.GetFileNameWithoutExtension(finfo.FullName));

                                progress.NumPages        = userParams.EndPage + 1; // important! (establishes the range of the page progress bar)
                                progress.PrintingStarted = true;                   // Just before print start, set this (so that the Print Progress form will show)

                                doc.Print(userParams, cancel, progress);           // Long running activity (cancel and progress will update via callback, elsewhere)
                            }
                        }
                    }
            }
            catch (Exception ex)
            {   // Some PDFs won't print (for various reasons). All code should be written to expect failure (never assume success by default).
                Console.WriteLine("Exception when printing...{0}{1}", Environment.NewLine, ex.Message);
            }
            finally
            {   // Tell the Print Progress form we're done...
                progress.ReportProgressDone();
            }
        }
        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PDFDoc == null)
            {
                MessageBox.Show("Please open a PDF document first before printing");
                return;
            }

            // Get some parameters
            PrintUserParams userParams = new PrintUserParams();
            // These are the "other" print parameters that hang off the user parameters.
            PrintParams printParams = userParams.PrintParams;

            // Print to a printer
            // For a list of the current print drivers available under WinNT, look at:
            // HKEY_CURRENT_USER\Software\Microsoft\WindowsNT\CurrentVersion\Devices
            // Or use the default printer

            if (userParams.PosePrintDialog(PDFDoc) == true)
            {
                PDFDoc.Print(userParams);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("PrintPDF Sample:");

            try
            {   // Printing may fail for reasons that have nothing to do with DLE / APDFL.
                // PDF documents may contain material that cannot be printed, etc. Given
                // that, it's always best to expect the printing step to fail and take
                // appropriate measures. Such care becomes critical for server-based 24/7
                // systems. This try block is simply here as an example (the steps one
                // would normally take are more involved).

                // To use DLE / APDFL one must always begin by initializing the library. This action
                // is expensive (both time and resource wise) and should only be done when necessary.
                // In a threaded product, a separate library must be instantiated on each thread.
                using (Library lib = new Library())
                {
                    Console.WriteLine(@"Library initialized.");

                    String sInput         = "../../Resources/Sample_Input/sample.pdf";
                    String outFileNamePrn = "../PrintPDF_out.prn";  // HINT: you'll find the file (in the working directory) next to PrintPDF.exe
                    string outFileNamePs  = "../PrintPDF_out.ps";   // HINT: you'll find the file (in the working directory) next to PrintPDF.exe

                    if (args.Length > 0)
                    {
                        sInput = args[0];
                    }

                    Console.WriteLine("Input file: " + sInput + ". Writing to output " + outFileNamePrn + " and " + outFileNamePs);

                    // Open a PDF document ("using" will automatically .Close and .Dispose it)...
                    using (Document doc = new Document(sInput))
                    {
                        #region Print To File (via Printer Driver)
                        // Platform print to a file...
                        //
                        // Printed output from the following method is composed by the selected
                        // printer's driver; along with assistance from DLE / APDFL. The actual
                        // output format will vary (e.g., PCL, PostScript, XPS, etc.). PostScript
                        // files produced via a PostScript driver and this method are NOT suitable
                        // for Distillation, Normalization, etc. All output from the method below
                        // will be optimized specifically for the target printer and is only suitable
                        // for direct consumption by that printer (or a print spooler that will
                        // ultimately transfer it to the target printer).

                        using (PrintUserParams userParams = new PrintUserParams())
                        {   // NOTE: userParams are only valid for ONE print job...
                            userParams.NCopies                 = 1;
                            userParams.ShrinkToFit             = true;
                            userParams.PrintParams.ExpandToFit = true;

#if !MONO
                            // This sets the file name that the printer driver knows about
                            // It's completely optional and only needs to be set if one wants
                            // a value that differs from the PDF file name (which is
                            // automatically provided by default). It is used for the
                            // %%Title comment in a PostScript file, etc.

                            //Isolate the filename from the path
                            int    index = sInput.LastIndexOf("/");
                            String nameOnly;

                            if (index != -1)
                            {
                                nameOnly = sInput.Substring(index + 1);
                            }
                            else
                            {
                                nameOnly = sInput;
                            }

                            userParams.InFileName = nameOnly;
#endif

                            // When print to file you MUST use 1+ page ranges...
                            // Page ranges allow complex collections of pages to be emitted.
                            // If you don't provide a page range, only the first page of the document will print.
                            //
                            // The code below creates 3 page ranges...
                            // As specified (below), DLE will emit up to 8 pages (1-4, 2, 4, 1, 3).
                            int upToFourPages = ((doc.NumPages > 4) ? 3 : doc.NumPages - 1); // 0-based
                            if (upToFourPages == 0)
                            {                                                                // the end page must always be >= 1
                                upToFourPages = 1;
                            }
                            IList <PageRange> pageRanges = new List <PageRange>();
                            pageRanges.Add(new PageRange(0, upToFourPages, PageSpec.AllPages));          // p1-4
                            if (doc.NumPages > 1)
                            {                                                                            // you can't ask for even or odd pages from a 1 page document
                                pageRanges.Add(new PageRange(0, upToFourPages, PageSpec.OddPagesOnly));  // p1,3
                                pageRanges.Add(new PageRange(0, upToFourPages, PageSpec.EvenPagesOnly)); // p2,4
                            }
                            PrintParams printParams = userParams.PrintParams;
                            printParams.PageRanges = pageRanges;

                            // Why .prn? Because we're printing via the printer driver (here)
                            // and cannot tell what type of output the driver will produce.
                            // PostScript drivers will produce PostScript output. PCL drivers
                            // will produce PCL or PCL_XL. Similarly, XPS drivers may produce
                            // XPS and PDF drivers may produce PDF (directly).
                            //
                            // PostScript produced via the PrintToFile method is NOT equivalent
                            // to PostScript produced via the ExportAsPostScript method.

                            Console.WriteLine(String.Format("Printing to File: {0}", outFileNamePrn));
                            doc.PrintToFile(userParams, null /* for cancel see the PrintPDFGUI sample */, new SamplePrintProgressProc(), outFileNamePrn);
                        }
                        #endregion

                        #region Print To Printer (via Printer Driver)
                        // Now let's, print directly to a printer (without ui)...
                        //
                        // Printed output from the following method is composed by the selected
                        // printer's driver; along with assistance from DLE / APDFL. The actual
                        // output format will vary (e.g., PCL, PostScript, XPS, etc.). PostScript
                        // files produced via a PostScript driver and this method are NOT suitable
                        // for Distillation, Normalization, etc. All output from the method below
                        // will be optimized specifically for the target printer and is only suitable
                        // for direct consumption by that printer (or a print spooler that will
                        // ultimately transfer it to the target printer).

                        using (PrintUserParams userParams = new PrintUserParams())
                        {   // NOTE: userParams are only valid for ONE print job...
                            userParams.NCopies                 = 1;
                            userParams.ShrinkToFit             = true;
                            userParams.PrintParams.ExpandToFit = true;

#if !MONO
                            // This sets the file name that the printer driver knows about
                            // It's completely optional and only needs to be set if one wants
                            // a value that differs from the PDF file name (which is
                            // automatically provided by default). It is used for the
                            // %%Title comment in a PostScript file, etc.

                            //Isolate the filename from the path
                            int    index = sInput.LastIndexOf("/");
                            String nameOnly;

                            if (index != -1)
                            {
                                nameOnly = sInput.Substring(index + 1);
                            }
                            else
                            {
                                nameOnly = sInput;
                            }

                            userParams.InFileName = nameOnly;
#endif

                            // When printing (directly) to a printer you cannot use page ranges...
                            // You need to use userParams.StartPage and EndPage (to request a single,
                            // linear sequence) instead. If you do not specify anything the entire
                            // document will print (i.e., all pages).
                            //
                            // As specified (below), DLE will print up to 4 pages (1-4).
                            int upToFourPages = ((doc.NumPages > 4) ? 3 : doc.NumPages - 1);
                            userParams.StartPage = 0;             // 0-based
                            userParams.EndPage   = upToFourPages; // 0-based

                            // Use the default printer...
                            userParams.UseDefaultPrinter(doc);

                            // ...or uncomment the code below (and comment out the code above) and
                            // assign the name of a (accessible) printer to userParams.DeviceName so
                            // as to explicitly target a printer.
                            //userParams.DeviceName = @"Change Me to a valid Printer Name";

                            Console.WriteLine(String.Format("Printing (direct) to Printer: {0}", userParams.DeviceName));
                            doc.Print(userParams, null /* for cancel see the PrintPDFGUI sample */, new SamplePrintProgressProc());
                        }
                        #endregion

                        #region Export As PostScript (Device Independent, DSC Compliant)
                        // Export as (DLE/PDFL composed) PostScript...
                        //
                        // PostScript files produced via this *export* method are suitable
                        // for Distillation, Normalization, etc. If a PostScript Printer
                        // Description (PPD) is registered (not shown here) the output will
                        // be device specific. Otherwise, it will be device independent.
                        // Consult the PostScript Language Document Structuring Conventions
                        // for more information about the conformance / structure of the
                        // exported PostScript.
                        //
                        // https://partners.adobe.com/public/developer/en/ps/5001.DSC_Spec.pdf

                        using (PrintUserParams userParams = new PrintUserParams())
                        {   // NOTE: userParams are only valid for ONE print job...
                            userParams.NCopies = 1;

                            // When export as PostScript you MUST use 1+ page ranges...
                            // Page ranges allow complex collections of pages to be emitted.
                            //
                            // The code below exports the entire PDF document (to a PostScript file)...
                            IList <PageRange> pageRanges = new List <PageRange>();
                            pageRanges.Add(new PageRange(0, (((doc.NumPages > 1)) ? doc.NumPages - 1 : 1), PageSpec.AllPages)); // all pages
                            PrintParams printParams = userParams.PrintParams;
                            printParams.PageRanges = pageRanges;

                            Console.WriteLine(String.Format("Exporting as PostScript to File: {0}", outFileNamePs));
                            doc.ExportAsPostScript(userParams, null /* for cancel see the PrintPDFGUI sample */, new SamplePrintProgressProc(), outFileNamePs);
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"An exception occurred. Here is the related information:");
                Console.Write(ex.ToString());
            }
        }
Ejemplo n.º 5
0
        private void Print(Stream pdfStream, string printerName)
        {
            using (var lib = new Library())
            {
                using (var doc = new Document(pdfStream))
                {
                    var userParams = new PrintUserParams
                    {
                        DeviceName = printerName,
                        StartPage = 0,
                        EndPage = doc.NumPages
                    };

                    userParams.PrintParams.SetPageSize = false;
                    userParams.ShrinkToFit = true;
                    doc.Print(userParams);
                }
            }
        }