public static Image DrawImage(Rectangle rectangle, PdfCanvas canvas, string imagePath, System.Drawing.RotateFlipType rotateFlipType, bool scaleAbsolute, bool centerVertically, bool centerHorizontally)
 {
     using (var originalImage = System.Drawing.Image.FromFile(imagePath))
     {
         originalImage.RotateFlip(rotateFlipType);
         using (var memoryStream = new MemoryStream())
         {
             originalImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
             var imageData = ImageDataFactory.Create(memoryStream.ToArray());
             var image     = new Image(imageData);
             if (scaleAbsolute)
             {
                 image.ScaleAbsolute(rectangle.GetWidth(), rectangle.GetHeight());
             }
             else
             {
                 image.ScaleToFit(rectangle.GetWidth(), rectangle.GetHeight());
             }
             var xOffset        = centerHorizontally ? (rectangle.GetWidth() / 2 - image.GetImageScaledWidth() / 2) : 0;
             var yOffset        = centerVertically ? (rectangle.GetHeight() / 2 - image.GetImageScaledHeight() / 2) : 0;
             var imageRectangle = new Rectangle(
                 rectangle.GetX() + xOffset,
                 rectangle.GetY() + yOffset,
                 image.GetImageScaledWidth(),
                 image.GetImageScaledHeight());
             canvas.AddImageFittedIntoRectangle(imageData, imageRectangle, false);
             return(image);
         }
     }
 }
예제 #2
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            Rectangle rect             = new Rectangle(36, 700, 100, 100);
            String    embeddedFileName = "test.docx";

            // the 3rd argument is the file description.
            // the 5th argument is the mime-type of the embedded file;
            // the 6th argument is the AFRelationship key value.
            PdfFileSpec   fileSpec   = PdfFileSpec.CreateEmbeddedFileSpec(pdfDoc, PATH, null, embeddedFileName, null, null);
            PdfAnnotation attachment = new PdfFileAttachmentAnnotation(rect, fileSpec);

            // This method sets the text that will be displayed for the annotation or the alternate description,
            // if this type of annotation does not display text.
            attachment.SetContents("Click me");

            // Create XObject and draw it with the imported image on the canvas
            // to add XObject as normal appearance.
            PdfFormXObject xObject   = new PdfFormXObject(rect);
            ImageData      imageData = ImageDataFactory.Create(IMG);
            PdfCanvas      canvas    = new PdfCanvas(xObject, pdfDoc);

            canvas.AddImageFittedIntoRectangle(imageData, rect, true);
            attachment.SetNormalAppearance(xObject.GetPdfObject());

            pdfDoc.AddNewPage().AddAnnotation(attachment);

            pdfDoc.Close();
        }
예제 #3
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(dest));
            PageSize    pageSize = PageSize.A4.Rotate();
            Document    doc      = new Document(pdfDoc, pageSize);

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());

            canvas.AddImageFittedIntoRectangle(ImageDataFactory.Create(IMAGE), pageSize, false);

            doc.Add(new Paragraph("Berlin!"));

            doc.Close();
        }
예제 #4
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(dest));
            PageSize    pageSize = PageSize.A4;
            Document    doc      = new Document(pdfDoc, pageSize);

            PdfCanvas canvas    = new PdfCanvas(pdfDoc.AddNewPage());
            var       re        = new Rectangle(pageSize.GetLeft() + 50, pageSize.GetTop() - 125, 200, 125);
            SolidLine solidLine = new SolidLine();

            solidLine.Draw(canvas, re);
            canvas.Rectangle(re);
            canvas.AddImageFittedIntoRectangle(ImageDataFactory.Create(IMAGE), re, true);

            doc.Close();
        }
예제 #5
0
            public override void Draw(DrawContext drawContext)
            {
                PdfCanvas canvas   = drawContext.GetCanvas();
                Rectangle position = GetOccupiedAreaBBox();

                Image image = new Image(img);

                image.ScaleToFit(10000000, position.GetHeight());

                float x = position.GetLeft();
                float y = position.GetBottom();

                while (x + image.GetImageScaledWidth() < position.GetRight())
                {
                    image.SetFixedPosition(x, y);
                    canvas.AddImageFittedIntoRectangle(img, new Rectangle(x, y, image.GetImageScaledWidth(), image.GetImageScaledHeight()), false);
                    x += image.GetImageScaledWidth();
                }
            }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc   = new PdfDocument(new PdfWriter(dest));
            PageSize    pageSize = PageSize.A4.Rotate();
            Document    doc      = new Document(pdfDoc, pageSize);

            ImageData image  = ImageDataFactory.Create(IMAGE);
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());

            canvas.SaveState();
            PdfExtGState state = new PdfExtGState().SetFillOpacity(0.6f);

            canvas.SetExtGState(state);
            Rectangle rect = new Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight());

            canvas.AddImageFittedIntoRectangle(image, rect, false);
            canvas.RestoreState();

            doc.Add(new Paragraph("Berlin!"));

            doc.Close();
        }
예제 #7
0
        public void HandleEvent(Event @event)
        {
            PdfDocumentEvent pdfDocumentEvent = (PdfDocumentEvent)@event;
            PdfDocument      pdfDocument      = pdfDocumentEvent.GetDocument();
            PdfPage          pdfPage          = pdfDocumentEvent.GetPage();

            PdfCanvas pdfCanvas = new PdfCanvas(
                pdfPage.NewContentStreamBefore(),
                pdfPage.GetResources(),
                pdfDocument);

            Rectangle rectangleArea = new Rectangle(
                0, pdfPage.GetPageSize().GetTop() - 120,
                pdfPage.GetPageSize().GetRight(), 120);

            pdfCanvas.AddImageFittedIntoRectangle(
                ImageDataFactory.Create(GlobalUrl.BACKGROUND),
                rectangleArea, true);

            new Canvas(pdfCanvas, rectangleArea)
            .Add(CreateHeader(pdfDocumentEvent))
            .Close();
        }
예제 #8
0
        public static void GenerateConfirmationOfServicesForm(
            Stream destinationStream,
            byte[] signBytes,
            string contractorRus,
            string contractorEnd,
            double amount,
            DateTime?date = null)
        {
            date ??= DateTime.UtcNow;

            using var sourceStream = H.Resources.Confirmation_of_Services_Form;
            using var document     = new PdfDocument(
                      new PdfReader(sourceStream),
                      new PdfWriter(destinationStream));
            var fontBytes = H.Resources.Times_New_Roman_Cyrillic;
            var font      = PdfFontFactory.CreateFont(fontBytes, "Cp1251", PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED);

            {
                var canvas = new PdfCanvas(document.GetFirstPage());
                canvas
                .BeginText()
                .SetFontAndSize(font, 11)
                .MoveText(100, 659)
                .ShowText($"{date.Value.ToString("dd MMMM yyyy", CultureInfo.GetCultureInfo("ru-RU"))}")
                .MoveText(-30, -100)
                .ShowText(contractorRus)
                .MoveText(316, -222)
                .ShowText($"{amount:N}")
                .MoveText(-317, -140)
                .ShowText(contractorRus)
                .EndText();

                canvas
                .AddImageFittedIntoRectangle(
                    ImageDataFactory.CreatePng(signBytes),
                    new Rectangle(65, 210, 50, 50),
                    true);
            }

            {
                var canvas = new PdfCanvas(document.GetLastPage());
                canvas
                .BeginText()
                .SetFontAndSize(font, 11)
                .MoveText(100, 672)
                .ShowText(date.Value.ToString("MMMM dd, yyyy", CultureInfo.InvariantCulture))
                .MoveText(-30, -90)
                .ShowText(contractorEnd)
                .MoveText(270, -177)
                .ShowText(amount.ToString("N", CultureInfo.InvariantCulture))
                .MoveText(-270, -185)
                .ShowText(contractorEnd)
                .EndText();

                canvas
                .AddImageFittedIntoRectangle(
                    ImageDataFactory.CreatePng(signBytes),
                    new Rectangle(65, 245, 50, 50),
                    true);
            }
        }