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);
            }
        }
示例#2
0
 public InternalGraphicsState(XGraphics gfx, XGraphicsContainer container)
 {
     this.gfx = gfx;
     container.InternalState = this;
     //#if GDI
     //      //GdiGraphicsState = container.GdiState;
     //      this.gfx = gfx;
     //      container.InternalState = this;
     //#endif
     //#if WPF
     //      this.gfx = gfx;
     //      container.InternalState = this;
     //#endif
 }
示例#3
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);
        }
示例#4
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);
            }
        }
示例#5
0
 public void EndContainer(XGraphicsContainer container)
 {
   BeginGraphic();
   RestoreState(container.InternalState);
   //      PdfGraphicsState pdfstate = (PdfGraphicsState)container.Handle;
   //      this.dirty |= DirtyFlags.Ctm;
   //      this.gfxState = pdfstate;
 }
示例#6
0
    public void BeginContainer(XGraphicsContainer container, XRect dstrect, XRect srcrect, XGraphicsUnit unit)
    {
      // Before saving, the current transformation matrix must be completely realized.
      BeginGraphic();
      RealizeTransform();
      this.gfxState.InternalState = container.InternalState;
      SaveState();
      //throw new NotImplementedException("BeginContainer");
      //      PdfGraphicsState pdfstate = (PdfGraphicsState)this.gfxState.Clone();
      //      this.gfxStateStack.Push(pdfstate);
      //      container.Handle = pdfstate;
      //      container.Handle = this.gfxState.Clone();

      //      Matrix matrix = new Matrix();
      //      matrix.Translate(srcrect.X, srcrect.Y);
      //      matrix.Scale(dstrect.Width / srcrect.Width, dstrect.Height / srcrect.Height);
      //      matrix.Translate(dstrect.X, dstrect.Y);
      //      Transform = matrix;
    }
示例#7
0
 public void EndContainer(XGraphicsContainer container)
 {
     BeginGraphicMode();
     RestoreState(container.InternalState);
 }
示例#8
0
 public void BeginContainer(XGraphicsContainer container, XRect dstrect, XRect srcrect, XGraphicsUnit unit)
 {
     // Before saving, the current transformation matrix must be completely realized.
     BeginGraphicMode();
     RealizeTransform();
     _gfxState.InternalState = container.InternalState;
     SaveState();
 }
 public InternalGraphicsState(XGraphics gfx, XGraphicsContainer container)
 {
     _gfx = gfx;
     container.InternalState = this;
 }