PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 2:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Apr 18, 2017 6:50 pm 
Offline

Joined: Tue Apr 18, 2017 6:31 pm
Posts: 4
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());
            }



       

        }


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 18, 2017 6:54 pm 
Offline

Joined: Tue Apr 18, 2017 6:31 pm
Posts: 4
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());
            }



       

        }


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 19, 2017 8:40 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Hi!

Your use MigraDoc in a way that will work only when the paragraphs fit on a single page. All you can get from the page fields is "1 / 1", so while it would be nice if the page fields would work, it would not give you more functionality.

Use RenderPage instead of RenderObject when there can be more than one page - and page fields work when RenderPage is used properly.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 145 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group