Q:
1. Is it by design that Form XObjects are being flattened into the page content? We can understand why you might want to do this to protect the user from unknowingly modifying the appearance of many pages that all invoke the same Form XObject, but in our case that is the desired behaviour. Flattening the Form XObject in our case simply results in redundant data on every page that invokes the same Form XObject, increasing the total PDF file size.
a) If it is by design, should we be separately iterating through Form XObjects to edit them rather than doing it through ElementReader.FormBegin(), or have you considered adding a setting that controls whether Form XObjects get flattened vs edited in place?
b) If it is by design, can you confirm that the loss of the transformation matrix is a bug?
Please see the following sample code.
static void Main(string[] args)
{
PDFNet.Initialize();
string input = @"C:\input.pdf";
string output = @"C:\output.pdf";
using (PDFDoc pdf = new PDFDoc(input))
using (ElementBuilder elementBuilder = new ElementBuilder())
using (ElementReader elementReader = new ElementReader())
using (ElementWriter elementWriter = new ElementWriter()) {
for (PageIterator itrPage = pdf.GetPageIterator(); itrPage.HasNext(); itrPage.Next()) {
elementReader.Begin(page);
elementWriter.Begin(page, ElementWriter.WriteMode.e_replacement, false);
elementWriter.End();
elementReader.End();
}
pdf.Save(output, SDFDoc.SaveOptions.e_remove_unused);
}
}
private static void ProcessElements(ElementReader elementReader, ElementWriter elementWriter)
{
Element element;
while ((element = elementReader.Next()) != null) {
switch (element.GetType()) {
case Element.Type.e_form:
elementReader.FormBegin();
ProcessElements(elementReader, elementWriter);
elementReader.End();
break;
}
elementWriter.WriteElement(element);
}
With ElementReader/Writer you can choose whether or not you want to flatten form XObjects.
Actually you can flatten forms while preserving Forms matrix (as described here https://groups.google.com/d/msg/pdfnet-sdk/85uqFiZl_rw/S5WjKJmqBfIJ), but it is usually a better idea to preserve forms (since flattening may increase file size and may also interfere with patterns).
Attached is a version of ElementEdit sample showing how to do this.