PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Wed Aug 13, 2025 11:10 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: SetClip deprecated
PostPosted: Fri Nov 16, 2012 10:22 am 
Offline

Joined: Fri Nov 16, 2012 10:16 am
Posts: 2
I've inherited an old .NET 1.1 project which I am migrating to .NET 3.5. This included an old version of PdfSharp which I've replaced with the latest. However, there is some PdfSharp code used in our project which is now deprecated. I include an example below:

Code:
      private void DrawBody(XGraphics gr)
      {
         if (resources != null)
         {
            using (Pen pen = new Pen(new SolidBrush(titleBackground),2))
            {
               RectangleF oldClip = gr.Graphics.ClipBounds;
               try
               {
                  HPResource resource;
                  RectangleF clipRect = CalcClipRect(gr);
                  int i = 0;
                  int row = startRow;

                  lastPrintPos = headerHeight+1;
                  while ((row < resources.Count) && (lastPrintPos+resources[row].RowHeight <= Math.Round(clipRect.Bottom+(toPrinter ? 0 : resources.PlanHeight), 0)))
                  {
                     resource = resources[row];
                     ticBottom = lastPrintPos+resource.RowHeight;

                     // get the content drawn
                     DrawBodyContent(gr, resource);
                     

                     i++;
                     gr.DrawLine(pen, left, ticBottom, left+width-1, ticBottom);
                     lastPrintPos = ticBottom;
                     row++;
                     lastPrintRow = row ;
                  }
                  printFinished = (row == resources.Count && canFinish);
               }
               finally
               {
                  gr.SetClip(oldClip, XCombineMode.Replace);
               }
            }
         }
      }


The call to SetClip in the finally clause is throwing an error since this method is deprecated in the latest PdfSharp. The error suggests using IntersectClip instead but this seems to do something different. How can I achieve the same result in the new PdfSharp as the deprecated SetClip call?

Any help greatly appreciated.


Top
 Profile  
Reply with quote  
 Post subject: Re: SetClip deprecated
PostPosted: Mon Nov 19, 2012 9:30 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3134
Location: Cologne, Germany
Short version: Deprecated functions lead to warnings, not errors. Disable the warning in the line above SetClip and you're done.

Long version: I don't know how to replace SetClip with IntersectClip. After a quick glance at your code, I'd try Save and Restore instead ClipBounds/SetClip.
See this sample (final two procedures):
http://www.pdfsharp.net/wiki/Graphics-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: SetClip deprecated
PostPosted: Mon Nov 19, 2012 9:39 am 
Offline

Joined: Fri Nov 16, 2012 10:16 am
Posts: 2
Thomas Hoevel wrote:
Short version: Deprecated functions lead to warnings, not errors. Disable the warning in the line above SetClip and you're done.

Long version: I don't know how to replace SetClip with IntersectClip. After a quick glance at your code, I'd try Save and Restore instead ClipBounds/SetClip.
See this sample (final two procedures):
http://www.pdfsharp.net/wiki/Graphics-sample.ashx


Many thanks. In this case it does produce an error. Looking at the pdfsharp code for SetClip shows why:

Code:
    /// <summary>
    /// Sets the clipping region to the specified graphical path.
    /// </summary>
    [Obsolete("Use IntersectClip", true)]
    public void SetClip(XRect rect, XCombineMode combineMode)
    {
      throw new InvalidOperationException("Function is obsolete. Use IntersectClip.");
      //XGraphicsPath path = new XGraphicsPath();
      //path.AddRectangle(rect);
      //SetClip(path, combineMode);
    }


I've replaced the SetClip call in the finally clause with the following which seems to be the equivalent:

Code:
       gr.Graphics.SetClip(oldClip, CombineMode.Replace);


Other than that I don't really want to change this legacy code unless I have to!


Top
 Profile  
Reply with quote  
 Post subject: Re: SetClip deprecated
PostPosted: Mon Nov 19, 2012 10:35 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3134
Location: Cologne, Germany
Looks like your code actually needs "ResetClip" instead of "SetClip".
ResetClip is also obsolete, comment tells to use Save/Restore instead.

I understand you only want to make minimal changes to legacy code.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group