Hi everybody,
I am a new PDFSharp french user and in fact I've encountered a worry.
I bind one MigraDoc paragraph with MS SQL data source and I dont know how to auto adding pages when you've out of space.
I give you my code, please help me
Code:
protected void LinkButtonPDF_Click(object sender, EventArgs e)
{
DateTime time;
// Create new PDF document
PdfDocument document = new PdfDocument();
time = document.Info.CreationDate;
document.Info.Title = "Fiche produit FISA";
document.Info.Author = "FAUCONNET Ingénierie SA";
document.Info.Subject = "Server time: " +
time.ToString("F", CultureInfo.InvariantCulture);
// Create new page
PdfPage page = document.AddPage();
page.Width = XUnit.FromMillimeter(210);
//page.Height = XUnit.FromMillimeter(297);
// Create graphics object and draw clock
XGraphics gfx = XGraphics.FromPdfPage(page);
//XGraphics formGfx = XGraphics.FromForm(page);
// Create a font
XFont font = new XFont("Verdana", 12, XFontStyle.Regular);
XFont font2 = new XFont("Trebuchet MS", 25, XFontStyle.Bold);
XFont font3 = new XFont("Trebuchet MS", 17, XFontStyle.Bold);
XFont fontTitre = new XFont("Trebuchet MS", 15, XFontStyle.Underline);
String path = "D:/HostingSpaces/jerome@fisa.fr/fisa.fr.sphere-hosting.com/wwwroot/images/bg_top.png";
XImage img = XImage.FromFile(path);
gfx.DrawImage(img, 0, 0, page.Width, 112);
String path2 = "D:/HostingSpaces/jerome@fisa.fr/fisa.fr.sphere-hosting.com/wwwroot/images/logo00.png";
XImage logo = XImage.FromFile(path2);
gfx.DrawImage(logo, 10, 15);
gfx.DrawString("Services et support technique", font2, XBrushes.DarkSlateGray, new XRect(0, 35, page.Width, page.Height), XStringFormat.TopCenter);
gfx.DrawString("Logiciel Fauconnet Ingénirie SA", font3, XBrushes.DarkGray, new XRect(0, 57, page.Width, page.Height), XStringFormat.TopCenter);
String StrConnect = ConfigurationManager.ConnectionStrings["supportConnectionString"].ConnectionString;
SqlConnection Connection = new SqlConnection(StrConnect);
Connection.Open();
SqlCommand command = new SqlCommand("SELECT titre_fr, commentaire_fr, date_publication FROM articles INNER JOIN titre_traduction ON titre_traduction.id_titre=articles.id_titre_ INNER JOIN commentaire_traduction ON commentaire_traduction.id_commentaire=articles.id_commentaire_ WHERE type_fiche=1 AND id_produits_=@id_art", Connection);
command.Parameters.Add("@id_art", SqlDbType.Int, 10);
command.Parameters["@id_art"].Value = Request.QueryString["produit_id"];
//on créé le reader pour parcourir le résultat de la requête précédente
SqlDataReader read = command.ExecuteReader();
int y = 120;
int y2 = 150;
while (read.Read())
{
string titre = (string)read["titre_fr"];
string comment = (string)read["commentaire_fr"];
DateTime date_pub = (DateTime)read["date_publication"];
string result1 = String.Format(titre);
string result2 = String.Format("Mise en ligne le : "+date_pub);
string result3 = String.Format(comment);
gfx.DrawString(result1, fontTitre, XBrushes.DarkSlateGray, new XRect(20, y, page.Width, page.Height), XStringFormat.TopLeft);
gfx.DrawString(result2, font, XBrushes.Black, new XRect(20, 140, page.Width, page.Height), XStringFormat.TopLeft);
y += 40;
y2 += 40;
// You always need a MigraDoc document for rendering.
Document doc = new Document();
Section sec = doc.AddSection();
// Add a single paragraph with some text and format information.
Paragraph para = sec.AddParagraph();
para.Format.Alignment = ParagraphAlignment.Left;
para.Format.Font.Name = "Trebuchet MS";
para.Format.Font.Size = 10;
para.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.SlateGray;
para.AddText(comment);
para.Format.Borders.Distance = "2pt";
para.Format.Borders.Color = Colors.DarkGray;
// Create a renderer and prepare (=layout) the document
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
// Render the paragraph. You can render tables or shapes the same way.
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(1), XUnit.FromCentimeter(6), "19cm", para);
}
// Fermeture connection
Connection.Close();
// Draw the text
gfx.DrawString("Généré le : " + time.ToString(), font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormat.BottomCenter);
//RenderClock(gfx);
// Send PDF to browser
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}