0

I have already seen a solition to move content of a page to another postition. See: itext or itextsharp - move text in an existing PDF

Is it possible to do so without duplicating the whole content of the page?

a. Selece only the content of the rectabgle to move and copy only that.

b. Is there a possibility to create a reference to the page content und insert that in step 2 for the moved rectangle (PdfCopy).

I think the solution mentioned will add the page twice.

1: averything BUT the rectabgle (EoClip)

2: ONLY the rectangle (Clip)

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

Screem174
  • 35
  • 4
  • *"Selece only the content of the rectabgle to move and copy only that."* - This is complicated but possible. Essentially it amounts to redaction, once of everything in the rectangle, once of everything outside it, without the need to add the colored overlays. You can implement it using the `PdfCleanUp` extras (see the `iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup` namespace). – mkl Nov 11 '19 at 13:12
  • Actually, though, in the PDF file the original page contents exist only once for the page in question, in a form Xobject. Thus, if that's all you want, you already got it. Merely original page resources shared across pages in the source PDF probably are not shared anymore but duplicated. To prevent that you can switch to a `PdfStamper` based solution working inside the original PDF instead of in a new PDF. Or you port [this `OptimizeAfterMerge` code](https://stackoverflow.com/a/53544377/1729265) from PDFBox/Java to iText/.Net. – mkl Nov 11 '19 at 13:34

0 Answers0