コード例 #1
0
        public static PdfScaledTemplateInfo ScalePdfContent(Image image, PdfResizeInfo resizeInfo, PdfScalingOptions scalingOptions = null)
        {
            var currentSize     = new Rectangle(image);
            var contentTemplate = new ImgTemplate(image);

            return(ScalePdfContent(contentTemplate, currentSize, resizeInfo, scalingOptions));
        }
コード例 #2
0
        public static PdfScaledTemplateInfo ScalePdfContent(ImgTemplate contentTemplate, Rectangle currentContentSize, PdfResizeInfo resizeInfo, PdfScalingOptions scalingOptions = null)
        {
            if (contentTemplate == null)
            {
                throw new ArgumentNullException(nameof(contentTemplate), "Pdf Content to be resized cannot be null.");
            }
            if (currentContentSize == null)
            {
                throw new ArgumentNullException(nameof(currentContentSize), "Current size of Pdf Content cannot be null.");
            }
            if (resizeInfo == null)
            {
                throw new ArgumentNullException(nameof(resizeInfo), "Target Pdf size information cannot be null.");
            }

            //Initialize with Default Scaling Options...
            var pdfScalingOptions = scalingOptions ?? PdfScalingOptions.Default;

            //Don't mutate the original value...
            //var targetSize = targetDoc.PageSize;
            var pageSize   = resizeInfo.PageSize;
            var marginSize = resizeInfo.MarginSize;

            var targetWidth  = pageSize.Width - marginSize.Left - marginSize.Right;
            var targetHeight = pageSize.Height - marginSize.Top - marginSize.Bottom;

            bool scalingEnabled = false;

            //Flag to denote which dimension is the constraining one
            //NOTE: This changes based on if we are scaling the size Up or Down!
            switch (pdfScalingOptions.PdfContentScalingMode)
            {
            case PdfResizeScalingMode.ScaleAlways:
                scalingEnabled = true;
                break;

            case PdfResizeScalingMode.ScaleDownOnly:
                scalingEnabled = currentContentSize.Width > targetWidth || currentContentSize.Height > targetHeight;
                break;

            case PdfResizeScalingMode.ScaleUpOnly:
                scalingEnabled = currentContentSize.Width < targetWidth || currentContentSize.Height < targetHeight;
                break;
            }

            //BBernard
            //If Enabled then we handle dynamic rotation based on the input source rotation (if specified)...
            //NOTE: Rotation MUST be handled BEFORE the Scaling to ensure we scale with the appropriate Width & Height
            //      of the existing content!
            if (pdfScalingOptions.EnableDynamicRotationHandling && currentContentSize.Rotation > 0)
            {
                //BBernard
                //Compute Adjustment Rotation for correct rotation...if the input Source has a Rotation Already!
                //NOTE: Specific test scenarios have shown that to rotate content using iText the rotation direction
                //      appears to be counter clockwise in iText, vs the noted rotation of input documents.
                //      Therefore we compute the counter-clockwise equivalent to rotate teh content as we expect in these test cases...
                //For Example: If the input document has a rotation of 270, we need to actually rotate 90
                //              degrees via iTextSharp to ensure proper orientation.
                var rotationDegrees = ((360 - currentContentSize.Rotation) % 360);
                contentTemplate.RotationDegrees = rotationDegrees;
            }

            if (scalingEnabled)
            {
                //Determine if we should force Resize into the specified Target Size or if we should enable Landscape orientation
                //  (e.g. 90 degree rotation) to accomodate pages that are wider than taller...
                //NOTE: If landscape orientation is enabled then we only need to make adjustments if all of the following are true:
                //      a) the current size is using landscape orientation
                //      b) and the target size is not already in the same orientation (e.g. landscape)
                if (pdfScalingOptions.EnableDynamicLandscapeOrientation &&
                    currentContentSize.Width > currentContentSize.Height &&
                    targetHeight > targetWidth)
                {
                    pageSize = pageSize.Rotate();
                    //Don't mutate the original value...
                    //var targetSize = targetDoc.PageSize;
                    targetWidth  = pageSize.Width - marginSize.Left - marginSize.Right;
                    targetHeight = pageSize.Height - marginSize.Top - marginSize.Bottom;
                }

                //Support Maintaining Aspect Ratio...
                if (pdfScalingOptions.MaintainAspectRatio)
                {
                    contentTemplate.ScaleToFit(targetWidth, targetHeight);
                }
                //Support Skewed Resizing...
                else
                {
                    contentTemplate.ScaleAbsolute(targetWidth, targetHeight);
                }
            }

            //If Enabled then we adjust the position to center the content on the Page...
            if (pdfScalingOptions.EnableContentCentering)
            {
                var x = (targetWidth - contentTemplate.ScaledWidth) / 2;
                var y = (targetHeight - contentTemplate.ScaledHeight) / 2;
                contentTemplate.SetAbsolutePosition(x, y);
            }

            return(new PdfScaledTemplateInfo()
            {
                ScaledPdfContent = contentTemplate,
                ScaledPageSize = pageSize
            });
        }
コード例 #3
0
        public static PdfScaledTemplateInfo ScalePdfContent(PdfImportedPage currentPage, Rectangle currentContentSize, PdfResizeInfo resizeInfo, PdfScalingOptions scalingOptions = null)
        {
            var contentTemplate = new ImgTemplate(currentPage);

            return(ScalePdfContent(contentTemplate, currentContentSize, resizeInfo, scalingOptions));
        }