c# - ITextSharp - 将两个 pdf 合并到一个页面中

标签 c# asp.net pdf itextsharp

我会用简单的术语来回答这个问题。

我有这个 pdf:

 _____
|abcd |
|     |
|     |
|_____|

还有这个:

 _____
|1234 |
|4567 |
|     |
|_____|

我想合并它们得到:

 _____
|abcd |
|1234 |
|4567 |
|_____|

可以使用 iTextSharp 或任何其他免费工具吗?

提前致谢

最佳答案

这是一个老问题...但如果有人再次进入这里,我的解决方案是... 我把这个硬编码为两页到一页所以这是基础知识 首先我旋转了两个 PDF,然后将它们合并在一起

旋转两个页面使用这个:

 public static void RotatePDF(string inputFile, string outputFile)
    {
        using (FileStream outStream = new FileStream(outputFile, FileMode.Create))
        {
            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(inputFile);
            iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream);

            iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(1);
            int desiredRot = 90; // 90 degrees clockwise from what it is now
            iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE);

            if (rotation != null)
            {
                desiredRot += rotation.IntValue;
                desiredRot %= 360; // must be 0, 90, 180, or 270
            }
            pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot));

            stamper.Close();
        }
    }

现在您可以将它们合并在一起:

        public static void MergeTwoPdfsToSingle(string inputFile1, string inputFile2, string outputFile)
    {
        //Step 1: Create a Docuement-Object
        Document document = new Document();
        try
        {
            //Step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFile, FileMode.Create));

            //Step 3: Open the document
            document.Open();

            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page1;
            PdfImportedPage page2;                

            // we create a reader for the document
            PdfReader reader1 = new PdfReader(inputFile1);
            PdfReader reader2 = new PdfReader(inputFile2);

            document.SetPageSize(reader1.GetPageSizeWithRotation(1));
            document.NewPage();

            page1 = writer.GetImportedPage(reader1, 1);                                

            page2 = writer.GetImportedPage(reader2, 1);                

            cb.AddTemplate(page1, 0, 0);
            //play around to find the exact location for the next pdf
            cb.AddTemplate(page2, 0, 300);
        }
        catch (Exception e) { throw e; }
        finally { document.Close(); }
    }

关于c# - ITextSharp - 将两个 pdf 合并到一个页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4084157/

相关文章:

asp.net - 使用 DataContext 静态变量

asp.net - IIS 多线程

java - 如何使用 thymeleaf 作为模板引擎生成 pdf 报告?

c# - 提供了无效的请求 URI。 (堆栈跟踪中没有有用的信息)

c# - .Net 4.0 中的动态程序集加载

html - HTTP 请求未经客户端身份验证方案 'Anonymous' 授权。从服务器收到的身份验证 header 是 'NTLM’

c# - 以 PDF 格式生成 Crystal 报告...如何在新选项卡或页面中打开?

javascript - 使用 Javascript 填充 PDF 输入字段

c# - 添加到数据库 : parameter not supplied but it's not required in SQL column

c# - c# 编译器是否决定自己使用 stackalloc?