Showing posts with label PDF. Show all posts
Showing posts with label PDF. Show all posts

Monday, April 22, 2019

C# - Pdf rotate using iTextSharp

In C#, with opensource iTextSharp can be find in nuget package, we can easily rotate each pdf pages and save as new file, use below code

using iTextSharp.text;
using iTextSharp.text.pdf;

using System.IO;


namespace PdfRotate
{
    class Program
    {
        static void Main(string[] args)
        {
            //INFO: Change input and ouput path here
            string pdfFilePath = @"C:\Files\sample.pdf";
            string outputPath = @" C:\Files\sampleoutput.pdf ";

            //INFO: Change rotate degree here to 90, 180 etc
            RotatePages(pdfFilePath, outputPath, 180);           
        }

        private static void RotatePages(string pdfFilePath, string outputPath, int rotateDegree)
        {
            PdfReader reader = new PdfReader(pdfFilePath);
            int pagesCount = reader.NumberOfPages;

            for (int n = 1; n <= pagesCount; n++)
            {
                PdfDictionary page = reader.GetPageN(n);
                PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);
                int rotation =
                        rotate == null ? rotateDegree : (rotate.IntValue + rotateDegree) % 360;

                page.Put(PdfName.ROTATE, new PdfNumber(rotation));
            }

            PdfStamper stamper = new PdfStamper(reader, new FileStream(outputPath, FileMode.Create));
            stamper.Close();
            reader.Close();
        }
    }
}


C# - Pdf split using iTextSharp

In C#, with opensource iTextSharp can be find in nuget package, we can easily split pdf into mulitple files per page. use below code

using iTextSharp.text;
using iTextSharp.text.pdf;

using System.IO;

namespace PdfSplit
{
    class Program
    {

    
    static void Main(string[] args)
       {
            string pdfFilePath = @"C:\Files\sample.pdf";

            string outputPath = @"C:\Files\";

            SplitPages(pdfFilePath, outputPath);
             }

    private static void SplitPages(string pdfFilePath, string outputPath)
        {
           // Intialize a new PdfReader instance with the contents of the source Pdf file:
            PdfReader reader = new PdfReader(pdfFilePath);

            FileInfo file = new FileInfo(pdfFilePath);
            string pdfFileName = file.Name.Substring(0, file.Name.LastIndexOf(".")) + "-";

            Program obj = new Program();

            int pageNameSuffix = 0;
            for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
            {
                pageNameSuffix++;
                string newPdfFileName = string.Format(pdfFileName + "{0}", pageNameSuffix);
                obj.SplitAndSaveInterval(pdfFilePath, outputPath, pageNumber, newPdfFileName);
            }
        }

   private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int pagenumber, string pdfFileName)
        {
            using (PdfReader reader = new PdfReader(pdfFilePath))
            {
                Document document = new Document();
                PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
                document.Open();

                copy.AddPage(copy.GetImportedPage(reader, pagenumber));

                document.Close();
            }

        }
    }
 }