Beispiel #1
0
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();

            // Initialize PDF output stream
            using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF_out.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                // Initialize XPS input stream
                using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
                {
                    // Load XPS document form the stream
                    XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
                    // or load XPS document directly from file. No xpsStream is needed then.
                    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

                    // Initialize options object with necessary parameters.
                    Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()
                    {
                        JpegQualityLevel = 100,
                        ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,
                        TextCompression  = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,
                        PageNumbers      = new int[] { 1, 2, 6 }
                    };

                    // Create rendering device for PDF format
                    Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);

                    document.Save(device, options);
                }
            // ExEnd:1
        }
Beispiel #2
0
 ///<Summary>
 /// ConvertXpsToPdf method to convert Xps file to pdf
 ///</Summary>
 public Response ConvertXpsToPdf(string fileName, string folderName)
 {
     return(ProcessTask(fileName, folderName, ".pdf", false, false, delegate(string inFilePath, string outPath, string zipOutFolder)
     {
         using (FileStream pdfStream = new FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
             using (FileStream xpsStream = new FileStream(inFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
             {
                 XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
                 Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
                 document.Save(device, new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions());
             }
     }));
 }
        private List <string> GetDocumentPages(string file, string folderName, int currentPage)
        {
            List <string> lstOutput   = new List <string>();
            string        outfileName = "page_{0}";
            string        outPath     = Config.Configuration.OutputDirectory + folderName + "/" + outfileName;

            currentPage = currentPage - 1;
            Directory.CreateDirectory(Config.Configuration.OutputDirectory + folderName);
            string imagePath = string.Format(outPath, currentPage) + ".jpeg";

            if (System.IO.File.Exists(imagePath) && currentPage > 0)
            {
                lstOutput.Add(imagePath);
                return(lstOutput);
            }

            int i = currentPage;

            var filename = System.IO.File.Exists(Config.Configuration.WorkingDirectory + folderName + "/" + file)
                                ? Config.Configuration.WorkingDirectory + folderName + "/" + file
                                : Config.Configuration.OutputDirectory + folderName + "/" + file;

            using (FilePathLock.Use(filename))
            {
                try
                {
                    if (!System.IO.File.Exists(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf"))
                    {
                        Aspose.Page.Live.Demos.UI.Models.License.SetAsposePageLicense();
                        System.IO.FileStream pdfStream = new System.IO.FileStream(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf", System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
                        System.IO.FileStream psStream  = new System.IO.FileStream(Config.Configuration.WorkingDirectory + folderName + "/" + file, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                        try
                        {
                            if (file.ToLower().EndsWith(".xps"))
                            {
                                Page.XPS.XpsDocument document = new XpsDocument(psStream, new Page.XPS.XpsLoadOptions());
                                Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions();
                                Aspose.Page.XPS.Presentation.Pdf.PdfDevice      device  = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream, new Size(595, 842));
                                document.Save(device, options);
                            }
                            else
                            {
                                PsDocument document = new PsDocument(psStream);
                                Aspose.Page.EPS.Device.PdfSaveOptions options = new Aspose.Page.EPS.Device.PdfSaveOptions(true);
                                Aspose.Page.EPS.Device.PdfDevice      device  = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new Size(595, 842));
                                document.Save(device, options);
                            }
                        }
                        finally
                        {
                            psStream.Close();
                            pdfStream.Close();
                        }
                    }


                    PdfConverter objConverter = new PdfConverter();
                    objConverter.BindPdf(Config.Configuration.OutputDirectory + folderName + "/" + "outputPDF.pdf");
                    objConverter.DoConvert();
                    objConverter.CoordinateType = PageCoordinateType.CropBox;
                    if (currentPage < objConverter.PageCount)
                    {
                        lstOutput.Add(objConverter.PageCount.ToString());
                        i = 0;
                        while (objConverter.HasNextImage() && i < objConverter.PageCount)
                        {
                            objConverter.StartPage = (currentPage + 1);
                            objConverter.GetNextImage(imagePath, System.Drawing.Imaging.ImageFormat.Png);
                            lstOutput.Add(imagePath);
                            break;
                        }
                    }
                    objConverter.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return(lstOutput);
            }
        }