简体   繁体   English

ITextSharp - 在一个页面中合并两个pdf

[英]ITextSharp - merge two pdfs in a single page

i will put this question in simple terms. 我将简单地提出这个问题。

I have this pdf: 我有这个pdf:

 _____
|abcd |
|     |
|     |
|_____|

And this one: 还有这个:

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

I want to merge them to get: 我想将它们合并以获得:

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

It is possible using iTextSharp or any other free tool? 可以使用iTextSharp或任何其他免费工具吗?

Thanks in advance 提前致谢

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 这是一个老问题...但如果有人再次进入这里,我的解决方案就是这个...我做了这个硬编码将两页翻译成一页,所以这是基础知识首先我旋转了两个PDF,之后我将它们合并在一起

to rotate the two pages use this: 旋转这两个页面使用这个:

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

Yes... it's just Very Hard, even for a PDF Expert. 是的......即使对于PDF专家来说,它也很难。 And by asking the question, you've shown that you aren't one... at least not yet. 通过提问,你已经表明你不是一个......至少现在还没有。 Pull this off and you'll be well on your way... But: 把它拉下来,你会很顺利......但是:

There's no easy way to determine a bounding box that surrounds all the content on a given page. 没有简单的方法来确定围绕给定页面上所有内容的边界框。 com.itextpdf.text.pdf.parser (or its # equivalent) has several classes that might help you along the way, but the bottom line is that PDF isn't designed to be parsable like this. com.itextpdf.text.pdf.parser(或它的#等价物)有几个类可以帮助你一路走下去,但最重要的是PDF不是像这样可以解析的。

I strongly recommend you try some other approach. 强烈建议你尝试其他方法。 Anything that involves the phrase "and then we get the information out of the PDF" needs an overhaul. 任何涉及短语“然后我们从PDF中获取信息”的内容都需要进行彻底检查。 Oh, its possible, but there is Almost Always a better way to do it. 哦,它可能,但几乎总是一个更好的方法来做到这一点。

We used a product called PDFMerger that would do just this. 我们使用了一种名为PDFMerger的产品。 It wasn't cheap however. 然而它并不便宜。 We didn't really find anything else that could easily accomplish this. 我们并没有真正找到任何其他可以轻易实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM