iTextSharp的 - 在一个单一的页面合并两个PDF文件 [英] ITextSharp - merge two pdfs in a single page

查看:493
本文介绍了iTextSharp的 - 在一个单一的页面合并两个PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会把这个问题用简单的术语。

i will put this question in simple terms.

我有这个PDF:

 _____
|abcd |
|     |
|     |
|_____|

而这其中:

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

我要合并他们得到:

I want to merge them to get:

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

有可能使用iTextSharp的或任何其他免费工具?

It is possible using iTextSharp or any other free tool?

在此先感谢

推荐答案

这是个老问题...但如果​​有人在这里得到我再次的解决办法是这样...
我这样做很难codeD两成一个网页页面,所以这是基础知识
首先我旋转两个PDF文件后,我把它们合并在一起。

this is an old question... but if someone gets in here again my solution was this... i did this hard coded for two pages into one page so this is the basics first i rotated the two PDFs after that i merge them together

旋转两页使用:

 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();
        }
    }

现在你可以把它们合并在一起:

now you can merge them together:

        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(); }
    }

这篇关于iTextSharp的 - 在一个单一的页面合并两个PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆