PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jul 03, 2024 11:28 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Thu Feb 18, 2010 12:02 pm 
Offline

Joined: Thu Feb 18, 2010 11:57 am
Posts: 2
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 18, 2010 3:43 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
The rectangle is used for alignment only, not for clipping.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 24, 2010 1:13 pm 
Offline

Joined: Thu Feb 18, 2010 11:57 am
Posts: 2
Is there any way to clip text so that if the font is too large for the rectangle then it is clipped?


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 24, 2010 7:24 pm 
Offline

Joined: Mon Jan 04, 2010 2:44 pm
Posts: 23
I think I had something like that happen when I drew a rectangle after (and overlapping) the text; would that work?


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 25, 2010 12:43 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
That's not really clipping, I'd call that hiding.
You can draw a white rectangle to hide the text that should be clipped.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 14, 2010 2:39 pm 
Offline

Joined: Thu Oct 14, 2010 2:09 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 14, 2010 3:40 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 15, 2010 2:05 pm 
Offline

Joined: Thu Oct 14, 2010 2:09 pm
Posts: 3
Thanks for the reply Thomas.

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

George-


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 15, 2010 6:56 pm 
Offline

Joined: Thu Oct 14, 2010 2:09 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 17, 2011 12:55 pm 
Offline

Joined: Thu Feb 17, 2011 12:27 pm
Posts: 11
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.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 44 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