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

推荐订阅源

D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
L
LangChain Blog
B
Blog
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
U
Unit 42
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
小众软件
小众软件
I
InfoQ
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
C
Check Point 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;
    }
}