Exemple #1
0
    protected override void printWorker()
    {
        try
        {
            isPrinting       = true;
            officePowerPoint = new Microsoft.Office.Interop.PowerPoint.Application();
            officePowerPoint.DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone;
            Microsoft.Office.Interop.PowerPoint.Presentation doc = null;
            doc = officePowerPoint.Presentations.Open(
                filename,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoFalse);
            doc.PrintOptions.ActivePrinter     = printer;
            doc.PrintOptions.PrintInBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
            doc.PrintOptions.OutputType        = Microsoft.Office.Interop.PowerPoint.PpPrintOutputType.ppPrintOutputSlides;
            doc.PrintOut();
            System.Threading.Thread.Sleep(500);
            doc.Close();
            Marshal.FinalReleaseComObject(doc);
            doc           = null;
            workerPrintOk = true;
            isPrinting    = true;
        }
        catch (System.Exception ex)
        {
            isPrinting = false;

            Logging.Log("Unable to print PowerPoint file " + filename + ". Exception: " + ex.Message, Logging.LogLevel.Error);
            workerPrintOk = false;
        }
    }
        static void Main(string[] args)
        {
            string strPath =
                System.AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/");

            // Instantiate Object
            APServer.Server server = new APServer.Server();

            // Path and filename of output
            server.NewDocumentName = "Server.PowerPointPrinting.pdf";
            server.OutputDirectory = strPath;

            // Start the print job
            ServerDK.Results.ServerResult result = server.BeginPrintToPDF();
            if (result.ServerStatus == ServerDK.Results.ServerStatus.Success)
            {
                // Automate PowerPoint to print a document to activePDF Server
                // NOTE: You must add the 'Microsoft PowerPoint
                // <<version number>> Object Library' COM object as a reference
                // to your .NET application to access the PowerPoint Object.
                Microsoft.Office.Interop.PowerPoint._Application oPPT =
                    new Microsoft.Office.Interop.PowerPoint.Application();
                Microsoft.Office.Interop.PowerPoint.Presentation oPRES =
                    oPPT.Presentations.Open(
                        $"{strPath}Server.PowerPoint.Input.pptx",
                        Microsoft.Office.Core.MsoTriState.msoTrue,
                        Microsoft.Office.Core.MsoTriState.msoFalse,
                        Microsoft.Office.Core.MsoTriState.msoFalse);
                Microsoft.Office.Interop.PowerPoint.PrintOptions objOptions =
                    oPRES.PrintOptions;
                objOptions.ActivePrinter     = server.NewPrinterName;
                objOptions.PrintInBackground = 0;
                oPRES.PrintOut(1, 9999, "", 1, 0);
                oPRES.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;
                oPRES.Close();
                oPPT.Quit();

                // Wait(seconds) for job to complete
                result = server.EndPrintToPDF(waitTime: 30);
            }

            // Output result
            WriteResult(result);

            // Process Complete
            Console.WriteLine("Done!");
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
        public static void PrintDiscountCoupon(string companyName, string discountProductName, string discountValueOnProduct)
        {
            int    copiesToPrint         = 1;
            string printTemplateFileName = @"\Assets\Docs\PrinterReceipt.pptx";
            string qrCodeImageName       = @"\Assets\Images\QREncode.jpg";
            string printFileName         = @"\Assets\Docs\printReceipt.pptx";
            string printTemplateFilePath = Helper.GetAssetURI(printTemplateFileName);
            string qrCodeImagepath       = Helper.GetAssetURI(qrCodeImageName);
            string printReceiptFilePath  = Helper.GetAssetURI(printFileName);

            Microsoft.Office.Interop.PowerPoint.Presentation work = null;
            Microsoft.Office.Interop.PowerPoint.Application  app  = new Microsoft.Office.Interop.PowerPoint.Application();

            try
            {
                if (File.Exists(printReceiptFilePath))
                {
                    File.Delete(printReceiptFilePath);
                }
                if (File.Exists(qrCodeImagepath))
                {
                    File.Delete(qrCodeImagepath);
                }

                Microsoft.Office.Interop.PowerPoint.Presentations presprint = app.Presentations;
                work = presprint.Open(printTemplateFilePath, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
                work.PrintOptions.PrintInBackground = Microsoft.Office.Core.MsoTriState.msoFalse;
                Microsoft.Office.Interop.PowerPoint.Slide slide = work.Slides[1];
                foreach (var item in slide.Shapes)
                {
                    var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;

                    if (shape.HasTextFrame == MsoTriState.msoTrue)
                    {
                        if (shape.TextFrame.HasText == MsoTriState.msoTrue)
                        {
                            var textRange = shape.TextFrame.TextRange;
                            var text      = textRange.Text;
                            if (text.Contains("10%"))
                            {
                                text = text.Replace("10", discountValueOnProduct);
                                shape.TextFrame.TextRange.Text = text;
                            }
                            else if (text.Contains("Microsoft"))
                            {
                                text = text.Replace("Microsoft", companyName);
                                shape.TextFrame.TextRange.Text = text;
                            }
                            else if (text.Contains("Windows Phone 8"))
                            {
                                text = text.Replace("Windows Phone 8", discountProductName);
                                shape.TextFrame.TextRange.Text = text;
                            }
                        }
                    }
                    else
                    {
                        if (shape.Name.ToString() == "Picture 2")
                        {
                            shape.Delete();
                            //Add QRCode to print
                            RippleCommonUtilities.HelperMethods.GenerateQRCode("http://projectripple.azurewebsites.net/Ripple.aspx", qrCodeImagepath);
                            slide.Shapes.AddPicture(qrCodeImagepath, MsoTriState.msoFalse, MsoTriState.msoTrue, 560, 90, 80, 80);
                        }
                    }
                }

                work.SaveAs(printReceiptFilePath);
                work.PrintOut();
                work.Close();
                app.Quit();

                //delete the PrintReceipt File
                File.Delete(printReceiptFilePath);
                //Delete the QRCOde image
                File.Delete(qrCodeImagepath);
            }
            catch (System.Exception ex)
            {
                work.Close();
                app.Quit();
                //delete the PrintReceipt File
                File.Delete(printReceiptFilePath);
                //Delete the QRCOde image
                File.Delete(qrCodeImagepath);
                RippleCommonUtilities.LoggingHelper.LogTrace(1, "Went wrong in  Print Discount Coupon at Screen side: {0}", ex.Message);
            }
        }