Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have been given one requirement in ASP.net i.e. to send the documents (Which include .doc,.docx,.xlsx,.pdf) directly to printer from the server end where we are not allowed to install MS Office and Adobe.
I have been suggested to use XPS Api to give the input directly to printer, I found code for this.
C#
public class Class1
    {
        public void printxps()
        {
            Thread printingThread = new Thread(BatchXPSPrinter.PrintXPS);
            // Set the thread that will use PrintQueue.AddJob to single threading.
            printingThread.SetApartmentState(ApartmentState.STA);
            // Start the printing thread. The method passed to the Thread  
            // constructor will execute.
            printingThread.Start();
        }
    }

    public class BatchXPSPrinter
    {
        public static void PrintXPS()
        {
            // Create print server and print queue.
            LocalPrintServer localPrintServer = new LocalPrintServer();

            PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

            // Prompt user to identify the directory, and then create the directory object.
            String directoryPath = "D:\\";
            DirectoryInfo dir = new DirectoryInfo(directoryPath);

            // If the user mistyped, end the thread and return to the Main thread. 
            if (!dir.Exists)
            {
                throw new Exception("Invalid Drive");
            }
            else
            {
                // If there are no XPS files in the directory, end the thread  
                // and return to the Main thread. 
                if (dir.GetFiles("*.xps").Length == 0)
                {
                    throw new Exception("Invalid File");
                }
                else
                {
                    // Batch process all XPS files in the directory. 
                    foreach (FileInfo f in dir.GetFiles("*.xps"))
                    {
                        String nextFile = directoryPath + "\\" + f.Name;
                       // Console.WriteLine("Adding {0} to queue.", nextFile);

                        try
                        {
                            // Print the Xps file while providing XPS validation and progress notifications.
                           PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile,false);
                        }
                        catch (Exception e)
                        {
                        }
                    }// end for each XPS file
                }//end if there are no XPS files in the directory
            }//end if the directory does not exist
        }// end PrintXPS method

    }

However I am facing problem to convert the MS office and PDF documents to XPS format.
To convert the MS Office documents to XPS, everywhere I found the code samples that are making use of Interop Assemblies for which we must install MS Office on the server.
Request you to know let me know the approach I can follow to convert the Office Documents to XPS format without using Interop dll.
Thank you.
Posted
Updated 19-Mar-15 21:31pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900