PDF generation - add text and graphic to empty page

I’ve renamed the action to ‘Pdf’ from ‘PdfPig’, I couldn’t bear it any more. :grinning:

Code screenshot:


I hope the IntelliSense shot one image down will give a reasonable idea of what to do. I decided to leave the text ‘Y’ position as a positive double, subtracting it in code to give the negative offset required.

Experiment with ‘TextX’, ‘TextY’, ‘GraphicLeft’, ‘GrahicRight’, and ‘GraphicHeight’. The default values have the results shown directly below.

Code for the class:

using Robin.Core;
using Robin.Core.Attributes;
using System.ComponentModel;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Writer;
using UglyToad.PdfPig.Core;
using System.IO;
using System;
using System.Linq;

namespace Modules.PdfPig
    [Action(Order = 1)]
    [Throws("ActionError")] // TODO: change error name (or delete if not needed)
    public class GeneratePDFA2A : ActionBase
        #region Properties

        // NOTE: You can find sample description and friendly name entries in Resources

        [InputArgument, DefaultValue("Robin adds this text above the graphic.")]
        public string InputText { get; set; }

        [InputArgument, DefaultValue(@"C:\Windows\Fonts\tahoma.ttf")]
        public string FontPath { get; set; }

        public string ImagePath { get; set; }

        [InputArgument, DefaultValue(20)]
        public double TextX { get; set; }

        [InputArgument, DefaultValue(25)]
        public double TextY { get; set; }

        [InputArgument, DefaultValue(50)]
        public double GraphicLeft { get; set; }

        [InputArgument, DefaultValue(125)]
        public double GraphicRight { get; set; }

        [InputArgument, DefaultValue(40)]
        public double GraphicHeight { get; set; }

        public string FileOut { get; set; }

        public string Result { get; set; }


        #region Methods Overrides

        public override void Execute(ActionContext context)
                PdfDocumentBuilder pdfDocumentBuilder = new PdfDocumentBuilder();
                pdfDocumentBuilder.ArchiveStandard = PdfAStandard.A2A;
                PdfDocumentBuilder.AddedFont font = pdfDocumentBuilder.AddTrueTypeFont(File.ReadAllBytes(FontPath));
                PdfPageBuilder pdfPageBuilder = pdfDocumentBuilder.AddPage(PageSize.Letter, true);
                PdfPoint pdfPoint = new PdfPoint(0.0, pdfPageBuilder.PageSize.Top);
                double num = pdfPageBuilder.AddText(InputText, 12m, pdfPoint.Translate(TextX, -TextY), font).Min((Letter x) => x.GlyphRectangle.Bottom);
                PdfRectangle placementRectangle = new PdfRectangle(new PdfPoint(GraphicLeft, num - GraphicHeight), new PdfPoint(GraphicRight, num));
                pdfPageBuilder.AddJpeg(File.ReadAllBytes(ImagePath), placementRectangle);
                byte[] bytes = pdfDocumentBuilder.Build();
                    File.WriteAllBytes(FileOut, bytes);
                    Result = ("File output to: " + FileOut);
                catch (Exception arg)
                    Console.WriteLine(string.Format("Failed to write output to file due to error: {0}.", arg));
            catch (Exception e)
                if (e is ActionException) throw;

                throw new ActionException("ActionError", e.Message, e.InnerException);

            // TODO: set values to Output Arguments here