繁体   English   中英

使用Itextsharp清除PDF的旋转

[英]Purge rotation of PDF using Itextsharp

我正在尝试清除旋转,如果pdf使用itextsharp旋转了。 使用Abc PDF可以正常工作,我想用Itextshap来实现。

Abc PDF代码工作代码

    public void PurgeRotation(string src, string dest)
    {
        float iPages, iPageCntr;
        Doc theSrc = new Doc();

        theSrc.Read(src);

        double iSourceX, iSourceY;
        iSourceX = theSrc.MediaBox.Width;
        iSourceY = theSrc.MediaBox.Height;
        iPages = theSrc.PageCount;


        Doc theDst = new Doc();
        theDst.MediaBox.Width = theSrc.MediaBox.Height;
        theDst.MediaBox.Height = theSrc.MediaBox.Width;

        double h = theSrc.MediaBox.Height;
        double w = theSrc.MediaBox.Width;
        //theDst.Rect.Rectangle=theSrc.MediaBox;(Not working);
        theDst.Rect.String = theSrc.MediaBox.ToString();

        XRect rect = new XRect();


        theDst.Transform.Rotate(270, 0, 0);
        theDst.Transform.Translate(0, iSourceX);

        for (iPageCntr = 1; iPageCntr <= iPages; iPageCntr++)
        {
            theDst.Page = theDst.AddPage();
            theDst.AddImageDoc(theSrc,(int) iPageCntr, null);

        }

        // Save new A3 format
        theDst.Save(dest);

        theSrc.Clear();
        theDst.Clear();



    }

我的Itextshap代码

     public void PurgeUsingItextshap(string src, string dest)
    {
        using (FileStream outStream = new FileStream(dest, FileMode.Create))
        {

            PdfReader reader = new PdfReader(src);
            int n = reader.NumberOfPages;
            PdfDictionary page;
            PdfNumber rotate;
            for (int p = 1; p <= n; p++)
            {
                page = reader.GetPageN(p);
                rotate = page.GetAsNumber(PdfName.ROTATE);

                if (rotate == null)
                {
                    page.Put(PdfName.ROTATE, new PdfNumber(0));
                }
                else
                {
                    page.Put(PdfName.ROTATE, new PdfNumber((rotate.IntValue + 0) % 270));
                }
                //}
                page.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(0));
            }
            //}
            iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream);
            stamper.Close();
            reader.Close();
        }


    }

如果pdf具有旋转,我想清除pdf的旋转

var pageRotation = reader.GetPageRotation(i);

我该如何实现? 非常感谢

此代码检查所有旋转的页面,并将旋转设置为零。

这是你想要的?

     static void Main(string[] args)
        {
            var reader = new PdfReader(@"C:\x.pdf");
            FileStream outStream = new FileStream(@"C:\x_new.pdf", FileMode.Create);

            PdfDictionary page;

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                page = reader.GetPageN(i);

                PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);

                if (rotate.IntValue != 0)
                {
                    Console.WriteLine("Rotated page found");
                    page.Put(PdfName.ROTATE, new PdfNumber(0));
                }
                else
                    Console.WriteLine("This page is not rotated");


            }

            PdfStamper stamp = new PdfStamper(reader, outStream);

            stamp.Close();
            reader.Close();
            outStream.Close();

            Console.Read();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM