ITextSharp - merge two pdfs in a single page

后端 未结 3 690
再見小時候
再見小時候 2021-01-13 07:29

i will put this question in simple terms.

I have this pdf:

 _____
|abcd |
|     |
|     |
|_____|

And this one:

 __         


        
3条回答
  •  耶瑟儿~
    2021-01-13 07:38

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

提交回复
热议问题