Exemplo n.º 1
0
        private void SamplePage2(PdfDocument document)
        {
            PdfPage   page = document.AddPage();
            XGraphics gfx  = XGraphics.FromPdfPage(page);

            gfx.MUH = PdfFontEncoding.Unicode;

            Document doc = new Document();

            MigraDoc.Rendering.DocumentRenderer docRender = new MigraDoc.Rendering.DocumentRenderer(doc);

            docRender.PrepareDocument();

            XRect A4Rect = new XRect(0, 0, XUnit.FromCentimeter(21).Point, XUnit.FromCentimeter(29.7).Point);

            int pageCount = docRender.FormattedDocument.PageCount;

            for (int idx = 0; idx < pageCount; idx++)
            {
                XRect rect = GetRect(idx);
                XGraphicsContainer container = gfx.BeginContainer(rect, A4Rect, XGraphicsUnit.Point);

                gfx.DrawRectangle(XPens.LightGray, A4Rect);

                docRender.RenderPage(gfx, idx + 1);

                gfx.EndContainer(container);
            }
        }
Exemplo n.º 2
0
        void IDrawingCanvas.DrawString(string value, FontInfo font, BrushEx brush, RectangleF rect, StringFormatEx format)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            XGraphicsContainer clipState = _graphics.BeginContainer();
            var xRect = PdfConverter.Convert(rect);
            var xFont = _fonts.GetPdfFont(font);

            _graphics.IntersectClip(xRect);
            var resultRect = _graphics.MeasureString(value, xFont, GetFormat(format));

            if ((format.WrapMode == WrapMode.NoWrap && !value.Contains('\n')) ||
                (resultRect.Width * PdfConverter.TwipsPerPoint <= rect.Width))
            {
                _graphics.DrawString(value, xFont, (BrushBase)brush, xRect, GetFormat(format));
                _graphics.EndContainer(clipState);
                return;
            }

            if (format.WrapMode == WrapMode.NoWrap && value.Contains('\n'))
            {
                var lines = value.Split('\n');
                var y     = xRect.Y;
                for (var i = 0; i < lines.Length; i++)
                {
                    var line = lines[i].Trim('\r');
                    _graphics.DrawString(line, xFont, (BrushBase)brush, new XRect(xRect.X, y, xRect.Width, resultRect.Height), GetFormat(format));
                    y += resultRect.Height;
                }
                _graphics.EndContainer(clipState);
                return;
            }

            // http://developer.th-soft.com/developer/2015/07/17/pdfsharp-improving-the-xtextformatter-class-measuring-the-height-of-the-text/
            // http://developer.th-soft.com/developer/2015/09/21/xtextformatter-revisited-xtextformatterex2-for-pdfsharp-1-50-beta-2/
            var tf = new XTextFormatter(_graphics)
            {
                Alignment = GetAlignment(format)
            };

            tf.DrawString(value, xFont, (BrushBase)brush, xRect);

            _graphics.EndContainer(clipState);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Renders a whole MigraDoc document scaled to a single PDF page.
        /// </summary>
        static void SamplePage2(PdfDocument document)
        {
            PdfPage   page = document.AddPage();
            XGraphics gfx  = XGraphics.FromPdfPage(page);

            // HACK²
            gfx.MUH  = PdfFontEncoding.Unicode;
            gfx.MFEH = PdfFontEmbedding.Default;

            // Create document from HalloMigraDoc sample
            Document doc = HelloMigraDoc.Documents.CreateDocument();

            // Create a renderer and prepare (=layout) the document
            MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
            docRenderer.PrepareDocument();

            // For clarity we use point as unit of measure in this sample.
            // A4 is the standard letter size in Germany (21cm x 29.7cm).
            XRect A4Rect = new XRect(0, 0, A4Width, A4Height);

            int pageCount = docRenderer.FormattedDocument.PageCount;

            for (int idx = 0; idx < pageCount; idx++)
            {
                XRect rect = GetRect(idx);

                // Use BeginContainer / EndContainer for simplicity only. You can naturaly use you own transformations.
                XGraphicsContainer container = gfx.BeginContainer(rect, A4Rect, XGraphicsUnit.Point);

                // Draw page border for better visual representation
                gfx.DrawRectangle(XPens.LightGray, A4Rect);

                // Render the page. Note that page numbers start with 1.
                docRenderer.RenderPage(gfx, idx + 1);

                // Note: The outline and the hyperlinks (table of content) does not work in the produced PDF document.

                // Pop the previous graphical state
                gfx.EndContainer(container);
            }
        }
        /// <summary>
        /// Demonstrates the use of XGraphics.Transform.
        /// </summary>
        public override void RenderPage(XGraphics gfx)
        {
            XGraphicsContainer container1; //, container2;

            base.RenderPage(gfx);

            container1 =
                gfx.BeginContainer(new XRect(20, 50, 100, 200), new XRect(0, 0, 10, 10), XGraphicsUnit.Point);
            gfx.DrawLine(XPens.Blue, 0, 0, 10, 10);
            gfx.EndContainer(container1);

            container1 =
                gfx.BeginContainer(new XRect(220, 50, 100, 200), new XRect(0, 0, 10, 10), XGraphicsUnit.Point);
            gfx.DrawLine(XPens.Blue, 0, 0, 10, 10);
            gfx.EndContainer(container1);

#if true_
            gfx.SetClip(new XRect(20, 20, 300, 500));
            gfx.DrawRectangle(XBrushes.Yellow, 0, 0, gfx.PageSize.Width, gfx.PageSize.Height);

            gfx.SetClip(new XRect(100, 200, 300, 500), XCombineMode.Intersect);
            gfx.DrawRectangle(XBrushes.LightBlue, 0, 0, gfx.PageSize.Width, gfx.PageSize.Height);

            gfx.DrawLine(XPens.MediumSlateBlue, 0, 0, 150, 200);
            gfx.DrawPolygon(properties.Pen1.Pen, GetPentagram(75, new PointF(150, 200)));


            Matrix matrix = new Matrix();
            //matrix.Scale(2f, 1.5f);
            //matrix.Translate(-200, -400);
            //matrix.Rotate(45);
            //matrix.Translate(200, 400);
            //gfx.Transform = matrix;
            //gfx.TranslateTransform(50, 30);

#if true
            gfx.TranslateTransform(30, 40, XMatrixOrder.Prepend);
            gfx.ScaleTransform(2.0f, 2.0f, XMatrixOrder.Prepend);
            gfx.RotateTransform(15, XMatrixOrder.Prepend);
#else
            gfx.TranslateTransform(30, 40, XMatrixOrder.Append);
            gfx.ScaleTransform(2.0f, 2.0f, XMatrixOrder.Append);
            gfx.RotateTransform(15, XMatrixOrder.Append);
#endif
            bool id = matrix.IsIdentity;
            matrix.Scale(2.0f, 2.0f, MatrixOrder.Prepend);
            //matrix.Translate(30, -50);
            matrix.Rotate(15, MatrixOrder.Prepend);
            Matrix mtx = gfx.Transform.ToMatrix();
            //gfx.Transform = matrix;

            gfx.DrawLine(XPens.MediumSlateBlue, 0, 0, 150, 200);
            gfx.DrawPolygon(properties.Pen2.Pen, GetPentagram(75, new PointF(150, 200)));

            gfx.ResetClip();

            gfx.DrawLine(XPens.Red, 0, 0, 1000, 1000);

            gfx.DrawPolygon(XPens.SandyBrown, GetPentagram(75, new PointF(150, 200)));
#endif
        }
Exemplo n.º 5
0
        public override void RenderPage(XGraphics gfx)
        {
            base.RenderPage(gfx);

            PointF[] origins = new PointF[]
            {
                new PointF(100, 200), new PointF(300, 200),
                new PointF(100, 400), new PointF(300, 400),
                new PointF(100, 600), new PointF(350, 600),
            };
            PointF             origin;
            XGraphicsContainer container;
            float length = 100;

            // Not transformed
            origin = origins[0];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, origin);

            // Translation
            container = gfx.BeginContainer(new RectangleF(10, 10, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[1];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(20, -30);
            DrawAxes(gfx, XPens.DarkGray, origin, length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, origin);
            gfx.EndContainer(container);

            // Scaling
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[2];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.ScaleTransform(1.3f, 1.5f);
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Rotation
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[3];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.RotateTransform(-45);
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Skewing (or shearing)
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[4];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(1, -0.3f, -0.4f, 1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Reflection
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[5];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(-1, 0, 0, -1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);
        }