Как я могу переместить прямоугольник на странице PDF, не дублируя каждую информацию на странице, используя c # и iTextSharp - PullRequest
0 голосов
/ 06 ноября 2019

Я уже видел решение переместить содержимое страницы в другое сообщение. См .: itext или itextsharp - переместить текст в существующий PDF

Возможно ли это сделать без дублирования всего содержимого страницы?

a. Выделите только содержимое прямоугольника для перемещения и скопируйте только это.

b. Есть ли возможность создать ссылку на содержимое страницы и вставить ее на шаге 2 для перемещенного прямоугольника (PdfCopy).

Я думаю, что упомянутое решение добавит страницу дважды.

1: все, но прямоугольник (EoClip)

2: ТОЛЬКО прямоугольник (клип)

        /// <summary>
        /// Edit the position of content in all pages in a document
        /// </summary>
        /// <param name="file">Path of the file to be edited</param>
        /// <param name="saveAs">Path of the file to be saved</param>
        /// <param name="x">Top left corner - X</param>
        /// <param name="y">Top left corner - Y/param>
        /// <param name="w">Rectangle width</param>
        /// <param name="h">Rectangle height</param>
        /// <param name="moveX">Move X (+ right, - left)</param>
        /// <param name="moveY">Move Y (+ down, - up)</param>
        [Description("Edit the scale of all pages in a document")]
        [CommandLineVisible(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Order("EditPosition.00")]
        public static void EditPosition(
            [Description("Path of the file to be edited")]
            [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
            string file,
            [Description("Path of the file to be saved")]
            [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
            string saveAs,
            [Description("Top left corner - X")]
            float x,
            [Description("Top left corner - Y")]
            float y,
            [Description("Rectangle width")]
            float w,
            [Description("Rectangle height")]
            float h,
            [Description("Move X (+ right, - left)")]
            float moveX,
            [Description("Move Y (+ down, - up)")]
            float moveY
            )
        {
            file = prepareInput(file);
            FileInfo input = new FileInfo(file);
            writeInfoLine(String.Format("Datei \"{0}\"", file));
            FileInfo tmpPDF = new FileInfo(System.IO.Path.GetTempFileName());
            if (saveAs == String.Empty || saveAs == null)
            {
                saveAs = input.FullName;
            }

            using (var reader = new PdfReader(new MemoryStream(File.ReadAllBytes(input.FullName))))
            using (var document = new Document(reader.GetPageSize(1)))
            using (var ms = new MemoryStream())
            using (var writer = PdfWriter.GetInstance(document, ms))
            {
                document.Open();
                PdfContentByte cb = writer.DirectContent;

                // Rectangle umrechnen; PDF Orientierung von unten links
                RectangleF area = new RectangleF(x, y, w, h);

                for (int p = 1; p <= reader.NumberOfPages; p++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, p);
                    iTextSharp.text.Rectangle pageSize = reader.GetPageSize(p);


                    document.SetPageSize(pageSize);
                    document.NewPage();

                    PdfTemplate template1 = cb.CreateTemplate(pageSize.Width, pageSize.Height);
                    template1.Rectangle(0, 0, pageSize.Width, pageSize.Height);
                    template1.Rectangle(area.Left, pageSize.Height - area.Bottom,
                            area.Width, area.Height);
                    template1.EoClip(); // ADD 1
                    template1.NewPath();
                    template1.AddTemplate(page, 0, 0);
                    PdfTemplate template2 = cb.CreateTemplate(pageSize.Width, pageSize.Height);
                    template2.Rectangle(area.Left, pageSize.Height - area.Bottom,
                            area.Width, area.Height);
                    template2.Clip(); // ADD 2
                    template2.NewPath();
                    template2.AddTemplate(page, 0, 0);
                    cb.AddTemplate(template1, 0, 0);
                    cb.AddTemplate(template2, moveX, -moveY);

                }
                document.Close();
                File.WriteAllBytes(tmpPDF.FullName, ms.GetBuffer());
            }

            // Move temp.pdf to destination
            lastpath = saveOutput(saveAs, tmpPDF, input, null);
            return;
        }

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...