PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Sep 27, 2024 5:16 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Fri Nov 29, 2013 10:01 am 
Offline

Joined: Mon Nov 04, 2013 12:58 pm
Posts: 8
hi, is anyone using ben fosters migradoc extensions?

https://github.com/benfoster/MigraDoc.Extensions

i was searching for something to convert html and this works a treat! however, i've got one issue...

in the example provided, he uses a section:

Code:
var doc = new Document();
var section = doc.AddSection();

var html = File.ReadAllText("example.html");
section.AddHtml(html);

var renderer = new PdfDocumentRenderer();
renderer.Document = doc;
renderer.RenderDocument();

renderer.Save(outputName);


however, i've been using a section with paragraphs so i can position the text in my template:

Code:
var gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
gfx.MFEH = PdfFontEmbedding.Default;

var doc = new Document();
var sec = doc.AddSection();
var profile = sec.AddParagraph();

var html = File.ReadAllText("example.html");
profile.AddText(html );

var docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(21.2), XUnit.FromCentimeter(7.5), XUnit.FromCentimeter(6.6), profile);


try as i might, i cant figure out a way to create a section as his example and then position it correctly...

does anyone have any pointers? this feels like i'm 99% of the way there and i'm just missing the last bit...

any suggestions would be tops!

cheers,

jake


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2013 9:37 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3110
Location: Cologne, Germany
You can create a dummy document with a section, call AddHtml for that section and see how many paragraphs have been added.
With the Clone() method, you can add one or more of those paragraphs to the section in your document.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2013 10:43 am 
Offline

Joined: Mon Nov 04, 2013 12:58 pm
Posts: 8
hi thomas, thank you for replying - feel one step closer to nailing this ;)

i've investigated your suggestion and can see the logic in what you suggest. however, i cant figure out how to transfer the paragraphs in the dummy document to the main document? it feels like it should be a foreach - loop through the paragraphs and clone them after the html conversion has taken place?

Code:
//the main document
var doc = new Document();
var sec = doc.AddSection();

//paragraph that gets absolutely positioned
var profile = sec.AddParagraph();
profile.AddFormattedText(String.Format("{0}\r\n\r\n", toscAdminUser.Position));

//dummy doc to do the html conversion
var dummyDoc = new Document();
var dummySection = doc.AddSection();
DefineStyles(dummyDoc);
dummySection.AddHtml("<p>my html text</p><p>with two paragraphs</p>");

//add the converted html section here?!

//position the profile
var docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(21.2), XUnit.FromCentimeter(7.5), XUnit.FromCentimeter(6.6), profile);


i've been examining the 'dummySection' in debug and can see things like 'elements' which contains the paragraphs, i just cant figure out how to get at them?!?!

almost there, if you've any pointed they'd be greatly received ;)

cheers,

jake


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2013 12:56 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3110
Location: Cologne, Germany
You can use Elements.Count, Elements[i], and foreach should also work.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2013 2:27 pm 
Offline

Joined: Mon Nov 04, 2013 12:58 pm
Posts: 8
ok, given it a go - i've successfully managed to loop over the paragraphs but ran into some issues with fonts and positioning...

each page in my pdf has a section and i position the paragraphs in the section. i cant figure out how to position the section, so cloning the paragraphs between sections wont help...

so i tried this:

Code:
//the main document
var doc = new Document();
var sec = doc.AddSection();

//paragraph that gets absolutely positioned
var profile = sec.AddParagraph();
profile.AddFormattedText(String.Format("{0}\r\n\r\n", toscAdminUser.Position));

//dummy doc to do the html conversion
var dummyDoc = new Document();
var dummySection = doc.AddSection();
DefineStyles(dummyDoc);
dummySection.AddHtml("<p>my html text</p><p>with two paragraphs</p>");

//position the profile
var docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();

//position paragraphs
foreach (Paragraph element in dummySection .Elements)
{
   var el = element;
   el.Format.Font.Name = "Helvetica Neue LT Pro 45 Light";
   el.Format.Font.Size = 8;
   el.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black;
   el.Format.Alignment = ParagraphAlignment.Left;
   docRenderer.RenderObject(gfx, XUnit.FromCentimeter(21.2), XUnit.FromCentimeter(7.5), XUnit.FromCentimeter(3), el);
}


this brings in the formatted text but... it ignores the width (meaning the text flows off the page) and of course it plonks each paragraph on top of each other! i tried to change the y positioning but of course that's kinda impossible as i've no idea of the height of each paragraph!

feels like i'm so close... and on the verge of trying a find and replace routine to fix the html but would love to get this working!

cheers,

jake


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 02, 2013 2:41 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3110
Location: Cologne, Germany
Instead of drawing each paragraph individually, you can call "docRenderer.RenderPage(gfx, 1); " to draw the whole page.
The section has a PageSetup that allows you to specify page size and margins (top, left, right, bottom).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 06, 2013 2:54 pm 
Offline

Joined: Mon Nov 04, 2013 12:58 pm
Posts: 8
'fraid i had to abandon this... just couldnt get the darn thing to work, such a shame as the html conversion was perfect!

in the end, i've just use the HtmlAgilityPack library to convert the <p> tags to line breaks. it kinda gets the project i'm working on off the ground.

if i do find a way to do it, i'll post it up here!

cheers,

jake


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

All times are UTC


Who is online

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