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 2.0.
To import content from one PDF document to another using FormXObject:
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(); } |
For more information about implementation of Form XObject feature using DsPdf, see DsPdf sample browser.