ITextSharp - merge two pdfs in a single page

血红的双手。 提交于 2019-12-19 08:14:37

问题


i will put this question in simple terms.

I have this 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?

Thanks in advance


回答1:


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

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



回答2:


Yes... it's just Very Hard, even for a PDF Expert. 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.

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. Oh, its possible, but there is Almost Always a better way to do it.




回答3:


We used a product called PDFMerger that would do just this. It wasn't cheap however. We didn't really find anything else that could easily accomplish this.



来源:https://stackoverflow.com/questions/4084157/itextsharp-merge-two-pdfs-in-a-single-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!