Пример #1
0
        public virtual void RetrieveImageExtendedIncorrectBase64Test()
        {
            HtmlResourceResolver resourceResolver = CreateResolver();
            PdfXObject           image            = resourceResolver.RetrieveImageExtended(bLogoCorruptedData);

            NUnit.Framework.Assert.IsNull(image);
        }
Пример #2
0
        public void SetBarcode(Barcode barcode, int numberPage, float paddingY)
        {
            _xObject = barcode.GetBarcode.CreateFormXObject(null, _pdfDoc);
            PdfCanvas canvas = new PdfCanvas(_pdfDoc.GetPage(numberPage));

            canvas.AddXObject(_xObject, 2, 0, 0, 2, 40, paddingY);
        }
        public void RetrieveImageExtendedBase64Test()
        {
            HtmlResourceResolver resourceResolver = CreateResolver();
            PdfXObject           image            = resourceResolver.RetrieveImageExtended(bLogo);

            NUnit.Framework.Assert.NotNull(image);
        }
Пример #4
0
        public virtual void CalculateProportionallyFitRectangleWithHeightTest()
        {
            String         fileName    = "calculateProportionallyFitRectangleWithHeightTest.pdf";
            String         destPdf     = DESTINATION_FOLDER + fileName;
            String         cmpPdf      = SOURCE_FOLDER + "cmp_" + fileName;
            FileStream     fos         = new FileStream(destPdf, FileMode.Create);
            PdfWriter      writer      = new PdfWriter(fos);
            PdfDocument    document    = new PdfDocument(writer);
            PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(5, 5, 15, 20));

            formXObject.Put(PdfName.Matrix, new PdfArray(new float[] { 1, 0.57f, 0, 2, 20, 5 }));
            new PdfCanvas(formXObject, document).Circle(10, 15, 10).Fill();
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itext.png"));
            PdfPage         page         = document.AddNewPage();
            PdfCanvas       canvas       = new PdfCanvas(page);
            Rectangle       rect         = PdfXObject.CalculateProportionallyFitRectangleWithHeight(formXObject, 0, 0, 20);

            canvas.AddXObjectFittedIntoRectangle(formXObject, rect);
            rect = PdfXObject.CalculateProportionallyFitRectangleWithHeight(imageXObject, 20, 0, 20);
            canvas.AddXObjectFittedIntoRectangle(imageXObject, rect);
            canvas.Release();
            page.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
                                                                             ));
        }
        /// <summary>Applies an image list style to an element.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="element">the element</param>
        public static void ApplyListStyleImageProperty(IDictionary <String, String> cssProps, ProcessorContext context
                                                       , IPropertyContainer element)
        {
            String listStyleImage = cssProps.Get(CssConstants.LIST_STYLE_IMAGE);

            if (listStyleImage != null && !CssConstants.NONE.Equals(listStyleImage))
            {
                String     url          = CssUtils.ExtractUrl(listStyleImage);
                PdfXObject imageXObject = context.GetResourceResolver().RetrieveImageExtended(url);
                if (imageXObject != null)
                {
                    Image image = null;
                    if (imageXObject is PdfImageXObject)
                    {
                        image = new Image((PdfImageXObject)imageXObject);
                    }
                    else
                    {
                        if (imageXObject is PdfFormXObject)
                        {
                            image = new Image((PdfFormXObject)imageXObject);
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    element.SetProperty(Property.LIST_SYMBOL, image);
                    element.SetProperty(Property.LIST_SYMBOL_INDENT, 5);
                }
            }
        }
Пример #6
0
        public virtual void RetrieveImageExtendedNullTest()
        {
            ResourceResolver resourceResolver = new ResourceResolver(baseUri);
            PdfXObject       image            = resourceResolver.RetrieveImageExtended(null);

            NUnit.Framework.Assert.Null(image);
        }
Пример #7
0
        public virtual void RetrieveImageExtendedIncorrectBase64Test()
        {
            ResourceResolver resourceResolver = new ResourceResolver(baseUri);
            PdfXObject       image            = resourceResolver.RetrieveImageExtended(bLogoCorruptedData);

            NUnit.Framework.Assert.Null(image);
        }
Пример #8
0
        public void IncorrectBase64Test()
        {
            ResourceResolver resourceResolver = new ResourceResolver(baseUri);
            PdfXObject       pdfXObject       = resourceResolver.TryResolveBase64ImageSource(bLogoIncorrect);

            NUnit.Framework.Assert.Null(pdfXObject);
        }
Пример #9
0
        public virtual void RetrieveImageExtendedNullTest()
        {
            HtmlResourceResolver resourceResolver = CreateResolver();
            PdfXObject           image            = resourceResolver.RetrieveImageExtended(null);

            NUnit.Framework.Assert.IsNull(image);
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "AddXObjectsToLayers.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode = PdfPageMode.UseOC;

                PdfLayer firstWatermarkLayer  = pdf.CreateLayer("First XObject");
                PdfLayer secondWatermarkLayer = pdf.CreateLayer("Second XObject");

                PdfXObject firstObject = pdf.CreateXObject();
                firstObject.Canvas.DrawString("Text on the first XObject");
                firstObject.Layer = firstWatermarkLayer;

                PdfPage page = pdf.GetPage(0);
                page.Canvas.DrawXObject(firstObject, 0, 100);

                PdfXObject secondObject = pdf.CreateXObject();
                secondObject.Canvas.DrawString("Text on the second XObject");
                secondObject.Layer = secondWatermarkLayer;

                page.Canvas.DrawXObject(secondObject, 100, 200);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
Пример #11
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            // Suppose that this is our image data
            byte[] circleData = new byte[]
            { (byte)0x3c, (byte)0x7e, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7e, (byte)0x3c };

            PdfSpecialCs colorspace = GetSeparationColorspace(pdfDoc.GetWriter(),
                                                              new DeviceCmyk(0.8f, 0.3f, 0.3f, 0.1f));

            // Specifying a single component colorspace in the image
            ImageData image = ImageDataFactory.Create(8, 8, 1, 1, circleData,
                                                      new int[] { 0, 0 });
            PdfImageXObject imageXObject = new PdfImageXObject(image);

            imageXObject.Put(PdfName.ColorSpace, colorspace.GetPdfObject());
            imageXObject.MakeIndirect(pdfDoc);

            // Now we add the image to the existing PDF document
            PdfPage pdfPage = pdfDoc.GetFirstPage();

            pdfPage.SetIgnorePageRotationForContent(true);
            PdfCanvas canvas = new PdfCanvas(pdfPage);
            Rectangle rect   = PdfXObject.CalculateProportionallyFitRectangleWithWidth(imageXObject, 100, 200, 100);

            canvas.AddXObjectFittedIntoRectangle(imageXObject, rect);

            pdfDoc.Close();
        }
Пример #12
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "CreateXObjectFromPage.pdf";

            using (PdfDocument other = new PdfDocument(@"Sample Data\jfif3.pdf"))
            {
                using (PdfDocument pdf = new PdfDocument())
                {
                    PdfXObject firstXObject  = pdf.CreateXObject(other.Pages[0]);
                    PdfXObject secondXObject = pdf.CreateXObject(other.Pages[1]);

                    PdfPage page       = pdf.Pages[0];
                    double  halfOfPage = page.Width / 2;
                    pdf.Pages[0].Canvas.DrawXObject(firstXObject, 0, 0, halfOfPage, 400, 0);
                    pdf.Pages[0].Canvas.DrawXObject(secondXObject, halfOfPage, 0, halfOfPage, 400, 0);

                    pdf.Save(pathToFile);
                }
            }

            Process.Start(pathToFile);
        }
        public virtual void CreateXObjectWithNullLinearGradientTest()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfXObject  pdfXObject  = AbstractRenderer.CreateXObject(null, new Rectangle(0, 0, 20, 20), pdfDocument);

            NUnit.Framework.Assert.IsNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
        }
Пример #14
0
        public virtual void RetrieveImageExtendedBase64Test()
        {
            ResourceResolver resourceResolver = new ResourceResolver(baseUri);
            PdfXObject       image            = resourceResolver.RetrieveImageExtended(bLogo);

            Assert.NotNull(image);
        }
Пример #15
0
 /// <summary>
 /// Creates an
 /// <see cref="Image"/>
 /// from a form XObject, the representation of a
 /// form in PDF syntax.
 /// </summary>
 /// <param name="xObject">
 /// an internal
 /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
 /// </param>
 /// <param name="left">a float value representing the horizontal offset of the lower left corner of the form</param>
 /// <param name="bottom">a float value representing the vertical offset of the lower left corner of the form</param>
 public Image(PdfFormXObject xObject, float left, float bottom)
 {
     this.xObject = xObject;
     SetProperty(Property.LEFT, left);
     SetProperty(Property.BOTTOM, bottom);
     SetProperty(Property.POSITION, LayoutPosition.FIXED);
 }
Пример #16
0
 /// <summary>
 /// Creates an
 /// <see cref="Image"/>
 /// from a form XObject, the representation of a
 /// form in PDF syntax.
 /// </summary>
 /// <param name="xObject">
 /// an internal
 /// <see cref="iText.Kernel.Pdf.Xobject.PdfFormXObject"/>
 /// </param>
 /// <param name="x">a float value representing the horizontal offset of the lower left corner of the form</param>
 /// <param name="y">a float value representing the vertical offset of the lower left corner of the form</param>
 public Image(PdfFormXObject xObject, float x, float y)
 {
     this.xObject = xObject;
     SetProperty(Property.X, x);
     SetProperty(Property.Y, y);
     SetProperty(Property.POSITION, LayoutPosition.FIXED);
 }
Пример #17
0
 /// <summary>
 /// Method that will set properties to be inherited by this branch renderer's
 /// children and will iterate over all children in order to draw them.
 /// </summary>
 /// <param name="context">
 /// the object that knows the place to draw this element and
 /// maintains its state
 /// </param>
 protected internal override void DoDraw(SvgDrawContext context)
 {
     if (GetChildren().Count > 0)
     {
         // if branch has no children, don't do anything
         PdfStream stream = new PdfStream();
         stream.Put(PdfName.Type, PdfName.XObject);
         stream.Put(PdfName.Subtype, PdfName.Form);
         PdfFormXObject xObject   = (PdfFormXObject)PdfXObject.MakeXObject(stream);
         PdfCanvas      newCanvas = new PdfCanvas(xObject, context.GetCurrentCanvas().GetDocument());
         ApplyViewBox(context);
         //Bounding box needs to be written after viewbox calculations to account for pdf syntax interaction
         stream.Put(PdfName.BBox, new PdfArray(context.GetCurrentViewPort()));
         context.PushCanvas(newCanvas);
         ApplyViewportClip(context);
         ApplyViewportTranslationCorrection(context);
         foreach (ISvgNodeRenderer child in GetChildren())
         {
             newCanvas.SaveState();
             child.Draw(context);
             newCanvas.RestoreState();
         }
         CleanUp(context);
         context.GetCurrentCanvas().AddXObject(xObject, 0, 0);
     }
 }
 private static bool ApplyBackgroundImage(PdfXObject image, IList <BackgroundImage> backgroundImagesList, BackgroundRepeat
                                          repeat, BlendMode backgroundBlendMode, BackgroundPosition position, BackgroundBox clip, BackgroundBox
                                          origin)
 {
     if (image == null)
     {
         return(false);
     }
     if (image is PdfImageXObject)
     {
         backgroundImagesList.Add(new BackgroundApplierUtil.HtmlBackgroundImage((PdfImageXObject)image, repeat, position
                                                                                , backgroundBlendMode, clip, origin));
         return(true);
     }
     else
     {
         if (image is PdfFormXObject)
         {
             backgroundImagesList.Add(new BackgroundApplierUtil.HtmlBackgroundImage((PdfFormXObject)image, repeat, position
                                                                                    , backgroundBlendMode, clip, origin));
             return(true);
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
 }
Пример #19
0
        private void DogAndCat()
        {
            // create embedded media file
            PdfEmbeddedFile EmbeddedMediaFile = new PdfEmbeddedFile(Document, "BugsBunny.mp4");

            // Section 8.5 page 669 table 8.64
            PdfDisplayMedia DisplayMedia = new PdfDisplayMedia(EmbeddedMediaFile);

            // display media options
            DisplayMedia.DisplayControls(true);

            // repeat count (zero is play indefinitly)
            DisplayMedia.RepeatCount(1);

//		DisplayMedia.SetMediaWindow(MediaWindow.Floating, 480, 270, WindowPosition.UpperLeft, WindowTitleBar.TitleBarWithCloseButton, WindowResize.KeepAspectRatio, "User video title");

            PdfRectangle AnnotRect = ImageSizePos.ImageArea(160, 120, 1.5, 2.0, 5.5, 7.0, ContentAlignment.TopLeft);

            // create PdfObject for annotation
            PdfAnnotation Annotation = new PdfAnnotation(Page, AnnotRect, DisplayMedia);

            PdfXObject NormalAppearance = new PdfXObject(Document, AnnotRect.Width, AnnotRect.Height);

            NormalAppearance.SaveGraphicsState();
            NormalAppearance.SetColorNonStroking(Color.CornflowerBlue);
            NormalAppearance.SetColorStroking(Color.DarkGoldenrod);
            NormalAppearance.DrawOval(0.0, 0.0, AnnotRect.Width, AnnotRect.Height, PaintOp.CloseFillStroke);
            NormalAppearance.RestoreGraphicsState();
            Annotation.Appearance(NormalAppearance);

            Annotation.ActivateActionWhenPageIsVisible(false);

            return;
        }
Пример #20
0
        public virtual void RetrieveImageExtendedByMalformedResourceNameTest()
        {
            String           fileName         = "retrieveStyl eSheetTest.css";
            ResourceResolver resourceResolver = new ResourceResolver(baseUri);
            PdfXObject       pdfXObject       = resourceResolver.RetrieveImageExtended(fileName);

            Assert.Null(pdfXObject);
        }
Пример #21
0
 public override PdfCanvas AddXObjectFittedIntoRectangle(PdfXObject xObject, Rectangle rect)
 {
     NUnit.Framework.Assert.AreEqual(100f, rect.GetX(), 0);
     NUnit.Framework.Assert.AreEqual(200f, rect.GetY(), 0);
     NUnit.Framework.Assert.AreEqual(300f, rect.GetWidth(), 0);
     NUnit.Framework.Assert.AreEqual(400f, rect.GetHeight(), 0);
     return(this);
 }
Пример #22
0
            /// <summary>
            /// Instantiates a new Background instance.
            /// </summary>
            /// <param name="pdf">the PdfDocument instance of the PDF to which the background will be added</param>
            /// <param name="src">the path to the single-page PDF file</param>
            public Background(PdfDocument pdf, String src)
            {
                PdfDocument template = new PdfDocument(new PdfReader(src));
                PdfPage     page     = template.GetPage(1);

                stationery = page.CopyAsFormXObject(pdf);
                template.Close();
            }
Пример #23
0
 /// <summary>Sets linearGradientBuilder.</summary>
 /// <remarks>
 /// Sets linearGradientBuilder.
 /// <para />
 /// Makes image null as far as we can't have them both. It also makes background-repeat: no-repeat.
 /// </remarks>
 /// <param name="linearGradientBuilder">
 ///
 /// <see cref="iText.Kernel.Colors.Gradients.AbstractLinearGradientBuilder"/>
 /// to be set.
 /// </param>
 /// <returns>
 /// this
 /// <see cref="Builder"/>.
 /// </returns>
 public virtual BackgroundImage.Builder SetLinearGradientBuilder(AbstractLinearGradientBuilder linearGradientBuilder
                                                                 )
 {
     this.linearGradientBuilder = linearGradientBuilder;
     this.repeat = new BackgroundRepeat(BackgroundRepeat.BackgroundRepeatValue.NO_REPEAT);
     this.image  = null;
     return(this);
 }
Пример #24
0
 /// <summary>
 /// Creates an
 /// <see cref="Image"/>
 /// from an image XObject, the representation of an
 /// image in PDF syntax, with a custom width and on a fixed position.
 /// </summary>
 /// <param name="xObject">
 /// an internal
 /// <see cref="iText.Kernel.Pdf.Xobject.PdfImageXObject"/>
 /// </param>
 /// <param name="x">a float value representing the horizontal offset of the lower left corner of the image</param>
 /// <param name="y">a float value representing the vertical offset of the lower left corner of the image</param>
 /// <param name="width">a float value</param>
 public Image(PdfImageXObject xObject, float x, float y, float width)
 {
     this.xObject = xObject;
     SetProperty(Property.X, x);
     SetProperty(Property.Y, y);
     SetWidth(width);
     SetProperty(Property.POSITION, LayoutPosition.FIXED);
 }
Пример #25
0
        /// <summary>
        ///     Renders an image, scaling it to the given width and height.
        ///     If the scaled width and height is the same intrinsic size
        ///     of the image, the image is not scaled
        /// </summary>
        /// <param name="x">The x position of left edge in millipoints.</param>
        /// <param name="y">The y position of top edge in millipoints.</param>
        /// <param name="w">The width in millipoints.</param>
        /// <param name="h">The height in millipoints.</param>
        /// <param name="image">The image to be rendered.</param>
        private void DrawImageScaled(
            int x, int y, int w, int h, FonetImage image)
        {
            PdfXObject xobj = this.pdfCreator.AddImage(image);

            CloseText();
            currentStream.FillImage(x, y, w, h, xobj);
        }
Пример #26
0
        public PdfBasicSchema(PdfDocument pdf, string source, string resRoute)
        {
            resourceRoute = resRoute;
            PdfDocument template = new PdfDocument(new PdfReader(source));
            PdfPage     page     = template.GetPage(1);

            stationery = page.CopyAsFormXObject(pdf);
            template.Close();
        }
Пример #27
0
 public virtual void CalculateProportionallyFitRectangleWithHeightForCustomXObjectTest()
 {
     NUnit.Framework.Assert.That(() => {
         PdfXObject pdfXObject = new PdfXObjectTest.CustomPdfXObject(new PdfStream());
         PdfXObject.CalculateProportionallyFitRectangleWithHeight(pdfXObject, 0, 0, 20);
     }
                                 , NUnit.Framework.Throws.InstanceOf <ArgumentException>().With.Message.EqualTo("PdfFormXObject or PdfImageXObject expected."))
     ;
 }
        public virtual void CreateXObjectWithInvalidColorTest()
        {
            AbstractLinearGradientBuilder gradientBuilder = new StrategyBasedLinearGradientBuilder();
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfXObject  pdfXObject  = AbstractRenderer.CreateXObject(gradientBuilder, new Rectangle(0, 0, 20, 20), pdfDocument
                                                                     );

            NUnit.Framework.Assert.IsNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
        }
Пример #29
0
 /// <summary>Adds an image to the cache.</summary>
 /// <param name="src">the source path</param>
 /// <param name="imageXObject">the image XObject to be cached</param>
 internal virtual void PutImage(String src, PdfXObject imageXObject)
 {
     if (cache.ContainsKey(src))
     {
         return;
     }
     EnsureCapacity();
     cache.Put(src, imageXObject);
 }
        public virtual void CreateXObjectTest()
        {
            AbstractLinearGradientBuilder gradientBuilder = new StrategyBasedLinearGradientBuilder().SetGradientDirectionAsStrategy
                                                                (StrategyBasedLinearGradientBuilder.GradientStrategy.TO_BOTTOM_LEFT).AddColorStop(new GradientColorStop
                                                                                                                                                      (ColorConstants.RED.GetColorValue(), 0d, GradientColorStop.OffsetType.RELATIVE)).AddColorStop(new GradientColorStop
                                                                                                                                                                                                                                                        (ColorConstants.GREEN.GetColorValue(), 0.5, GradientColorStop.OffsetType.RELATIVE));
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfXObject  pdfXObject  = AbstractRenderer.CreateXObject(gradientBuilder, new Rectangle(0, 0, 20, 20), pdfDocument
                                                                     );

            NUnit.Framework.Assert.IsNotNull(pdfXObject.GetPdfObject().Get(PdfName.Resources));
        }
Пример #31
0
 public void AddXObject(PdfXObject xObject)
 {
     xObjects.Add(xObject.Name, xObject.GetReference());
 }