Today we will generate a PDF with the open source Scryber.
The best way to work with Scryber is to use a template (PDFX file) for the style, the layout and the static content, then insert the dynamic content with C#.
Full documentation at this link.
Good example at this link.
Create a new XML file and rename it PdfScript.pdfx:
<?xml version="1.0" encoding="utf-8"?>
<pdf:Document xmlns:pdf="Scryber.Components, Scryber.Components" auto-bind="true" >
<Pages>
<pdf:Page id="MyFirstPage" >
<Content>
Hello World
</Content>
</pdf:Page>
</Pages>
</pdf:Document>Now you can load the template and modify it, then generate the PDF file:
using System.Drawing;
using Scryber.Components;
...
//Load the template containing a page with "Hello World"
using (var doc = PDFDocument.ParseDocument("../../PdfScript.pdfx"))
{
//Add a new label to the existing page
var lbl1 = new PDFLabel {Text = "\r\nHello World Again"};
lbl1.Style.Fill.Color = Color.Aquamarine;
lbl1.Style.Position.PositionMode = Scryber.Drawing.PositionMode.Block;
doc.Pages[0].Contents.Add(lbl1);
//Add a new page to the template dynamically
var page = new PDFPage
{
PaperSize = Scryber.PaperSize.A4,
PaperOrientation = Scryber.PaperOrientation.Portrait
};
doc.Pages.Add(page);
//Add some content to this new page
var lbl2 = new PDFLabel {Text = "This is the second page!"};
page.Contents.Add(lbl2);
doc.ProcessDocument("MyFirstPDF.pdf", System.IO.FileMode.Create);
}Enjoy! 😉






No Comments Yet