惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
博客园_首页
T
Tailwind CSS Blog
The Cloudflare Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - singleblue

IIS7 asp.net配置默认文档的错误 画图工具 UI层方案征集 B/S项目架构探讨 如何做自动录入数据功能 Failed to execute request because the App-Domain could not be created 解决ie7以下浏览器PNG图片背景不透明且链接失效的办法 拼接字符串的效率(转载) 最近准备用的数据访问类,欢迎拍砖! 关于JSON的文章 取水晶报表的总页数 javascript操作cookie javascript全角半角问题 以支持多种浏览器的方式创建XMLHttpRequest对象 四种引用 使用AjaxPro与Session交互时遇到一个问题 window.print() 急求datastage 控件隐藏(Visible=false)的一个问题
求助:如何合并两个PDF文档
singleblue · 2008-06-20 · via 博客园 - singleblue

如题

已经找到解决方法,一直没时间传上来和大家分享一下,

使用的是itextsharp

    /// <summary>
    /// 合并PDF文件
    /// </summary>
    /// <param name="lstFiles">要合并的文件列表</param>
    /// <returns></returns>
    private bool MergePDF(List<string> lstFiles)
    {
        bool bReturn = false;
        try
        {
            int f = 1;
            PdfReader reader = new PdfReader(lstFiles[f]);           
            int n = reader.NumberOfPages;
            Document document = new Document(reader.GetPageSizeWithRotation(1));           
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(lstFiles[0], FileMode.Create));           
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;
            int rotation;
            while (f < lstFiles.Count)
            {
                int i = 0;
                while (i < n)
                {
                    i++;
                    document.SetPageSize(reader.GetPageSizeWithRotation(i));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                }
                f++;
                if (f < lstFiles.Count)
                {
                    reader = new PdfReader(lstFiles[f]);                   
                    n = reader.NumberOfPages;                   
                }
            }
            document.Close();
            bReturn = true;           
        }
        catch
        {
            bReturn = false;           
        }
        return bReturn;
    }
}