开发者

ITextSharp - merge two pdfs in a single page

开发者 https://www.devze.com 2023-01-23 10:26 出处:网络
i will put this question in simple terms. I have this pdf: _____ |abcd | _____| And this one: _____ |1234 |

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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消