Пример #1
0
        protected void createPdfButton_Click(object sender, EventArgs e)
        {
            // Create a PDF document
            Document pdfDocument = new Document();

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            try
            {
                // The result of adding elements to PDF document
                AddElementResult addElementResult = null;

                // The titles font used to mark various sections of the PDF document
                PdfFont titleFont    = pdfDocument.AddFont(new Font("Times New Roman", 12, FontStyle.Bold, GraphicsUnit.Point));
                PdfFont subtitleFont = pdfDocument.AddFont(new Font("Times New Roman", 8, FontStyle.Bold, GraphicsUnit.Point));

                // The position on X anf Y axes where to add the next element
                float yLocation = 5;
                float xLocation = 5;

                // Create a PDF page in PDF document
                PdfPage pdfPage = pdfDocument.AddPage();

                // Add section title
                TextElement titleTextElement = new TextElement(xLocation, yLocation, "Images Scaling", titleFont);
                titleTextElement.ForeColor = Color.Black;
                addElementResult           = pdfPage.AddElement(titleTextElement);
                yLocation = addElementResult.EndPageBounds.Bottom + 10;
                pdfPage   = addElementResult.EndPdfPage;

                float titlesYLocation = yLocation;

                // Add an unscaled image

                // Add section title
                TextElement subtitleTextElement = new TextElement(xLocation, titlesYLocation, "Unscaled small image with normal resolution", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                addElementResult = pdfPage.AddElement(subtitleTextElement);

                pdfPage = addElementResult.EndPdfPage;
                float imagesYLocation = addElementResult.EndPageBounds.Bottom + 10;

                string       imagePath            = Server.MapPath("~/DemoAppFiles/Input/Images/picture_small.jpg");
                ImageElement unscaledImageElement = new ImageElement(xLocation, imagesYLocation, imagePath);
                addElementResult = pdfPage.AddElement(unscaledImageElement);

                RectangleF scaledDownImageRectangle = new RectangleF(addElementResult.EndPageBounds.Right + 30, addElementResult.EndPageBounds.Y,
                                                                     addElementResult.EndPageBounds.Width, addElementResult.EndPageBounds.Height);

                // Add a large image scaled down to same size in PDF

                // Add section title
                subtitleTextElement           = new TextElement(scaledDownImageRectangle.X, titlesYLocation, "Scaled down large image has higher resolution", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                pdfPage.AddElement(subtitleTextElement);

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_large.jpg");
                ImageElement     scaledDownImageElement = new ImageElement(scaledDownImageRectangle.X, scaledDownImageRectangle.Y, scaledDownImageRectangle.Width, imagePath);
                AddElementResult scaledDownImageResult  = pdfPage.AddElement(scaledDownImageElement);

                // Add a border around the scaled down image
                RectangleElement borderElement = new RectangleElement(scaledDownImageRectangle);
                pdfPage.AddElement(borderElement);

                // Add an unscaled small image

                float columnX = scaledDownImageResult.EndPageBounds.Right + 30;

                // Add section title
                subtitleTextElement           = new TextElement(columnX, titlesYLocation, "Unscaled small image", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                pdfPage.AddElement(subtitleTextElement);

                imagePath            = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg");
                unscaledImageElement = new ImageElement(columnX, imagesYLocation, imagePath);
                AddElementResult unscaledImageResult = pdfPage.AddElement(unscaledImageElement);

                RectangleF unscaledImageRectangle = unscaledImageResult.EndPageBounds;

                // Add an enlarged image

                // Add section title
                subtitleTextElement           = new TextElement(columnX, unscaledImageRectangle.Bottom + 10, "Enlarged small image has lower resolution", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                AddElementResult enlargedImageTitle = pdfPage.AddElement(subtitleTextElement);

                float enlargedImageWidth = unscaledImageRectangle.Width + 35;

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg");
                ImageElement enlargedImageElement = new ImageElement(columnX, enlargedImageTitle.EndPageBounds.Bottom + 10, enlargedImageWidth, imagePath);
                // Allow the image to be enlarged
                enlargedImageElement.EnlargeEnabled = true;
                AddElementResult enalargedImageResult = pdfPage.AddElement(enlargedImageElement);

                yLocation = addElementResult.EndPageBounds.Bottom + 10;

                // Scale an image preserving the aspect ratio

                titlesYLocation = yLocation;

                // Add section title
                subtitleTextElement           = new TextElement(xLocation, titlesYLocation, "Scaled down image preserving aspect ratio", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                addElementResult = pdfPage.AddElement(subtitleTextElement);

                pdfPage   = addElementResult.EndPdfPage;
                yLocation = addElementResult.EndPageBounds.Bottom + 10;

                RectangleF boundingRectangle = new RectangleF(xLocation, yLocation, scaledDownImageRectangle.Width, scaledDownImageRectangle.Width);
                imagesYLocation = boundingRectangle.Y;

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg");
                ImageElement keepAspectImageElement = new ImageElement(boundingRectangle.X, imagesYLocation, boundingRectangle.Width, boundingRectangle.Width, true, imagePath);
                addElementResult = pdfPage.AddElement(keepAspectImageElement);

                borderElement           = new RectangleElement(boundingRectangle);
                borderElement.ForeColor = Color.Black;
                pdfPage.AddElement(borderElement);

                // Scale an image without preserving aspect ratio
                // This can produce a distorted image

                boundingRectangle = new RectangleF(addElementResult.EndPageBounds.Right + 30, addElementResult.EndPageBounds.Y, scaledDownImageRectangle.Width, scaledDownImageRectangle.Width);

                // Add section title
                subtitleTextElement           = new TextElement(boundingRectangle.X, titlesYLocation, "Scaled down image without preserving aspect ratio", subtitleFont);
                subtitleTextElement.ForeColor = Color.Navy;
                pdfPage.AddElement(subtitleTextElement);

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg");
                ImageElement notKeepAspectImageElement = new ImageElement(boundingRectangle.X, imagesYLocation, boundingRectangle.Width, boundingRectangle.Width, false, imagePath);
                addElementResult = pdfPage.AddElement(notKeepAspectImageElement);

                borderElement           = new RectangleElement(boundingRectangle);
                borderElement.ForeColor = Color.Black;
                pdfPage.AddElement(borderElement);

                pdfPage   = addElementResult.EndPdfPage;
                yLocation = addElementResult.EndPageBounds.Bottom + 20;

                // Add transparent images

                // Add section title
                titleTextElement           = new TextElement(xLocation, yLocation, "Transparent Images", titleFont);
                titleTextElement.ForeColor = Color.Black;
                addElementResult           = pdfPage.AddElement(titleTextElement);
                yLocation = addElementResult.EndPageBounds.Bottom + 10;
                pdfPage   = addElementResult.EndPdfPage;

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/transparent.png");
                ImageElement trasparentImageElement = new ImageElement(xLocation, yLocation, 150, imagePath);
                addElementResult = pdfPage.AddElement(trasparentImageElement);

                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/rose.png");
                trasparentImageElement = new ImageElement(addElementResult.EndPageBounds.Right + 60, yLocation + 20, 150, imagePath);
                pdfPage.AddElement(trasparentImageElement);

                pdfPage   = addElementResult.EndPdfPage;
                yLocation = addElementResult.EndPageBounds.Bottom + 20;

                // Rotate images

                // Add section title
                titleTextElement           = new TextElement(xLocation, yLocation, "Rotated Images", titleFont);
                titleTextElement.ForeColor = Color.Black;
                addElementResult           = pdfPage.AddElement(titleTextElement);
                yLocation = addElementResult.EndPageBounds.Bottom + 10;
                pdfPage   = addElementResult.EndPdfPage;

                // Add a not rotated image
                imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/compass.png");
                ImageElement noRotationImageElement = new ImageElement(xLocation, yLocation, 125, imagePath);
                addElementResult = pdfPage.AddElement(noRotationImageElement);

                float imageXLocation = addElementResult.EndPageBounds.X;
                float imageYLocation = addElementResult.EndPageBounds.Y;
                float imageWidth     = addElementResult.EndPageBounds.Width;
                float imageHeight    = addElementResult.EndPageBounds.Height;

                // The rotated coordinates system location
                float rotatedImageXLocation = imageXLocation + imageWidth + 20 + imageHeight;
                float rotatedImageYLocation = imageYLocation;

                // Add the image rotated 90 degrees
                ImageElement rotate90ImageElement = new ImageElement(0, 0, 125, imagePath);
                rotate90ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
                rotate90ImageElement.Rotate(90);
                pdfPage.AddElement(rotate90ImageElement);

                rotatedImageXLocation += 20 + imageWidth;
                rotatedImageYLocation  = imageYLocation + imageHeight;

                // Add the image rotated 180 degrees
                ImageElement rotate180ImageElement = new ImageElement(0, 0, 125, imagePath);
                rotate180ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
                rotate180ImageElement.Rotate(180);
                pdfPage.AddElement(rotate180ImageElement);

                rotatedImageXLocation += 20;
                rotatedImageYLocation  = imageYLocation + imageWidth;

                // Add the image rotated 270 degrees
                ImageElement rotate270ImageElement = new ImageElement(0, 0, 125, imagePath);
                rotate270ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
                rotate270ImageElement.Rotate(270);
                pdfPage.AddElement(rotate270ImageElement);

                pdfPage   = addElementResult.EndPdfPage;
                yLocation = addElementResult.EndPageBounds.Bottom + 20;

                // Save the PDF document in a memory buffer
                byte[] outPdfBuffer = pdfDocument.Save();

                // Send the PDF as response to browser

                // Set response content type
                Response.AddHeader("Content-Type", "application/pdf");

                // Instruct the browser to open the PDF file as an attachment or inline
                Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Image_Elements.pdf; size={0}", outPdfBuffer.Length.ToString()));

                // Write the PDF document buffer to HTTP response
                Response.BinaryWrite(outPdfBuffer);

                // End the HTTP response and stop the current page processing
                Response.End();
            }
            finally
            {
                // Close the PDF document
                pdfDocument.Close();
            }
        }
        protected void createPdfButton_Click(object sender, EventArgs e)
        {
            // Get the server IP and port
            String serverIP   = textBoxServerIP.Text;
            uint   serverPort = uint.Parse(textBoxServerPort.Text);

            // Create a PDF document
            Document pdfDocument = new Document(serverIP, serverPort);

            // Set optional service password
            if (textBoxServicePassword.Text.Length > 0)
            {
                pdfDocument.ServicePassword = textBoxServicePassword.Text;
            }

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            // The titles font used to mark various sections of the PDF document
            PdfFont titleFont = new PdfFont("Times New Roman", 12, true);

            titleFont.Bold = true;
            PdfFont subtitleFont = new PdfFont("Times New Roman", 8, true);

            subtitleFont.Bold = true;

            // Create a PDF page in PDF document
            PdfPage pdfPage = pdfDocument.AddPage();

            // Add section title
            TextElement titleTextElement = new TextElement(5, 5, "Images Scaling", titleFont);

            titleTextElement.ForeColor = RgbColor.Black;
            pdfPage.AddElement(titleTextElement);

            // Add an unscaled image

            // Add section title
            TextElement subtitleTextElement = new TextElement(0, 0, "Unscaled small image with normal resolution", subtitleFont);

            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            string       imagePath            = Server.MapPath("~/DemoAppFiles/Input/Images/picture_small.jpg");
            ImageElement unscaledImageElement = new ImageElement(0, 0, imagePath);

            pdfDocument.AddElement(unscaledImageElement, 10);

            // Add a large image scaled down to same size in PDF

            // Add section title
            subtitleTextElement           = new TextElement(0, 0, "Scaled down large image has higher resolution", subtitleFont);
            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_large.jpg");
            ImageElement scaledDownImageElement = new ImageElement(0, 0, 50, imagePath);

            pdfDocument.AddElement(scaledDownImageElement, 10);

            // Add a border around the scaled down image
            RectangleElement borderElement = new RectangleElement(0, 0, 0, 0);

            pdfDocument.AddElement(borderElement);

            // Add an unscaled small image

            // Add section title
            subtitleTextElement           = new TextElement(0, 0, "Unscaled small image", subtitleFont);
            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            imagePath            = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg");
            unscaledImageElement = new ImageElement(0, 0, imagePath);
            pdfDocument.AddElement(unscaledImageElement, 10);

            // Add an enlarged image

            // Add section title
            subtitleTextElement           = new TextElement(0, 0, "Enlarged small image has lower resolution", subtitleFont);
            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/picture_smaller.jpg");
            ImageElement enlargedImageElement = new ImageElement(0, 0, 200, imagePath);

            // Allow the image to be enlarged
            enlargedImageElement.EnlargeEnabled = true;
            pdfDocument.AddElement(enlargedImageElement, 10);

            // Scale an image preserving the aspect ratio

            // Add section title
            subtitleTextElement           = new TextElement(0, 0, "Scaled down image preserving aspect ratio", subtitleFont);
            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg");
            ImageElement keepAspectImageElement = new ImageElement(0, 0, 80, 80, true, imagePath);

            pdfDocument.AddElement(keepAspectImageElement, 10);

            borderElement           = new RectangleElement(0, 0, 0, 0);
            borderElement.ForeColor = RgbColor.Black;
            pdfDocument.AddElement(borderElement);

            // Scale an image without preserving aspect ratio
            // This can produce a distorted image

            // Add section title
            subtitleTextElement           = new TextElement(0, 0, "Scaled down image without preserving aspect ratio", subtitleFont);
            subtitleTextElement.ForeColor = RgbColor.Navy;
            pdfDocument.AddElement(subtitleTextElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/landscape.jpg");
            ImageElement notKeepAspectImageElement = new ImageElement(0, 0, 80, 80, false, imagePath);

            pdfDocument.AddElement(notKeepAspectImageElement, 10);

            borderElement           = new RectangleElement(0, 0, 0, 0);
            borderElement.ForeColor = RgbColor.Black;
            pdfDocument.AddElement(borderElement);

            // Add transparent images

            // Add section title
            titleTextElement           = new TextElement(0, 0, "Transparent Images", titleFont);
            titleTextElement.ForeColor = RgbColor.Black;
            pdfDocument.AddElement(titleTextElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/transparent.png");
            ImageElement trasparentImageElement = new ImageElement(0, 0, 150, imagePath);

            pdfDocument.AddElement(trasparentImageElement, 10);

            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/rose.png");
            trasparentImageElement = new ImageElement(0, 0, 150, imagePath);
            pdfDocument.AddElement(trasparentImageElement, 10);

            // Rotate images

            // Add a new page to document
            pdfPage = pdfDocument.AddPage();

            // Add section title
            titleTextElement           = new TextElement(0, 0, "Rotated Images", titleFont);
            titleTextElement.ForeColor = RgbColor.Black;
            pdfPage.AddElement(titleTextElement);

            // Add a not rotated image
            imagePath = Server.MapPath("~/DemoAppFiles/Input/Images/compass.png");
            ImageElement noRotationImageElement = new ImageElement(0, 25, 125, imagePath);

            pdfPage.AddElement(noRotationImageElement);

            // The rotated coordinates system location
            float rotatedImageXLocation = 125;
            float rotatedImageYLocation = 175;

            // Add the image rotated 90 degrees
            ImageElement rotate90ImageElement = new ImageElement(0, 0, 125, imagePath);

            rotate90ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
            rotate90ImageElement.Rotate(90);
            pdfPage.AddElement(rotate90ImageElement);

            rotatedImageXLocation = 125;
            rotatedImageYLocation = 450;

            // Add the image rotated 180 degrees
            ImageElement rotate180ImageElement = new ImageElement(0, 0, 125, imagePath);

            rotate180ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
            rotate180ImageElement.Rotate(180);
            pdfPage.AddElement(rotate180ImageElement);

            rotatedImageXLocation = 0;
            rotatedImageYLocation = 600;

            // Add the image rotated 270 degrees
            ImageElement rotate270ImageElement = new ImageElement(0, 0, 125, imagePath);

            rotate270ImageElement.Translate(rotatedImageXLocation, rotatedImageYLocation);
            rotate270ImageElement.Rotate(270);
            pdfPage.AddElement(rotate270ImageElement);

            // Save the PDF document in a memory buffer
            byte[] outPdfBuffer = pdfDocument.Save();

            // Send the PDF as response to browser

            // Set response content type
            Response.AddHeader("Content-Type", "application/pdf");

            // Instruct the browser to open the PDF file as an attachment or inline
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Image_Elements.pdf; size={0}", outPdfBuffer.Length.ToString()));

            // Write the PDF document buffer to HTTP response
            Response.BinaryWrite(outPdfBuffer);

            // End the HTTP response and stop the current page processing
            Response.End();
        }