• 使用iTextSharp读取PDF文档


    网上有很多获取pdf页数方法有读流判断type/page的但是经过实际测试文件太大的时候往往不是很准确,既然这样我们就不用费心去自己写了可以用itextsharp。

    这里是下载地址:http://sourceforge.net/projects/itextsharp/files/

    (1)创建一个Windows控制台应用程序,命名为ReadPdfDemo。
    (2)添加对iTextSharp的引用。
    (3)在“Program.cs”文件中添加如下引用。
    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    (4)直接修改“Program.cs”文件的代码如下。
    namespace ReadPdfDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("读取PDF文档");
                try
                {
                    // 创建一个PdfReader对象
                    PdfReader reader = new PdfReader(@"C:origin.pdf");
                    // 获得文档页数
                    int n = reader.NumberOfPages;
                    // 获得第一页的大小
                    Rectangle psize = reader.GetPageSize(1);
                    float width = psize.Width;
                    float height = psize.Height;
                    // 创建一个文档变量
                    Document document = new Document(psize, 50, 50, 50, 50);
                    // 创建该文档
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:Read.pdf", FileMode.Create));
                    // 打开文档
                    document.Open();
                    // 添加内容
                    PdfContentByte cb = writer.DirectContent;
                    int i = 0;
                    int p = 0;
                    Console.WriteLine("一共有 " + n + " 页.");
                    while (i < n)
                    {
                        document.NewPage();
                        p++;
                        i++;
                        PdfImportedPage page1 = writer.GetImportedPage(reader, i);
                        cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);
                        Console.WriteLine("处理第 " + i + " 页");
                        if (i < n)
                        {
                            i++;
                            PdfImportedPage page2 = writer.GetImportedPage(reader, i);
                            cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2);
                            Console.WriteLine("处理第 " + i + " 页");
                        }
                        if (i < n)
                        {
                            i++;
                            PdfImportedPage page3 = writer.GetImportedPage(reader, i);
                            cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0);
                            Console.WriteLine("处理第 " + i + " 页");
                        }
                        if (i < n)
                        {
                            i++;
                            PdfImportedPage page4 = writer.GetImportedPage(reader, i);
                            cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0);
                            Console.WriteLine("处理第 " + i + " 页");
                        }
                        cb.SetRGBColorStroke(255, 0, 0);
                        cb.MoveTo(0, height / 2);
                        cb.LineTo(width, height / 2);
                        cb.Stroke();
                        cb.MoveTo(width / 2, height);
                        cb.LineTo(width / 2, 0);
                        cb.Stroke();
                        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        cb.BeginText();
                        cb.SetFontAndSize(bf, 14);
                        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0 ? 1 : 0)), width / 2, 40, 0);
                        cb.EndText();
                    }
                    // 关闭文档
                    document.Close();
                }
                catch (Exception de)
                {
                    Console.Error.WriteLine(de.Message);
                    Console.Error.WriteLine(de.StackTrace);
                }
            }
        }
    }

  • 相关阅读:
    Win32汇编
    Boost ASIO 实现异步IO远控
    Python 使用oslo.vmware管理ESXI虚拟机
    Python 巡检接入钉钉机器人
    Django Ajax序列化与反序列化
    Nacos 认证绕过
    Lanproxy 遍历目录漏洞 CVE-2021-3019 附批量POC
    Apache Solr 全版本任意读取文件漏洞
    垂直水平居中的多种方法 主要的4种
    vue provide/inject 父组件如何给孙子组件传值
  • 原文地址:https://www.cnblogs.com/dylanblog/p/4229347.html
Copyright © 2020-2023  润新知