.NET/C# PDFsharp and MigraDoc
Just some sample code for reference, creating PDF documents on the fly.
Sample Code
Document rapport = new Document();
rapport.UseCmykColor = true;
// Logo og rapportoverskrift.
Section section = rapport.AddSection();
section.AddImage(PDFHeadLocation);
// ButikkinfoBox.
TextFrame ButikkInfo = section.AddTextFrame();
ButikkInfo.Height = "2.0cm";
ButikkInfo.Width = "8.0cm";
ButikkInfo.WrapFormat.DistanceLeft = "11.0cm";
ButikkInfo.WrapFormat.DistanceTop = "3.0cm";
ButikkInfo.RelativeVertical = RelativeVertical.Page;
ButikkInfo.RelativeHorizontal = RelativeHorizontal.Page;
//ButikkInfo.FillFormat.Color = Colors.GreenYellow;
// Just a visual aid during development to see the visual size of TextFrame.
// Butikkinfo.
paragraph = ButikkInfo.AddParagraph();
paragraph.Format.Font.Color = Color.FromCmyk(75, 68, 67, 90);
paragraph.Format.Font.Name = "Verdana";
paragraph.Format.Font.Size = 10;
paragraph.Format.SpaceAfter = 0;
paragraph.AddFormattedText("Verksted: ", TextFormat.Bold);
paragraph.AddFormattedText(EuronicsInfo["Navn"]);
paragraph.AddLineBreak();
paragraph.AddFormattedText("Adresse: ", TextFormat.Bold);
paragraph.AddFormattedText(EuronicsInfo["Addr"]);
paragraph.AddLineBreak();
paragraph.AddFormattedText("Sted: ", TextFormat.Bold);
paragraph.AddFormattedText(EuronicsInfo["Postnr"] +" "+ EuronicsInfo["Sted"]);
paragraph.AddLineBreak();
paragraph.AddFormattedText("Servicenr.: ", TextFormat.Bold);
paragraph.AddFormattedText(mainfrm.txtServicenr_gettext());
// CRITICAL style.
Style CRITICALstyle = rapport.Styles.AddStyle("CRITICALstyle", "Normal");
CRITICALstyle.Font.Color = Colors.Red;
CRITICALstyle.Font.Bold = false;
CRITICALstyle.Font.Underline = Underline.Single;
CRITICALstyle.Font.Name = "Lucida Console";
CRITICALstyle.Font.Size = 10;
// CRITICALNOTE style.
Style CRITICALstyleNOTE = rapport.Styles.AddStyle("CRITICALstyleNOTE", "Normal");
CRITICALstyleNOTE.Font.Color = Colors.Red;
CRITICALstyleNOTE.Font.Bold = false;
CRITICALstyleNOTE.Font.Underline = Underline.None;
CRITICALstyleNOTE.Font.Name = "Verdana";
CRITICALstyleNOTE.Font.Size = 10;
// OK style.
Style OKstyle = rapport.Styles.AddStyle("OKstyle", "Normal");
OKstyle.Font.Color = Color.FromCmyk(75, 68, 67, 90);
OKstyle.Font.Bold = false;
OKstyle.Font.Underline = Underline.None;
OKstyle.Font.Name = "Lucida Console";
OKstyle.Font.Size = 10;
// SMON logg.
string[] TXTfile = File.ReadAllLines(TXTFileLocation, Encoding.GetEncoding(1252));
foreach (string l in TXTfile) {
if (
l.Contains("Reallocated_Sector_Ct") && !l.Contains("- 0") ||
l.Contains("Spin_Retry_Count") && !l.Contains("- 0") ||
l.Contains("Current_Pending_Sector") && !l.Contains("- 0") ||
l.Contains("Reallocation_Event_Count") && !l.Contains("- 0") ||
l.Contains("Retired_Block_Count") && !l.Contains("- 0") ||
l.Contains("Reported_Uncorrectable_Errors") && !l.Contains("- 0")
) {
paragraph = section.AddParagraph();
paragraph.Style = "CRITICALstyle";
paragraph.AddFormattedText(l.Replace(" ", "\u00A0"));
paragraph.AddLineBreak();
paragraph = section.AddParagraph();
paragraph.Style = "CRITICALstyleNOTE";
paragraph.AddFormattedText("^ (KRITISK FEIL - NÆRLIGGENDE FARE FOR DØD HARDDISK).");
} else {
paragraph = section.AddParagraph();
paragraph.Style = "OKstyle";
paragraph.AddFormattedText(l.Replace(" ", "\u00A0"));
}
paragraph.AddLineBreak();
}
// Sluttnotat.
paragraph = section.AddParagraph();
paragraph.Format.Font.Color = Color.FromCmyk(75, 68, 67, 90);
paragraph.Format.Font.Name = "Arial";
paragraph.Format.Font.Size = 12;
paragraph.Format.SpaceAfter = 0;
paragraph.AddFormattedText("Full Service", TextFormat.Bold);
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
pdfRenderer.Document = rapport;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save(PDFSaveLocation);
pdfRenderer.PdfDocument.Close();