投稿者 snowmansnow  (社会人) 投稿日時 2022/9/1 21:40:28

 こんばんは、検索でたどり着いた方用に
 あるPDFのアノテーションを他のPDFに平行移動する例を載せます。
 試作品なので、状況によってはエラーがでるかもしれません
 良かったら見てみてください
using System;
using System.Windows.Forms;
using iText.Kernel.Pdf;
using iText.Kernel.Colors;
using iText.Kernel.Pdf.Annot;
using Rectangle = iText.Kernel.Geom.Rectangle;

namespace WindowsFormsApp_PdfInkAnnoCS
{
    public partial class Form1Line31 : Form
    {
        public Form1Line31()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String SRC = "C:ほげほげ/平行移動の元.PDF";
            int p = 1;
            PdfDocument pdfDoc3 = new PdfDocument(new PdfReader(SRC));
            PdfPage page2 = pdfDoc3.GetPage(p);
            PdfDictionary pageDict = page2.GetPdfObject();
            PdfArray annots = pageDict.GetAsArray(PdfName.Annots);
            if (annots != null)
            {
                for (int i = 0; i < annots.Size(); i++)
                {
                    PdfDictionary annodic = annots.GetAsDictionary(i);
                    changeDict(annodic, PdfName.InkList);
                }
            }
            pdfDoc3.Close();
        }
        public void changeDict(PdfDictionary annodic2, PdfName CNAME)
        {
            if (annodic2 == null)
            {
                return;
            }
            changeInk(annodic2, annodic2.GetAsArray(CNAME));
        }
        public void changeInk(PdfDictionary annodic3, PdfArray array)
        {
            if (array == null)
            {
                return;
            }
            PdfArray annodicRect = annodic3.GetAsArray(PdfName.InkList);
            PdfArray outer = new PdfArray();
            for (int k = 0; k < annodicRect.Size(); ++k)
            {
                PdfArray inner = new PdfArray();
                PdfArray inner2 = annodicRect.GetAsArray(0);
                for (int j = 0; j < inner2.Size(); ++j)
                {
                    inner.Add(new PdfNumber((inner2.GetAsNumber(j).FloatValue() + (float)120.0)));
                }
                outer.Add(inner);
                //outer.Add(inner);
            }
            String SRC2 = "C:/ほげほげ/平行移動先.PDF";
            String DEST2 = "C:/ほげほげ/平行移動結果.PDF";
            int p2 = 1;
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC2), new PdfWriter(DEST2));
            PdfPage page = pdfDoc.GetPage(p2);
            Rectangle rect = new Rectangle(0, 0, 0, 0);
            PdfInkAnnotation inkAnnotation = new PdfInkAnnotation(rect, outer);
            // Set arrow's border style 
            PdfDictionary borderStyle = new PdfDictionary();
            borderStyle.Put(PdfName.S, PdfName.S);
            borderStyle.Put(PdfName.W, new PdfNumber(3));
            inkAnnotation.SetBorderStyle(borderStyle);
            inkAnnotation.SetColor(ColorConstants.RED);
            inkAnnotation.SetTitle(new PdfString("iText"));
            inkAnnotation.SetContents("Hi welcome");
            page.AddAnnotation(inkAnnotation);
            pdfDoc.Close();
        }
    }
}