コード例 #1
0
        /// <summary>
        /// AppData contains the information needed to create a new PDF with overlayed information including name, description, website, and image.
        /// </summary>
        /// <param name="data">The data to use</param>
        static void addTextToPdf(AppData data)
        {
            using (PdfStamper stamper = new PdfStamper(new PdfReader(sourcePDF), File.Create(data.filename)))
            {
                // Create the header textbox
                TextField name = new TextField(stamper.Writer, new Rectangle(80, 932, 719, 1022), "Name");
                name.Text      = data.name;
                name.Alignment = Element.ALIGN_CENTER;

                // Get and set the image
                Image image;
                try
                {
                    image = Image.GetInstance(data.photo);
                } catch
                {
                    image = Image.GetInstance("../../files/default.png");
                }

                image.ScaleAbsolute(new Rectangle(80, 200, 721, 593));
                image.SetAbsolutePosition(79, 488);

                // Create the description textbox
                TextField description = new TextField(stamper.Writer, new Rectangle(80, 74, 719, 438), "Description");
                description.Options   = TextField.MULTILINE;
                description.Text      = data.description;
                description.Alignment = Element.ALIGN_CENTER;

                //calculate the font size
                float size = ColumnText.FitText(new Font(description.Font), data.description, new Rectangle(0, 0, 560, 365), 80, 0);
                description.FontSize = size;

                //Add the website URL if it exists
                TextField website = new TextField(stamper.Writer, new Rectangle(80, 30, 719, 60), "Website");
                website.Text      = data.website;
                website.Alignment = Element.ALIGN_CENTER;

                // Write and close
                stamper.AddAnnotation(name.GetTextField(), 1);
                stamper.GetOverContent(1).AddImage(image);
                stamper.AddAnnotation(description.GetTextField(), 1);
                stamper.AddAnnotation(website.GetTextField(), 1);
                stamper.Close();
            }
        }