示例#1
0
        private static void WinformPrintAsync(Object obj)
        {
            PrintParams printParams = (PrintParams)obj;

            lock (SyncObj)
            {
                HTMLPrinter htmlPrinter = new HTMLPrinter(printParams.PrinterName, printParams.SleepMillSeconds);
                htmlPrinter.WinFormPrint(printParams.HtmlFilePath, printParams.Landscape, printParams.Copies);
            }
        }
        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);
            }
        }
        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());
            }
        }