Wyśrodkowanie strony pdfimported w iTextSharp

Dołączam pliki PDF razem za pomocą poniższej funkcji za pomocą iTextSharp. Działa dobrze. Jedynym problemem jest to, że pliki PDF większe niż ustawiony rozmiar dokumentu (A4) są skalowane i umieszczane w lewym dolnym rogu dokumentu. Chciałbym to wyśrodkować. Czy ktoś może wskazać mi właściwy kierunek, aby to osiągnąć? Twoje zdrowie.

    private void appendPDF(appendDoc doc)
    {
        PdfContentByte pdfContentByte = pdfWriter.DirectContent;
        PdfReader pdfReader = null;

        if (doc.MemoryStream != null && doc.MemoryStream.CanRead)
        {
            pdfReader = new PdfReader(doc.MemoryStream);
        }
        else if (File.Exists(doc.FullFilePath))
        {
            pdfReader = new PdfReader(doc.FullFilePath);
        }

        if (pdfReader != null)
        {
            for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
            {                    
                PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, pageIndex);

                float importedPageXYRatio = importedPage.Width / importedPage.Height;

                if (XYRatio > 1f)
                {
                   iTextDocument.SetPageSize(PageSize.A4.Rotate());
                }
                else
                {
                   iTextDocument.SetPageSize(PageSize.A4);
                }

                iTextDocument.NewPage();
                pdfContentByte.AddTemplate(importedPage, 0, 0);

            }
        }
    }

Edytować:

To było rozwiązanie, którego użyłem.

private void appendPDF(appendDoc doc)
    {
        PdfContentByte pdfContentByte = pdfWriter.DirectContent;
        PdfReader pdfReader = null;

        if (doc.MemoryStream != null && doc.MemoryStream.CanRead)
        {
            pdfReader = new PdfReader(doc.MemoryStream);
        }
        else if (File.Exists(doc.FullFilePath))
        {
            pdfReader = new PdfReader(doc.FullFilePath);
        }

        if (pdfReader != null)
        {
            for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
            {                    
                PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, pageIndex);

                float importedPageXYRatio = importedPage.Width / importedPage.Height;

                if (XYRatio > 1f)
                {
                   iTextDocument.SetPageSize(PageSize.A4.Rotate());
                }
                else
                {
                   iTextDocument.SetPageSize(PageSize.A4);
                }                  

                iTextDocument.NewPage();

                var truePageWidth = iTextDocument.PageSize.Width - iTextDocument.LeftMargin - iTextDocument.RightMargin;
                var truePageHeight = iTextDocument.PageSize.Height - iTextDocument.TopMargin - iTextDocument.BottomMargin;

                var x = (truePageWidth - importedPage.Width) / 2 + iTextDocument.RightMargin;
                var y = (truePageHeight - importedPage.Height) / 2 + iTextDocument.BottomMargin;

                pdfContentByte.AddTemplate(importedPage, x, y);                    
            }
        }
    }

questionAnswers(1)

yourAnswerToTheQuestion