Ejemplo n.º 1
0
        /// <summary>
        /// réorganise les pages d'un document pdf selon la liste d'indices de pages
        /// <param name="inputFile">fichier à ordonner</param>
        /// <param name="indexList">pages dans l'ordre voulu</param>
        /// </summary>
        public static void DoReorder(string inputFile, int[] indexList)
        {
            //var inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            //var output = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
            var output = Path.Combine(ServiceCfg.OutputFolderPath, Path.GetFileName(inputFile));

            //Bind a reader to our input file
            var reader = new PdfReader(inputFile);

            //Create our output file, nothing special here
            using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(reader.GetPageSizeWithRotation(1)))
                {
                    //Use a PdfCopy to duplicate each page
                    using (PdfCopy copy = new PdfCopy(doc, fs))
                    {
                        doc.Open();
                        copy.SetLinearPageMode();
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            copy.AddPage(copy.GetImportedPage(reader, i));
                        }
                        //Reorder pages
                        copy.ReorderPages(indexList);
                        doc.Close();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Re-order the pages of a given pdf and create a new PDF at the output file
        /// </summary>
        /// <param name="inputPdf">Input pdf</param>
        /// <param name="pageSelection">The new order (comma separated pagenumber).</param>
        /// <param name="outputPdf">Output pdf.</param>
        public static void ReOrderPages(string inputPdf, string pageSelection, string outputPdf)
        {
            //Bind a reader to our input file
            PdfReader reader = new PdfReader(inputPdf);

            //Create our output file, nothing special here
            using (FileStream fs = new FileStream(outputPdf, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(reader.GetPageSizeWithRotation(1)))
                {
                    //Use a PdfCopy to duplicate each page
                    using (PdfCopy copy = new PdfCopy(doc, fs))
                    {
                        doc.Open();
                        copy.SetLinearPageMode();
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            PdfImportedPage page = copy.GetImportedPage(reader, i);
                            //Check if tagged pdf
                            Boolean tagged = page.IsTagged();
                            copy.AddPage(page);
                        }

                        //Reorder pages
                        int[] newOrder = pageSelection.Split(',').Select(n => int.Parse(n)).ToArray();
                        copy.ReorderPages(newOrder);
                        doc.Close();
                    }
                }
            }
        }