Documents for PDF .NET Edition
Features / Form XObjects
In This Topic
    Form XObjects
    In This Topic

    Form XObject is a technique for representing objects, such as text, graphics, page and images within a PDF document. The purpose of Form XObject is specifically for recurrent objects that gets stored once in a PDF document but can be referenced multiple times, either on several pages or at several locations on the same page and produces the same result each time. Hence, one of the common use cases of Form XObjects could be to import the content of existing PDF document to another. For more information on Form XObject, see PDF specification 1.7

    To import content from one PDF document to another using FormXObject:

    1. Initialize two instances of GcPdfDocument class, one to load the existing PDF file from which the content is to be imported and the other one is the target PDF to which the imported content is to be rendered.
    2. Create a list of the FormXObject class using the pages from the loaded PDF document.
    3. Loop through the FormXObject list to add pages to the target PDF document and render the corresponding FormXObject to each page using the DrawForm method of the GcPdfGraphics class.
      C#
      Copy Code
      static void Main(string[] args)
      {
          //Create a temporary pdf document to load an existing document
          GcPdfDocument tempDoc = new GcPdfDocument();
          FileStream fs = new FileStream(("SlidePages.pdf"), FileMode.Open, FileAccess.Read);
          tempDoc.Load(fs);
      
          // Create a new pdf document
          GcPdfDocument mainDoc = new GcPdfDocument();
          Page p;
          GcPdfGraphics g;
      
          TextFormat tf = new TextFormat()
          {
              Font = StandardFonts.HelveticaBold,
              FontSize = 16,
              ForeColor = Color.FromArgb(128, Color.Red),
          };
      
          // Create a list of FormXObject for the pages of the loaded PDF:
          var fxos = new List<FormXObject>();
          tempDoc.Pages.ToList().ForEach(p_ => fxos.Add(new FormXObject(mainDoc, p_)));
          for (int i = 0; i < fxos.Count; ++i)
          {
              p = mainDoc.NewPage();
              g = p.Graphics;
      
              var rcfx = new RectangleF(10, 50, 500, 600);
              // Draw on the main document through FormX object
              g.DrawForm(fxos[i], rcfx, null, ImageAlign.ScaleImage);
              g.DrawRectangle(rcfx, Color.Red);
              g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, false);
          }
          mainDoc.Save("FormXResult.pdf");
      
          Console.WriteLine("Press any key to exit.");
          Console.ReadKey();
      }
      
    Back to Top

    For more information about implementation of Form XObject feature using GcPdf, see GcPdf sample browser.