Hi,
Thank you for this great open source code. I have tried using a mix between migra and pdfsharp, and it works flawlessly till i faced the "Object reference not set to instance of object" when adding AddPageField() statement, generally adding any field to footer throughs exception.
May you please have a look on my code below and advise.
Code:
public static void ExportPDF(List<string> str_imgs, string ir_Header = "Inspection Request no. 355")
{
var doc = new PdfDocument();
var p = doc.AddPage();
p.Orientation = PdfSharp.PageOrientation.Portrait;
p.TrimMargins.Left = "1cm";
p.TrimMargins.Right = "1cm";
p.TrimMargins.Top = "2cm";
p.TrimMargins.Bottom = "2cm";
var gfx = XGraphics.FromPdfPage(p);
gfx.MUH = PdfFontEncoding.Unicode;
XFont font = new XFont("Arial", 13, XFontStyle.Bold);
gfx.DrawString("This is a test string", font, XBrushes.Blue, new XPoint(XUnit.FromMillimeter(0), XUnit.FromMillimeter(14)));
for (int i = 0; i < str_imgs.Count; i++)
{
var im = XImage.FromFile(str_imgs[i]);
var ratio = im.PointWidth / im.PointHeight;
XUnit pic_Hloc = XUnit.FromMillimeter(15);
string imgcap = "Image " + (i + 1).ToString("00");
if (i > 0)
{
var tim = XImage.FromFile(str_imgs[i - 1]);
pic_Hloc += XUnit.FromMillimeter(190) / (tim.PointWidth / tim.PointHeight) + gfx.MeasureString(imgcap, font).Height + XUnit.FromMillimeter(2);
}
XUnit width = XUnit.FromMillimeter(190);
XUnit height = XUnit.FromMillimeter(190) / ratio;
gfx.DrawImage(im, p.Width / 2 - width / 2, pic_Hloc, width, height);
gfx.DrawString(imgcap, font, XBrushes.Black, new XPoint(width / 2 - gfx.MeasureString(imgcap, font).Width / 2, pic_Hloc + height + XUnit.FromMillimeter(5)));
}
Document Migdoc = new Document();
MigraDoc.DocumentObjectModel.Section sec = Migdoc.AddSection();
var head = sec.Headers.Primary;
head.Format.WidowControl = true;
sec.PageSetup.OddAndEvenPagesHeaderFooter = false;
var para = new MigraDoc.DocumentObjectModel.Paragraph();
para.Format.Alignment = ParagraphAlignment.Justify;
para.Format.Borders.HasBorder(BorderType.Bottom);
para.Format.Borders.Bottom.Width = 2;
para.Format.Borders.Bottom.Color = Colors.Black;
para.Format.Font.Size = 14;
para.Format.Font.Bold = true;
para.AddText(ir_Header);
head.Add(para);
var footf = sec.Footers.Primary.AddTextFrame();
footf.Width = "21cm";
footf.MarginBottom = 0;
footf.Orientation = MigraDoc.DocumentObjectModel.Shapes.TextOrientation.Horizontal;
footf.MarginTop = "2cm";
var foot = sec.Footers.Primary;
Migdoc.DefaultPageSetup.FooterDistance = Unit.FromMillimeter(40);
foot.Format.Borders.HasBorder(BorderType.Top);
foot.Format.Borders.Top.Width = 2;
foot.Format.Borders.Top.Color = Colors.Black;
MigraDoc.DocumentObjectModel.Paragraph Leftpara = sec.Footers.Primary.AddParagraph();
Leftpara.AddText("Page ");
Leftpara.AddPageField();
Leftpara.AddText("/");
Leftpara.AddNumPagesField();
Leftpara.Format.Font.Size = 14;
Leftpara.Format.Alignment = ParagraphAlignment.Center;
var renderer = new MigraDoc.Rendering.DocumentRenderer(Migdoc);
renderer.PrepareDocument();
renderer.RenderObject(gfx, XUnit.FromMillimeter(0), XUnit.FromMillimeter(0), p.Width, para);
renderer.RenderObject(gfx, XUnit.FromMillimeter(0), XUnit.FromMillimeter(270), p.Width, Leftpara);//<-- Exception here
string filesave = @"C:\testsample.pdf";
try
{
doc.Save(filesave);
Process.Start(filesave);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}