#--------------------------------------------------------------------------------------- # Copyright (c) 2001-2011 by PDFTron Systems Inc. All Rights Reserved. # Consult legal.txt regarding legal and license information. #--------------------------------------------------------------------------------------- require '../../../Lib/PDFNetRuby' include PDFNetRuby #--------------------------------------------------------------------------------------- # The sample code shows how to edit the page display list and how to modify graphics state # attributes on existing Elements. In particular the sample program strips all images from # the page and changes text color to blue. #--------------------------------------------------------------------------------------- def ProcessElements(reader, writer) element = reader.Next() # Read page contents while !element.nil? do type = element.GetType() case type when Element::E_image # remove all images by skipping them when Element::E_inline_image # remove all images by skipping them when Element::E_text # Process text strings... # Set all text to blue color. gs = element.GetGState() gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB()) cp = ColorPt.new(0, 0, 1) gs.SetFillColor(cp) writer.WriteElement(element) when Element::E_form # Recursively process form XObjects reader.FormBegin() ProcessElements(reader, writer) reader.End() else writer.WriteElement(element) end element = reader.Next() end end PDFNet.Initialize() # Relative path to the folder containing the test files. input_path = "../../TestFiles/" output_path = "../../TestFiles/Output/" input_filename = "newsletter.pdf" output_filename = "newsletter_edited.pdf" puts "-------------------------------------------------" # Open the test file puts "Opening the input file..." doc = PDFDoc.new(input_path + input_filename) doc.InitSecurityHandler() writer = ElementWriter.new() reader = ElementReader.new() itr = doc.GetPageIterator() while itr.HasNext() do page = itr.Current() reader.Begin(page) writer.Begin(page, ElementWriter::E_replacement, false) ProcessElements(reader, writer) writer.End() reader.End() itr.Next() end doc.Save(output_path + output_filename, SDFDoc::E_remove_unused) doc.Close() puts "Done. Result saved in " + output_filename + "..."