예제 #1
0
        public override PDFGraphics CreateGraphics(PDFWriter writer, StyleStack styles, PDFContextBase context)
        {
            Style    full = styles.GetFullStyle(this);
            PageSize size = this.GetPageSize(full);

            return(PDFGraphics.Create(writer, false, this, DrawingOrigin.TopLeft, size.Size, context));
        }
예제 #2
0
        protected virtual PDFGraphics CreateGraphics(PDFWriter writer, Styles.StyleStack styles, PDFRenderContext context)
        {
            var sz = new PDFSize(this._childContainer.Width, this._childContainer.Height);

            if (this._position.ViewPort.HasValue)
            {
                sz = this._position.ViewPort.Value.Size;
            }
            return(PDFGraphics.Create(writer, false, this, DrawingOrigin.TopLeft, sz, context));
        }
예제 #3
0
        public virtual PDFObjectRef OutputToPDF(PDFRenderContext context, PDFWriter writer)
        {
            PDFGraphics graphics = context.Graphics;
            Style       full     = null;

            PDFComponentArrangement arrange = this.GetFirstArrangement();

            if (null != arrange)
            {
                full = arrange.FullStyle;
            }

            if (null == full)
            {
                full = context.FullStyle;
            }

            PDFImageXObject img = this.GetImageObject(context, full);

            if (img != null)
            {
                PDFPoint pos = context.Offset;


                PDFSize imgsize = context.Space;

                //the pictures are drawn from their bottom left corner, so take off the height.
                //if (context.DrawingOrigin == DrawingOrigin.TopLeft)
                //    pos.Y = pos.Y + imgsize.Height;

                graphics.SaveGraphicsState();

                StyleValue <double> op;
                if (full.TryGetValue(StyleKeys.FillOpacityKey, out op))
                {
                    if (op.Value(full) < 1.0)
                    {
                        graphics.SetFillOpacity(op.Value(full));
                    }
                }
                PDFObjectRef imgref = img.EnsureRendered(context, writer);
                graphics.PaintImageRef(img, imgsize, pos);

                graphics.RestoreGraphicsState();
                return(imgref);
            }
            else
            {
                return(null);
            }
        }
예제 #4
0
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                return(ReturnTypes.EksikParametre);
            }

            string path = args[0];

            string[] textArray = textArrayAl(path);

            PdfOzellikleri pdfOzellikleri = new PdfOzellikleri(textArray.Take(100));

            Document doc = new Document();

            doc.Compress = false;
            TextArea textArea = textAreaAyarla(textArray, pdfOzellikleri);

            while (textArea != null)
            {
                Page page = pdfOzellikleri.YeniPageAl();

                doc.Pages.Add(page);
                PDFGraphics graphics = page.Graphics;

                //DrawText richtext at x = 50; y =75 and height = 650
                //OverflowText is return as RichText
                //Loop until there is no overflow.
                textArea = graphics.DrawTextArea(0, 0, textArea);
            }

            FileStream fs = kaydedilecekDosyaDon(path);

            try
            {
                doc.Generate(fs);
                return(ReturnTypes.Basarili);
            }
            catch (Exception)
            {
                return(ReturnTypes.BilinmeyenHata);
            }
            finally
            {
                fs.Flush();
                fs.Close();
            }
        }
예제 #5
0
        /// <summary>
        /// Outputs the background for this block in the specified rect with the background style.
        /// </summary>
        /// <param name="bg">The backgrond style</param>
        /// <param name="border">The border style (which indicates the corner radius)</param>
        /// <param name="context">The current render context</param>
        /// <param name="rect">The rectangle to be output</param>
        protected virtual void OutputBackground(PDFBrush bg, PDFUnit?corner, PDFRenderContext context, PDFRect rect)
        {
            PDFGraphics g = context.Graphics;

            if (null != bg)
            {
                if (corner.HasValue && corner.Value != PDFUnit.Zero)
                {
                    g.FillRoundRectangle(bg, rect, corner.Value);
                }
                else
                {
                    g.FillRectangle(bg, rect);
                }
            }
        }
예제 #6
0
        public PDFObjectRef OutputToPDF(PDFRenderContext context, PDFWriter writer)
        {
            Style fullstyle = context.FullStyle;

            if (null == fullstyle)
            {
                throw new ArgumentNullException("context.FullStyle");
            }

            PDFGraphics graphics = context.Graphics;

            if (null == graphics)
            {
                throw new ArgumentNullException("context.Graphics");
            }

            if (null != this.Path)
            {
                PDFBrush brush = fullstyle.CreateFillBrush();
                PDFPen   pen   = fullstyle.CreateStrokePen();

                if (null != pen && null != brush)
                {
                    graphics.FillAndStrokePath(brush, pen, context.Offset, this.Path);
                }

                else if (null != brush)
                {
                    graphics.FillPath(brush, context.Offset, this.Path);
                }

                else if (null != pen)
                {
                    graphics.DrawPath(pen, context.Offset, this.Path);
                }
            }
            return(null);
        }
예제 #7
0
        /// <summary>
        /// Outputs the border for this block in the specified rect with the border style
        /// </summary>
        /// <param name="context">The current render context</param>
        /// <param name="cliprect">The rectangle that shoud be rendered as the border</param>
        protected virtual void OutputClipping(PDFRenderContext context, PDFRect cliprect, PDFUnit corner, Sides sides, PDFThickness inset)
        {
            PDFGraphics g = context.Graphics;

            g.SaveGraphicsState();

            if (inset.IsEmpty == false)
            {
                cliprect.X      += inset.Left;
                cliprect.Y      += inset.Top;
                cliprect.Width  -= (inset.Left + inset.Right);
                cliprect.Height -= (inset.Top + inset.Bottom);
            }

            if (corner != PDFUnit.Zero)
            {
                g.SetClipRect(cliprect, sides, corner);
            }
            else
            {
                g.SetClipRect(cliprect);
            }
        }
예제 #8
0
        /// <summary>
        /// This will render the actual content of the XObject graphical content in a new object reference.
        /// This can then be referred to.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        private PDFObjectRef OutputContent(PDFRenderContext context, PDFWriter writer)
        {
            PDFObjectRef xObject = writer.BeginObject();

            IStreamFilter[] filters = (context.Compression == OutputCompressionType.FlateDecode) ? this._page.PageCompressionFilters : null;

            writer.BeginStream(xObject, filters);

            this.Location = context.Offset.Offset(0, this.Line.OffsetY);
            PDFSize     origSpace    = context.Space.Clone();
            PDFGraphics prevGraphics = context.Graphics;

            using (PDFGraphics g = this.CreateGraphics(writer, context.StyleStack, context))
            {
                context.Graphics = g;
                g.SaveGraphicsState();
                g.RestoreGraphicsState();

                context.Offset = Drawing.PDFPoint.Empty;

                this._childContainer.OutputToPDF(context, writer);
            }
            context.Offset   = this.Location;
            context.Space    = origSpace;
            context.Graphics = prevGraphics;

            long len = writer.EndStream();

            writer.BeginDictionary();

            this.WriteXObjectDictionaryContent(context, writer, len, filters);

            writer.EndDictionary();
            writer.EndObject();

            return(xObject);
        }
예제 #9
0
        static void Main(string[] args)
        {
            PDFSize paper = new PDFSize(842, 595); //A4 paper size 210mm×297mm
            // Create a document object and initialize it
            PDFDocument doc = new PDFDocument();

            doc.VersionMinor = 4;

            // Create a catalog
            Catalog cat = new Catalog();
            // Create an indirect obejct pointing to the catalog
            // Look how we get the id number from the document
            IndirectObject ioCat = new IndirectObject(cat, doc.NextObjectID());

            doc.IndirectObjects.Add(ioCat);
            doc.Trailer.Root = new Reference(ioCat);
            // Create pages object
            Pages          pages   = new Pages();
            IndirectObject ioPages = new IndirectObject(pages, doc.NextObjectID());

            doc.IndirectObjects.Add(ioPages);
            Reference refPages = new Reference(ioPages);

            // Create a reference to the Pages object and add it to teh catalog object
            cat.Pages = refPages;
            Page page = new Page();

            // define the dimention of the pages
            page.MediaBox = new PDFArray()
            {
                new Number(0), new Number(0), new Number(paper.Width), new Number(paper.Height)
            };
            // Set the page objects parent
            page.Parent = refPages;
            // Create indirect object for pages
            IndirectObject ioPage = new IndirectObject(page, doc.NextObjectID());

            doc.IndirectObjects.Add(ioPage);
            pages.AddPageRef(new Reference(ioPage));
            // Create resources object for each pages
            page.Resources = new Resources();


            PDFFontDescriptor fontDescriptor = new PDFFontDescriptor();

            fontDescriptor.FontBBox = new PDFArray()
            {
                new Number(0), new Number(0), new Number(0), new Number(0)
            };
            IndirectObject _fontDescriptorID = new IndirectObject(fontDescriptor, doc.NextObjectID());

            doc.IndirectObjects.Add(_fontDescriptorID);
            Reference fontDescriptorRef = new Reference(_fontDescriptorID);

            PDFFont        descendantFont   = new PDFFont();
            IndirectObject descendantFontID = new IndirectObject(descendantFont, doc.NextObjectID());

            doc.IndirectObjects.Add(descendantFontID);
            descendantFont.SubType  = new Name("CIDFontType2");
            descendantFont.BaseFont = new Name("SimSun");

            descendantFont.FontDescriptor = fontDescriptorRef;
            descendantFont.CIDSystemInfo  = new PDFDictionary();
            descendantFont.CIDSystemInfo.Add(new Name("Registry"), "(Adobe)");
            descendantFont.CIDSystemInfo.Add(new Name("Ordering"), "(GB1)");

            // Create font andf initialize it
            PDFFont ascii = new PDFFont();

            ascii.Name     = new Name("F2");
            ascii.Encoding = new Name("WinAnsiEncoding");
            ascii.BaseFont = new Name("Helvetica");
            ascii.SubType  = new Name("TrueType");
            IndirectObject arialID = new IndirectObject(ascii, doc.NextObjectID());

            doc.IndirectObjects.Add(arialID);


            // Create font and initialize it
            PDFFont gb = new PDFFont();

            gb.SubType         = new Name("Type0");
            gb.Name            = new Name("F1");
            gb.BaseFont        = new Name("SimSun");
            gb.Encoding        = new Name("UniGB-UCS2-H");
            gb.FontDescriptor  = fontDescriptorRef;
            gb.DescendantFonts = new PDFArray()
            {
                new Reference(descendantFontID)
            };
            IndirectObject fontID = new IndirectObject(gb, doc.NextObjectID());

            doc.IndirectObjects.Add(fontID);


            // add the font dictionary to the resources
            PDFDictionary dicFont = new PDFDictionary();

            dicFont.CarriageReturn = string.Empty;
            dicFont.Add(ascii.Name, new Reference(arialID));
            dicFont.Add(gb.Name, new Reference(fontID));
            page.Resources.Add(new Name("Font"), dicFont);
            // Create the procedure set
            ProcSet procset = new ProcSet();

            procset.Add(new Name("PDF"));
            procset.Add(new Name("Text"));
            IndirectObject ioProcset = new IndirectObject(procset, doc.NextObjectID());

            doc.IndirectObjects.Add(ioProcset);
            // add the proc set to the resources
            page.Resources.Add(new Name("ProcSet"), new Reference(ioProcset));

            PDFStream      content  = new PDFStream();
            IndirectObject ioStream = new IndirectObject(content, doc.NextObjectID());

            doc.IndirectObjects.Add(ioStream);
            page.Content = new Reference(ioStream);
            Info           info   = new Info();
            IndirectObject ioInfo = new IndirectObject(info, doc.NextObjectID());

            doc.IndirectObjects.Add(ioInfo);
            doc.Trailer.Info = new Reference(ioInfo);

            //draw
            double      unit = 2.834;
            PDFGraphics g    = new PDFGraphics();

            g.state(1, 0, 0, 1, 0, 0);
            g.SetLineWidth(1f);
            g.SetRGBFillColor(Color.SteelBlue);
            g.SetRGBStrokeColor(Color.Black);
            g.DrawRectangle(20 * unit, paper.Height - 20 * unit, 10 * unit, 10 * unit);
            g.FillAndStrokePath();
            content.Write(g);

            //Set font and write
            // create the text object to write to
            TextObject text = new TextObject();

            text.SetFont(gb, 8);
            text.SetRGBFillColor(Color.Blue);
            text.SetRGBStrokeColor(Color.Blue);
            text.Move(10 * unit, paper.Height - 5 * unit);
            text.SetRGBFillColor(Color.Black);
            text.SetRGBStrokeColor(Color.Black);
            text.SetFont(gb, 9);
            Func <string, string> str2code = (string s) =>
            {
                StringBuilder o = new StringBuilder("<");
                foreach (var c in s)
                {
                    o.Append(Convert.ToString(c, 16).ToUpper().PadLeft(4, '0'));
                }
                o.Append('>');
                return(o.ToString());
            };

            text.WriteBinary(str2code("绘制一个大小边长1厘米的正方形"));
            content.Write(text);

            doc.Save("demo.pdf");
        }
        public void OutputOverlayColor(PDFBrush fill, PDFGraphics graphics, PDFPoint blocklocation)
        {
            PDFRect rect = new PDFRect(blocklocation.X + this.OffsetX, blocklocation.Y, this.TotalBounds.Width, this.TotalBounds.Height);

            graphics.FillRectangle(fill, rect);
        }
예제 #11
0
 protected virtual PDFGraphics CreateGraphics(PDFWriter writer, StyleStack styles, PDFRenderContext context)
 {
     return(PDFGraphics.Create(writer, false, this, DrawingOrigin.TopLeft, this.Size, context));
 }
예제 #12
0
        protected virtual PDFObjectRef OutputContent(PDFRenderContext context, PDFWriter writer)
        {
            PDFObjectRef oref = writer.BeginObject();


            IStreamFilter[] filters = (context.Compression == OutputCompressionType.FlateDecode) ? this.PageCompressionFilters : null;
            writer.BeginStream(oref, filters);


            PDFPoint pt = context.Offset.Clone();
            PDFSize  sz = context.Space.Clone();

            using (PDFGraphics g = this.CreateGraphics(writer, context.StyleStack, context))
            {
                context.Graphics = g;

                if (null != this.HeaderBlock)
                {
                    this.HeaderBlock.OutputToPDF(context, writer);
                }

                this.ContentBlock.OutputToPDF(context, writer);

                if (null != this.FooterBlock)
                {
                    this.FooterBlock.OutputToPDF(context, writer);
                }

                if (_outputbadge)
                {
                    this.PaintBadgeXObj(context, writer);
                }
            }
            context.Offset = pt;
            context.Space  = sz;

            long len = writer.EndStream();

            writer.BeginDictionary();


            if (null != filters && filters.Length > 0)
            {
                writer.BeginDictionaryEntry("Length");
                writer.WriteNumberS(len);
                writer.EndDictionaryEntry();
                writer.BeginDictionaryEntry("Filter");
                writer.BeginArray();

                foreach (IStreamFilter filter in filters)
                {
                    writer.BeginArrayEntry();
                    writer.WriteName(filter.FilterName);
                    writer.EndArrayEntry();
                }
                writer.EndArray();
                writer.EndDictionaryEntry();
            }
            else
            {
                writer.BeginDictionaryEntry("Length");
                writer.WriteNumberS(len);
                writer.EndDictionaryEntry();
            }

            writer.EndDictionary();
            writer.EndObject();
            return(oref);
        }
예제 #13
0
        protected virtual void ReleaseClipping(PDFRenderContext context)
        {
            PDFGraphics g = context.Graphics;

            g.RestoreGraphicsState();
        }
예제 #14
0
        /// <summary>
        /// Outputs the border for this block in the specified rect with the border style
        /// </summary>
        /// <param name="bg">The background style</param>
        /// <param name="border">The border style</param>
        /// <param name="context">The current render context</param>
        /// <param name="rect">The rectangle that shoud be rendered as the border</param>
        protected virtual void OutputBorder(PDFBrush bg, PDFPenBorders border, PDFRenderContext context, PDFRect rect)
        {
            PDFGraphics g = context.Graphics;

            if (null != border.AllPen && border.AllSides > 0)
            {
                if (border.CornerRadius.HasValue && border.CornerRadius.Value != PDFUnit.Zero)
                {
                    g.DrawRoundRectangle(border.AllPen, rect, border.AllSides, border.CornerRadius.Value);
                }
                else
                {
                    g.DrawRectangle(border.AllPen, rect, border.AllSides);
                }
            }

            if (null != border.TopPen)
            {
                if (border.CornerRadius.HasValue && border.CornerRadius.Value != PDFUnit.Zero)
                {
                    g.DrawRoundRectangle(border.TopPen, rect, Sides.Top, border.CornerRadius.Value);
                }
                else
                {
                    g.DrawRectangle(border.TopPen, rect, Sides.Top);
                }
            }

            if (null != border.RightPen)
            {
                if (border.CornerRadius.HasValue && border.CornerRadius.Value != PDFUnit.Zero)
                {
                    g.DrawRoundRectangle(border.RightPen, rect, Sides.Right, border.CornerRadius.Value);
                }
                else
                {
                    g.DrawRectangle(border.RightPen, rect, Sides.Right);
                }
            }

            if (null != border.BottomPen)
            {
                if (border.CornerRadius.HasValue && border.CornerRadius.Value != PDFUnit.Zero)
                {
                    g.DrawRoundRectangle(border.BottomPen, rect, Sides.Bottom, border.CornerRadius.Value);
                }
                else
                {
                    g.DrawRectangle(border.BottomPen, rect, Sides.Bottom);
                }
            }

            if (null != border.LeftPen)
            {
                if (border.CornerRadius.HasValue && border.CornerRadius.Value != PDFUnit.Zero)
                {
                    g.DrawRoundRectangle(border.LeftPen, rect, Sides.Left, border.CornerRadius.Value);
                }
                else
                {
                    g.DrawRectangle(border.LeftPen, rect, Sides.Left);
                }
            }
        }
        //
        // overrides
        //


        /// <summary>
        /// Main overridden method
        /// </summary>
        protected override void DoLayoutComponent()
        {
            IDisposable record = this.Context.PerformanceMonitor.Record(PerformanceMonitorType.Layout_Pages, "Page " + this.Component.ID);

            //Take a copy of the style stack for the header and footer
            this.PageStyleStack = this.Context.StyleStack.Clone();


            //Get the page size and position options
            PageSize pgsize = this.FullStyle.CreatePageSize();

            pgsize.Size = this.GetNextPageSize(this.Component, this.FullStyle, pgsize.Size);

            PDFPositionOptions options = this.FullStyle.CreatePostionOptions();



            //Graphics
            PDFGraphics g = this.Page.CreateGraphics(this.StyleStack, this.Context);

            this.Context.Graphics = g;


            //Size, border, margins
            PDFRect bounds      = new PDFRect(PDFPoint.Empty, pgsize.Size);
            PDFRect contentrect = GetContentRectFromBounds(bounds, options.Margins, options.Padding);


            //Columns
            PDFColumnOptions colOpts = this.FullStyle.CreateColumnOptions();

            //Overflow
            OverflowAction action = options.OverflowAction;



            PDFLayoutPage pg = BuildNewPage(pgsize.Size, options, colOpts, action);

            //Register page numbering
            PDFPageNumberOptions numbers = this.GetPageNumbering(this.FullStyle);

            this.RegisterPageNumbering(pg, numbers);

            this.LayoutPageContent();



            //close the last page
            PDFLayoutPage last = this.DocumentLayout.CurrentPage;

            if (last.IsClosed == false)
            {
                last.Close();
            }

            //Unregister the page numbers.
            this.UnRegisterPageNumbering(last, numbers);

            //release graphics
            this.Context.Graphics = null;

            g.Dispose();
            record.Dispose();
        }
        //
        // methods
        //

        #region protected override PDFObjectRef DoRenderToPDF(PDFContextBase context, PDFWriter writer)

        /// <summary>
        /// Renders the tiling image
        /// </summary>
        /// <param name="context"></param>
        /// <param name="writer"></param>
        /// <returns></returns>
        protected override PDFObjectRef DoRenderToPDF(PDFContextBase context, PDFWriter writer)
        {
            IStreamFilter[] filters = writer.DefaultStreamFilters;
            PDFObjectRef    pattern = writer.BeginObject();

            writer.BeginDictionary();
            writer.WriteDictionaryNameEntry("Type", "Pattern");
            writer.WriteDictionaryNumberEntry("PatternType", (int)this.PatternType);
            writer.WriteDictionaryNumberEntry("PaintType", (int)this.PaintType);
            writer.WriteDictionaryNumberEntry("TilingType", (int)this.TilingType);
            writer.BeginDictionaryEntry("BBox");

            PDFPoint offset = new PDFPoint(this.Start.X, this.Start.Y - this.ImageSize.Height);// this.Start;
            PDFSize  size   = this.ImageSize;

            PDFSize graphicsSize = new PDFSize(size.Width + offset.X, size.Height + offset.Y);

            writer.WriteArrayRealEntries(true, offset.X.PointsValue,
                                         offset.Y.PointsValue,
                                         offset.X.PointsValue + size.Width.PointsValue,
                                         offset.Y.PointsValue + size.Height.PointsValue);

            writer.EndDictionaryEntry();

            writer.WriteDictionaryRealEntry("XStep", this.Step.Width.PointsValue);
            writer.WriteDictionaryRealEntry("YStep", this.Step.Height.PointsValue);

            PDFObjectRef all = this.Resources.WriteResourceList(context, writer);

            writer.WriteDictionaryObjectRefEntry("Resources", all);

            writer.BeginStream(pattern);

            using (PDFGraphics g = PDFGraphics.Create(writer, false, this, DrawingOrigin.TopLeft,
                                                      graphicsSize, context))
            {
                offset = new PDFPoint(offset.X, 0.0);
                g.PaintImageRef(this.Image, size, offset);
            }
            long len = writer.EndStream();

            if (null != filters && filters.Length > 0)
            {
                writer.BeginDictionaryEntry("Length");
                writer.WriteNumberS(len);
                writer.EndDictionaryEntry();
                writer.BeginDictionaryEntry("Filter");
                writer.BeginArray();
                foreach (IStreamFilter filter in filters)
                {
                    writer.BeginArrayEntry();
                    writer.WriteName(filter.FilterName);
                    writer.EndArrayEntry();
                }
                writer.EndArray();
                writer.EndDictionaryEntry();
            }
            else
            {
                writer.BeginDictionaryEntry("Length");
                writer.WriteNumberS(len);
                writer.EndDictionaryEntry();
            }

            writer.EndDictionary();
            writer.EndObject();

            return(pattern);
        }