ITextSharp - combine two pdf files on one page

I posed this question in simple words.

I have this pdf:

 _____
|abcd |
|     |
|     |
|_____|

And this one:

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

I want to combine them to get:

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

Can I use iTextSharp or any other free tool?

Thanks in advance

+5
source share
3 answers

This is an old question ... but if someone gets here again, I solved it ... I made it hardcoded for two pages on one page, so this is the basics first I rotated the two PDF files, after which I combined them together

to rotate 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 combine 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(); }
    }
+3
source

... , PDF Expert. , , , ... . , ... :

, . com.itextpdf.text.pdf.parser( # ) , , PDF .

. , " PDF", . , , .

0

We used a product called PDFMerger that would do just that. However, it was not cheap. We did not find anything else that could easily accomplish this.

0
source

All Articles