PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Mon Sep 19, 2016 3:17 pm 
Offline

Joined: Fri Sep 09, 2016 6:53 pm
Posts: 3
Hello,

I am creating a dynamic PDF using MigraDoc and PDFSharp.

Since i mentioned its a dynamic PDF, the way i created is more complicated since i need to use of lot of styling attributes in the dynamic data. First I prepare data from the JSON inputs and i will it to print into PDF. One piece of code is shown below.

xPosition = 1.2;
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
// Render the paragraph.
// Rendering unit is in Centimeters
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(xPosition ), XUnit.FromCentimeter(yPosition), "18.4cm", dataToPrint);

In the above piece of code, the dataToPrint variable is the actual data grabbed from the JSON variable. After printing the above, i need to calculate the YPosition manually to print the next data in the JSON variable.

I calculate yPosition by considering there will be 80 characters in a line but i know that method doesn't work all the time since PDFSharp uses vector graphics and also line length varies by length of individual characters.

In iTextSharp there is a variable called PdfWriter.getVerticalPosition() to get the current yPosition.

Is there any variable in PDFSharp/MigraDoc to get the yPosition after printing in the PDF???

Any help would be much appreciated because i couldn't find a proper solution for a long time. Thanks in advance.


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 19, 2016 7:56 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Hi!

You should be able to determine the height of the objects drawn by RenderObject.

If you use MigraDoc as intended, MigraDoc will keep track of the Y position.
Are there reasons why you cannot use the normal MigraDoc mechanism?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 20, 2016 1:36 pm 
Offline

Joined: Fri Sep 09, 2016 6:53 pm
Posts: 3
The data is too dynamic also the data in JSON needs to be processed for styling purpose.

For Example, the Json data will be like,

var xViewModel = new List<XViewModel >()
{
new xViewModel {
ContentTitle = "<span><b>Content Title</b></span>",
ContentValue = "<span>The Content Values comes here</span><span><b colorPropSet=''Report_Blue''>Q | Some Question here</b>
<Child JSON comes here>
}
};


The above view model goes like a big loo. It may have a 100 child elements or even more.

The problem is I need to process the styling properties of each "ContentTitle" and "ContentValue" and will assign the properties like color, font, font-size and so on.

Before i inject the values to MigraDoc/PDFSharp, i need to do lots of processing.. I would say i wrote a custom parsing functionality which will suit the need.

The above part restricts me to use the full feature of the MigraDoc/PDFSharp inbuilt Y position.

Is there any other way i can get the Y Position after printing/rendering the content ? ? It would be really helpful...

Please let me know if you need more information.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 21, 2016 4:03 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
jenurius wrote:
Please let me know if you need more information.
You explain what you are doing.
You do not explain why you do it in a somewhat complicated way (using RenderObject).

You do not explain why you cannot use MigraDoc as intended (create one document, add all the paragraphs you need, and finally let MigraDoc create a document for you). If you do so, you do not have to bother about the Y position and MigraDoc can even break paragraphs across page boundaries.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2016 1:32 pm 
Offline

Joined: Fri Sep 09, 2016 6:53 pm
Posts: 3
My work is to generate a report after a survey is answered.

The reason i do in a complicated way is because of the structure.

Example structure/data to be printed.

Content 1: data={"Some Content to be printed 1"}, Properties:{TextColor:"Red", Font: "SomeFont 1" more properties};
Content 2: data={"Some Content to be printed 2"}, Properties:{TextColor:"Green", Font: "SomeFont 2" more properties};
Content n: data={"Some Content to be printed n"}, Properties:{TextColor:"Black", Font: "SomeFont n" more properties};

In the above scenario, I need to print the content 1 with all the properties and Content 2 with all the properties and so on. So I need to use RenderObject way as i mentioned in the previous comments.

Also there are lots of custom elements like hyperlinks embedded in a rectangle as buttons will be printed too.

That's the reason I got to choose the complicated way.

May be a stupid way but that's what i understood with the available docs.

Really appreciated for replying and will be very helpfull for me if find a solution or a better way of doing this.

Hope the explanation i gave explains my problem/situation.

Thanks.


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 29, 2016 3:52 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
jenurius wrote:
In the above scenario, I need to print the content 1 with all the properties and Content 2 with all the properties and so on. So I need to use RenderObject way as i mentioned in the previous comments.
I would do this the normal way without RenderObject.
With MigraDoc you can mix several fonts and several text colors within a single paragraph. Where's the problem?

jenurius wrote:
Also there are lots of custom elements like hyperlinks embedded in a rectangle as buttons will be printed too.
I most likely would do this the normal way without RenderObject.
If needed I would use RenderPage and add some placeholders to the document that will be drawn with PDFsharp after RenderPage.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 223 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