Implements the IGraphics interface for the PDF-Sharp output.
Every point, path, rectangle and so on is measured in pixels. PDF doesn't know about pixels, but dots. One dot is the 72th of an inch. So one has to apply some scaling for alle drawn elements. This can be done with a call to ScaleTransform for example to get from pixel coordinates to dot coordinates. This solution has one disadvantage: There are a few points where PDF-Sharp scales from pixels to dots itself: fonts and images. If we don't care about this, all fonts and images would appear to small in the resulting PDF. The solution is to "unscale" those elements first. To do this, there are the two properties ScaleFont and ScaleImage. //We take the DPI of the screen from a Control Graphics gdiGraphics = (new Control()).CreateGraphics(); //Apply a scaling to get from pixels to dots graphics.ScaleTransform(72.0f / gdiGraphics.DpiX, 72.0f / gdiGraphics.DpiY); //Unscale the font graphics.ScaleFont = gdiGraphics.DpiY / 72.0f; //Unscale the images graphics.ScaleImage = gdiGraphics.DpiX / 72.0f; graphics.TakeInitialTransform(); Word wrap is slightly different to the GDI word wrapping. If a single word is longer than a line, GDI breaks the word and prints the rest in a new line. Here the rest isn't printed but an ellipsis is appendet to the not fitting word. The following points are on the to do list: - Clipping isn't very good right now. We should unerstand the regions. There are a few things which are not perfect and won't be changed now: - Only horizontal, vertical and diagonal gradients are drawn correctly. Thats ok since NClass dosn't use any other gradients. - Images get a gray border in pdf if they have colored (even white) pixels next to transparent pixels. A workaround is to draw them by GDI on a surface. This surface is white. This looks good until the backgound of the entity is also white. If not, images are inside a white box. - If one draws with a gradient brush and the next drawing operation uses a black brush, this drawing is done with the gradient again. That's because the brush isn't changed in pdf. This is a bug in PDFSharp.
Inheritance: IGraphics
Exemple #1
0
        // ========================================================================
        // Methods

        #region === Methods

        /// <summary>
        /// Exports the given diagram into a PDF. The PDF is saved at the location
        /// given in <see cref="fileName"/>. If selectedOnly is true, only the selected
        /// elements of the diagram get exported.
        /// </summary>
        public void Export(Graphics g)
        {
            if (MonoHelper.IsRunningOnMono)
            {
                GlobalFontSettings.FontResolver = new NClassFontResolver();
            }

            PdfDocument document    = new PdfDocument();
            PdfPage     page        = document.AddPage();
            RectangleF  diagramSize = nclassDocument.GetPrintingArea(selectedOnly);

            page.Width  = new XUnit(diagramSize.Width, XGraphicsUnit.Presentation) + new XUnit(padding.Right * 2);
            page.Height = new XUnit(diagramSize.Height, XGraphicsUnit.Presentation) + new XUnit(padding.Bottom * 2);

            XGraphics   gfx      = XGraphics.FromPdfPage(page);
            PDFGraphics graphics = new PDFGraphics(gfx);

            //Translate because of the padding.
            graphics.TranslateTransform(padding.Left, padding.Top);

            //Do some scaling to get from pixels to dots...
            Graphics gdiGraphics = g;

            graphics.ScaleTransform(72.0f / gdiGraphics.DpiX, 72.0f / gdiGraphics.DpiY);
            //Fonts are already mesured in dots but the size of them is also scaled by the above
            //transformation. So we have to applay an opposite transformation on the fonts first.
            graphics.ScaleFont = gdiGraphics.DpiY / 72.0f;
            //PDF-Sharp also does the scaling for the images itself. So we have to revert this as
            //we did it for the fonts.
            graphics.ScaleImage = gdiGraphics.DpiX / 72.0f;

            //Move everything to the top left corner
            graphics.TranslateTransform(-diagramSize.X, -diagramSize.Y);

            graphics.TakeInitialTransform();

            nclassDocument.Print(graphics, selectedOnly, Style.CurrentStyle);

            //Clean up...
            while (gfx.GraphicsStateLevel > 0)
            {
                gfx.Restore();
            }

            document.Options.CompressContentStreams = true;
            try
            {
                document.Save(fileName);
                Successful = true;
            }
            catch (IOException e)
            {
                MessageBox.Show(String.Format(Strings.Error_CoulNotWritePDF, e.Message), Strings.ErrorDialog_Title,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #2
0
    // ========================================================================
    // Methods

    #region === Methods

    /// <summary>
    /// Exports the given diagram into a PDF. The PDF is saved at the location
    /// given in <see cref="fileName"/>. If selectedOnly is true, only the selected
    /// elements of the diagram get exported.
    /// </summary>
    public void Export()
    {
      PdfDocument document = new PdfDocument();
      PdfPage page = document.AddPage();
      RectangleF diagramSize = nclassDocument.GetPrintingArea(selectedOnly);
      page.Width = new XUnit(diagramSize.Width, XGraphicsUnit.Presentation) + new XUnit(padding.Right*2);
      page.Height = new XUnit(diagramSize.Height, XGraphicsUnit.Presentation) + new XUnit(padding.Bottom*2);

      XGraphics gfx = XGraphics.FromPdfPage(page);
      PDFGraphics graphics = new PDFGraphics(gfx);

      //Translate because of the padding.
      graphics.TranslateTransform(padding.Left, padding.Top);

      //Do some scaling to get from pixels to dots...
      Graphics gdiGraphics = (new Control()).CreateGraphics();
      graphics.ScaleTransform(72.0f/gdiGraphics.DpiX, 72.0f/gdiGraphics.DpiY);
      //Fonts are already mesured in dots but the size of them is also scaled by the above
      //transformation. So we have to applay an opposite transformation on the fonts first.
      graphics.ScaleFont = gdiGraphics.DpiY/72.0f;
      //PDF-Sharp also does the scaling for the images itself. So we have to revert this as
      //we did it for the fonts.
      graphics.ScaleImage = gdiGraphics.DpiX/72.0f;

      //Move everything to the top left corner
      graphics.TranslateTransform(-diagramSize.X, -diagramSize.Y);

      graphics.TakeInitialTransform();

      nclassDocument.Print(graphics, selectedOnly, Style.CurrentStyle);

      //Clean up...
      while(gfx.GraphicsStateLevel > 0)
      {
        gfx.Restore();
      }

      document.Options.CompressContentStreams = true;
      try
      {
        document.Save(fileName);
        Successful = true;
      }
      catch(IOException e)
      {
        MessageBox.Show(String.Format(Strings.Error_CoulNotWritePDF, e.Message), Strings.ErrorDialog_Title,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }