SharpLeaf Tutorials > Document Layout Tutorials > Advanced DocumentLayouts

Advanced DocumentLayouts

Examples of multi-frame and multi-page document layouts.

Using two frames per page

   paper = PaperSize.Landscape(PaperSize.A5);
   left = 36;
   right = -36;
   top = 36;
   bottom = -48;  // Leave more room at bottom for footer
   center = 0.5 * paper[0];
   cl = center - 24;
   cr = center + 24;  // gap around center

   leftframe = new Frame("LeftFrame",left,top,cl,bottom);  // Left-hand frame
   rightframe = new Frame("RightFrame",cr,top,right,bottom);  // Right-hand frame
   rule = new Rule(center,top,center,bottom,1,Color.Gray);  // Rule between frames
   footfont = new FontType("Times",12,FontStyle.Bold,Color.Gray);  // Footer font
  // Page footer with page count, 12 point below bottom
   footer = new TextBlock("Page {page}/{pages}",footfont,left,bottom + 12,right,bottom 
            + 12,Alignment.Center,VerticalAlignment.Top,false);

   page = new PageLayout(paper);
  // Page frames are taken in the order in which they are added
   page.Add(new PageElement[] {leftframe,rightframe,rule,footer});

  // Take a deep copy as of now to ignore further changes to page and its children
   layout = new DocumentLayout(page.Clone());

   lf = new SharpLeaf(layout);
   lf.AddParagraphs(aplparas);


Using alternate page definitions

  // Let's modify the footer alignment to alternate between left and right pages
   footer.Alignment = Alignment.Left;
   leftpage = page.Clone();  // Modify page and freeze
   footer.Alignment = Alignment.Right;
   rightpage = page.Clone();  // Modify page and freeze

  // Use alternating page definitions, starting with a right page
   layout=new DocumentLayout(new PageLayout[] {rightpage,leftpage});

   lf = new SharpLeaf(layout);
   lf.AddParagraphs(aplparas);


Adding special element to first page

   sender = "John Smallcroft\n9256, Downside Close\nButthillbury\nLostshire NW82 "+
            "9ZP\nUnited Kingdom";
   recipient = "Bernard Petitgruet\n137, Impasse des oubliettes\n98350 St-Trou-lès-Oies\nFrance";

  // Let's modify our page to make the first page layout with addresses and a logo
  // Put a logo on top-left corner
   page.Add(new ImageBlock("dyalog.jpg","dyalog.jpg",left,top,72,72));
   addressfont = new FontType();  // Default font
  // Sender address (fixed) on top-right corner with no wrapping
   page.Add(new TextBlock(sender,addressfont,right,top,right,top,Alignment.Right,
            VerticalAlignment.Top,false));
// Recipient address - Frame to be flowed without clip nor skip, so that text 
            // overflows if necessary
   page.Add(new Frame("Recipient",left,144,right,200,false,false,false));
  // First page is a right page - remember we have touched it earlier
   footer.Alignment = Alignment.Right;
   top = 230;
  // Bring frames and rule down to leave space for addresses
   leftframe.SetCoordinates(left,top,cl,bottom);
   rightframe.SetCoordinates(cr,top,right,bottom);
   rule.SetCoordinates(center,top,center,bottom);
  // Need to be explicit about Frame order because Recipient was added after the others
   page.FrameList = new string[]{"Recipient","LeftFrame","RightFrame"};
   firstpage = page.Clone();  // Take deep copy of our new page definition

  // Remember that the first page is a right page
   layout=new DocumentLayout(new PageLayout[] {firstpage,leftpage,rightpage});
   layout.Repeat = 2;  // Repeat only the last two pages of the layout

   lf = new SharpLeaf(layout);
   lf.AddParagraphs(recipient);
   lf.NextFrame();
   lf.AddParagraphs(aplparas);


See also ...

PageLayout Glossary


Send comments on this topic
© Dyalog Ltd 2021