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:

p10

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; }

        [InputArgument]
        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; }

        [InputArgument]
        public string FileOut { get; set; }

        [OutputArgument]
        public string Result { get; set; }

        #endregion

        #region Methods Overrides

        public override void Execute(ActionContext context)
        {
            try
            {
                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();
                try
                {
                    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
        }

        #endregion
    }
}

Regards,
burque505

2 Likes