PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jul 02, 2024 11:25 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Apr 06, 2011 2:41 pm 
Offline

Joined: Wed Apr 06, 2011 2:18 pm
Posts: 2
Hi,
Iam using MigraDoc GDI+ in my application...

There is some content which is fetched from Database and Written in PDF through section.AddParagraph().
Code:
DatabaseTableReader dtr = dTable.CreateDataReader();
while(dtr.Read())
{
  Paragraph para = section.AddParagraph(dtr[0].ToString());
  //Some format Code...
}


The Requirement is, the content should not be spill over 2nd page... To Adjust that, either i have to change font size or i can remove some of the content...

Is it possible to know beforehand that the content could get spill over to 2nd page...

I have checked in forum and used KeepWithNext property but still content is written to 2nd page.
The Data which I retrieve from database is variable no of lines... Hence in one scenario, content might finished in half a page... in another it could spill over to 2nd page...

Request you to help in resolving the issue...

Thanks in advance

Seshagiri


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 06, 2011 3:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Hi,

Normally it is an advantage of MigraDoc over PDFsharp that you simply provide the contents of your document and let MigraDoc create as many pages as needed.

If you're allowed to create one page only, PDFsharp could be a better option. You can test for each item whether it fits or not.

If you want to stick to MigraDoc: you can call "GetDocumentObjectsFromPage()" to see what's on the second page.
With KeepWithNext you can control where page breaks may occur. If your paragraphs come in pairs, use KeepWithNext on the first paragraph of each pair to keep them together. If you only print the first page, then it won't contain a half pair.

You can start with e. g. 12 points and render a document. If there are two pages, try again with 11 points, then 10 points.
If it doesn't fit with 8 points, reduce the page margins to get more text.
If you do it right, you have to set the font sizes of a couple styles only before you render the document with a new renderer (e.g. Normal, Heading1, Heading2).
You can also reduce the spacing between paragraphs, maybe switch to a condensed/narrow font.

You can try all the tricks you'd use to get 2 Word pages onto 1 page (OK, MigraDoc doesn't support columns yet).

Another solution: if you get two pages from MigraDoc, then use PDFsharp to combine them on one (like the Two Pages on One sample does).

Is there a risk that a third page could be needed?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 07, 2011 6:10 am 
Offline

Joined: Wed Apr 06, 2011 2:18 pm
Posts: 2
Hi Thomas,
thanks for the suggestion...
It is Actually a Three Page Report... Where the content is predefined as to what content comes in which page...
There is no problem for the first page... but 2nd may spill over to 3rd and/or 3rd may spill over to 4th...

Suppose if I have to use GetDocumentObjectsFromPage(), then content must already be written to page...
For Ex: my code is somewhat like this....
Code:
void GenPDF()
{
  Document document = new Document();
  WriteContentPage1(document);
  document.LastSection.AddPageBreak();
  WriteContentPage2(document);
  document.LastSection.AddPageBreak();
  WriteContentPage3(document);
  PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
  renderer.Document = document;
  renderer.RenderDocument();
  FileStream fStream = new FileStream(Path, FileMode.Create);
  renderer.PdfDocument.Save(fStream);
}
void WriteContentPage1(Document document)
{
  Section section = document.LastSection;
/*Code to Retrieve Data from DB based on some logic...*/
DataTableReader dtr = dTable.CreateDataReader();
while(dtr.read())
{
  section.AddParagraph(dtr[0].ToString());
  //Format Code....
}

}

void WriteContentPage2(Document document)
{
  Section section = document.LastSection;
/*Code to Retrieve Data from DB based on some logic...*/
DataTableReader dtr = dTable.CreateDataReader();
while(dtr.read())
{
  section.AddParagraph(dtr[0].ToString());
  //Format Code....
}

}
void WriteContentPage3(Document document)
{
  Section section = document.LastSection;
/*Code to Retrieve Data from DB based on some logic...*/
DataTableReader dtr = dTable.CreateDataReader();
while(dtr.read())
{
  section.AddParagraph(dtr[0].ToString());
  //Format Code....
}

}

If i have to use GetDocumentObjectsFromPage() and change the Content size then i may have to use some thing like this
Code:
void GenPDF()
{
  Document document = new Document();
  WriteContentPage1(document);
  document.LastSection.AddPageBreak();

  WriteContentPage2(document,false);

  [b]DocumentRenderer docRenderer = new DocumentRenderer(document);
  docRenderer.PrepareDocument();
  DocumentObject[] docObj = docRenderer.GetDocumentObjectsFromPage(2);
 if(docObj.Length>0)
 {
  WriteContentPage2(document,true);
 }[/b]

  document.LastSection.AddPageBreak();

  WriteContentPage3(document, false);

  [b]DocumentRenderer docRenderer = new DocumentRenderer(document);
  docRenderer.PrepareDocument();
  DocumentObject[] docObj = docRenderer.GetDocumentObjectsFromPage(2);
 if(docObj.Length>0)
 {
  WriteContentPage3(document,true);
 
 }[/b]

  PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
  renderer.Document = document;
  renderer.RenderDocument();
  FileStream fStream = new FileStream(Path, FileMode.Create);
  renderer.PdfDocument.Save(fStream);
}
void WriteContentPage2(Document document,bool bChange)
{
if(bChange)
{
  Section section = document.LastSection;
 /*Code to Remove Last specific content*/
/*Code to Get Resized Data from DB based on some logic...*/
DataTableReader dtr = dTable.CreateDataReader();
while(dtr.read())
{
  section.AddParagraph(dtr[0].ToString());
   //Re-Format Code to adjust the content in page....
}
}
else
{
Section section = document.LastSection;
DataTableReader dtr = dTable.CreateDataReader();
while(dtr.read())
{
  section.AddParagraph(dtr[0].ToString());
   //Format Code....
}
 
}

}
//WriteContentPage3() // same as page2


To change the Content Size,i have to get new resized content from DB and Rewrite the content by deleting the previous content...

Is there any way to delete and insert the specific page or content...


thanks in advance....

Seshagiri


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 07, 2011 7:26 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
You can start a new section for every page.
Then it's easy to remove all objects from that section.

Or you can tag all objects (then you don't need to create sections).
This could be more efficient as you can create the whole document.
3 pages? You're done.
4 or more pages? See which "logical pages" spilled over and try again.

Some simplified code snippets:
Deleting (works better when decrementing):
Code:
for (int i = section.Elements.Count - 1; i >= 0; --i)
{
  DocumentObject obj = section.Elements[i];
  if (obj.Tag == "2" || obj.Tag == "3")
    section.Elements.RemoveObjectAt(i);
}


Adding:
Code:
Paragraph paragraph = section.AddParagraph(myText);
paragraph.Tag = "2"; // for second page

Tag is an object so you can define a Tag class if you need more than a single value.

_________________
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 30 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