【问题标题】:How can i identify pdf orientation whether landscape or portrait using itextsharp library in C#如何在 C# 中使用 itextsharp 库识别 pdf 方向是横向还是纵向
【发布时间】:2017-08-10 15:24:38
【问题描述】:

谁能知道如何使用C# 中的 itextsharp 库识别 pdf 方向是横向还是纵向。

下面是我的代码,它正在检索PDF 流并旋转图像,但我的问题是我们如何识别方向?

public static string ReadPdfFile(string fileName)
        {
        StringBuilder text = new StringBuilder();

        if (File.Exists(fileName))
        {




            byte[] bytes = System.IO.File.ReadAllBytes(fileName);



            using (MemoryStream ms = new MemoryStream())
            {
                Document doc = new Document();
                PdfWriter writer = PdfWriter.GetInstance(doc, ms);
                doc.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                PdfReader reader;
                reader = new PdfReader(bytes);
                int pages = reader.NumberOfPages;

                // loop over document pages
                for (int i = 1; i <= pages; i++)
                {

                    page = writer.GetImportedPage(reader, i);
                    //Rectangle pagesize = reader.GetPageSizeWithRotation(0);
                    AffineTransform scale = new AffineTransform(0, 1.0F, -1.0F, 0, 500, 500);
                    cb.AddTemplate(page,scale);
                }

                doc.Close();
                var rotatedFile =  ms.GetBuffer();

                ms.Flush();
                ms.Dispose();
                string filepath = @"D:\test2.pdf";
                File.Delete(filepath);
                using (FileStream Writer = new System.IO.FileStream(filepath, FileMode.Create, FileAccess.Write))
                {
                    Writer.Write(rotatedFile, 0, rotatedFile.Length);
                    string actualFilePath = "test2.pdf";
                    filepath = actualFilePath;
                }


            }

请帮助我,在此先感谢!

【问题讨论】:

    标签: c# pdf itext


    【解决方案1】:

    我评估页面的矩形大小以找出答案。需要记住的一点...您可以在同一个文件中有多个方向,因此您不能仅根据第一页来确定文件是横向的。如果所有页面都是横向的,则文件为横向,否则为混合方向文件。

    Rectangle currentPageRectangle = pdfReader.GetPageSizeWithRotation(<PageNumberHere>);
    if (currentPageRectangle.Width > currentPageRectangle.Height)
    {
        //page is landscape
    }
    else
    {
        //page is portrait
    }
    

    【讨论】:

    • 谢谢,你知道规模吗?就像如果 pdf 是肖像那么我们执行 'AffineTransform scale = new AffineTransform(0, 1.0F, -1.0F, 0, 500, 500);cb.AddTemplate(page,scale);'否则在横向检查中我不想给出任何比例,所以我想要默认比例我怎么能给出默认比例?
    • 对不起,我不知道规模。
    猜你喜欢
    • 2012-08-16
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多