PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Clip Text in a Rectangle both vertical and horizontal
http://forum.pdfsharp.de/viewtopic.php?f=2&t=1059
Page 1 of 1

Author:  bobski99 [ Thu Feb 18, 2010 12:02 pm ]
Post subject:  Clip Text in a Rectangle both vertical and horizontal

I want to draw text inside a rectangle, if the text is too large then I want it clipped. I tried

gfx.DrawString("Created with PDF from PDF Sharp", font, XBrushes.DarkOrchid, rect,XStringFormats.TopLeft);

but the text is not clipped as it exceeds the vertical or horizontal dimensions of the rectangle.

Author:  Thomas Hoevel [ Thu Feb 18, 2010 3:43 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

The rectangle is used for alignment only, not for clipping.

Author:  bobski99 [ Wed Feb 24, 2010 1:13 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

Is there any way to clip text so that if the font is too large for the rectangle then it is clipped?

Author:  DaleStan [ Wed Feb 24, 2010 7:24 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

I think I had something like that happen when I drew a rectangle after (and overlapping) the text; would that work?

Author:  Thomas Hoevel [ Thu Feb 25, 2010 12:43 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

That's not really clipping, I'd call that hiding.
You can draw a white rectangle to hide the text that should be clipped.

Author:  gwinter [ Thu Oct 14, 2010 2:39 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

I too am having the same lack of clipping issue in a rectangle, but if the rectangle is only used for alignment, then this would explain the lack of clipping. I think that there could be a work around for this lack of text clipping. I am trying to work out the code (with some problems) of creating a temporary document that I would draw the text into unclipped. Then, use the drawImage method to only select the part of the temporary document that is within the rectangle dimensions is the source rectangle and draw this into the same size rectangle as the destination rectangle in the current document. This should effectivly copy the clipped text from the temp document into the current document as an image, correct? Or am I missing something? Currently when I do this, I do get the very first string, but it appears to have very little height, so when it is transferred to the current document, it is very small. Perhaps I have a scaling problem? I am using the XGraphicsUnit::Point for scaling, but still having the problem? Any idea if this idea would even work before I spend more hours/days on it?

Thanks in advance,
George Winter

Author:  Thomas Hoevel [ Thu Oct 14, 2010 3:40 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

AFAIK there is no clipping available in PDF.

Therefore IIRC DrawImage won't do any clipping (the parameter is there for compatibility but is not used).

Author:  gwinter [ Fri Oct 15, 2010 2:05 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

Thanks for the reply Thomas.

I guess this is why I have not gotten it to work properly.

George-

Author:  gwinter [ Fri Oct 15, 2010 6:56 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

Hi again Thomas & Everyone,
I started thinking that there should be some way to do clipping of text. It is not elegant, but I came up with a solution.
Note, the font I am using has a height of 33 and the destination rectangle has a height of 23. This code has not been tweaked for error checking.
I will explain what I did in code:
//--------------------------------------------------
//If only partial line used, then draw into a temporary document of a clipped size and adjust the drawing such that it starts
//adjusted and therefore clips the code drawn into the temporary PDF document. So:

//Open a temporary PDF document
PdfDocument^ tempDocument = gcnew PdfSharp::Pdf::PdfDocument();

//Add a page
PdfPage^ tempPage = tempDocument->AddPage();

//Make all measurements relative to 0,0 and setup a small rectangle for the size of the page to draw into.
//Note that this will bypass the regular page sizes. "Frame" is a rectangle with its width and height set
PdfRectangle^ pdfRect = gcnew PdfRectangle(XPoint(0, 0), XSize(Frame.Width(), Frame.Height())); //PDFSharp need the origin to be set to 0,0

//Set the size of the new page in the temporary document
tempPage->MediaBox = pdfRect; //now set the rectangle as the whole page size

//Now get the graphics object from this newly sized temporary page
XGraphics^ tempGraphicsDrawingObject = XGraphics::FromPdfPage(tempPage);

//Now draw the string into this page adjusting the height for the new height of the frame
tempGraphicsDrawingObject->DrawString( outputString, m_fontObject, m_brush, 0, Frame.Height());

//And save to a temporary file
tempDocument->Save("c:/temp/tempjunk.pdf");

//This is where we load the image object of the temporary PDF document. There must be a way to do
//this somehow without saving and reloading the PDF document to disk. Anyone have any ideas?
XImage^ xImage = XImage::FromFile("c:/temp/tempjunk.pdf");

//Now, go back to the original document and specify the rectangle where you want to insert the WHOLE temporary image into.
XRect dRect = XRect(Frame.MinX(), Frame.MinY(), Frame.Width(), Frame.Height());

//Now draw in the image to the original document at the specified coordinates and thus we have clipping.
m_graphicsDrawingObject->DrawImage(xImage, dRect); //,, xRect, XGraphicsUnit::Point);

//It is at this point I delete the temporary PDF file using our custom crossplatform function to do so (not shown).
//--------------------------------------------------

And this is how text clipping can be done.
Best Regards,
George

Author:  SiliconMind [ Thu Feb 17, 2011 12:55 pm ]
Post subject:  Re: Clip Text in a Rectangle both vertical and horizontal

George, instead of saving temp PDF file, just use XForm. It works exactly the same, but you don't have to write anything to disk.
You can find code samples here: http://www.pdfsharp.net/wiki/Form_Objects.ashx

That's how I do clipping of GraphicsPaths.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/