Thanks Michael. It works perfectly. I'm posting the C# port for anyone else who has this problem.
class Program { static void Main(string[] args) { Program p = new Program(); p.FixPdfWithAllResourcesOnFirstPage(args); } public void FixPdfWithAllResourcesOnFirstPage(string[] args) { string inputFile = Path.GetFullPath(args[0]); string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.pdf"); PdfReader reader = new PdfReader(inputFile); NopListener nopListener = new NopListener(); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(nopListener); Do doOp = new Do(); processor.RegisterContentOperator("Do", doOp); for (int page = 1; page < reader.NumberOfPages; page++) { PdfDictionary resources = reader.GetPageResources(page); if (resources == null) { Console.WriteLine("Page {0} has no resources", page); continue; } doOp.xObjectNames.Clear(); processor.ProcessContent(ContentByteUtils.GetContentBytesForPage(reader, page), resources); Console.WriteLine("Page {0} : Names {1}", page, string.Join(",", doOp.xObjectNames)); PdfDictionary newResources = new PdfDictionary(); newResources.PutAll(resources); PdfDictionary xObjects = newResources.GetAsDict(PdfName.XOBJECT); PdfDictionary newxObjects = new PdfDictionary(); foreach (PdfName key in doOp.xObjectNames) { newxObjects.Put(key, xObjects.Get(key)); } newResources.Put(PdfName.XOBJECT, newxObjects); reader.GetPageN(page).Put(PdfName.RESOURCES, newResources); } reader.RemoveUnusedObjects(); PdfStamper stamper = new PdfStamper(reader, new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)); stamper.Close(); } } public class Do : IContentOperator { public List<PdfName> xObjectNames = new List<PdfName>(); public void Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List<PdfObject> operands) { PdfName xObjectName = (PdfName)operands[0]; xObjectNames.Add(xObjectName); } } public class NopListener : IRenderListener { public void RenderText(TextRenderInfo renderInfo) { } public void RenderImage(ImageRenderInfo renderInfo) { } public void EndTextBlock() { } public void BeginTextBlock() { } }; Darren On Friday, December 5, 2014 8:14 AM, mkl <m...@wir-sind-cool.org> wrote: Darren, FDnC Red wrote > This is a work in progress but I'm trying to follow the advice from > Michael posted here > http://stackoverflow.com/questions/15566896/itextsharp-splitted-pages-size-equals-file-size > but it's too vague for me so I have a question. In that SO answer I already mentioned that the fact that iText does not provide the image name to the render listener makes the situation a bit difficult. To get around these difficulties I now would propose to intervene before the name is lost by using a different ContentOperator for "Do". I made a small proof of concept. It's in Java for iText but should be easily translatable to C# for iTextSharp: public void testSeparateResources() throws IOException, DocumentException { InputStream resourceStream = getClass().getResourceAsStream("255_92226_specs.pdf"); PdfReader reader = new PdfReader(resourceStream); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(nopListener); Do doOp = new Do(); processor.registerContentOperator("Do", doOp); for (int page = 1; page <= reader.getNumberOfPages(); page++) { PdfDictionary resources = reader.getPageResources(page); if (resources == null) { System.out.printf("!!! page %d has no resources\n", page); continue; } doOp.names.clear(); processor.processContent(ContentByteUtils.getContentBytesForPage(reader, page), resources); System.out.println("page " + page + ": " + doOp.names); PdfDictionary newResources = new PdfDictionary(); newResources.putAll(resources); PdfDictionary xobjects = newResources.getAsDict(PdfName.XOBJECT); PdfDictionary newXobjects = new PdfDictionary(); for (PdfName key: doOp.names) { newXobjects.put(key, xobjects.get(key)); } newResources.put(PdfName.XOBJECT, newXobjects); reader.getPageN(page).put(PdfName.RESOURCES, newResources); } reader.removeUnusedObjects(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target/test-outputs/255_92226_specs-pageSeparation.pdf")); stamper.close(); } static class Do implements ContentOperator { public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) throws IOException { PdfName xobjectName = (PdfName)operands.get(0); names.add(xobjectName); } final List<PdfName> names = new ArrayList<PdfName>(); } final static RenderListener nopListener = new RenderListener() { @Override public void renderText(TextRenderInfo renderInfo) { } @Override public void renderImage(ImageRenderInfo renderInfo) { } @Override public void endTextBlock() { } @Override public void beginTextBlock() { } }; As you see I do not care about the information given to the RenderListener at all anymore. Instead I use a simple content operator for "Do" to collect the names of used xobjects. I hope this helps. Regards, Michael -- View this message in context: http://itext-general.2136553.n4.nabble.com/Resources-All-on-First-Page-tp4660563p4660597.html Sent from the iText - General mailing list archive at Nabble.com. ------------------------------------------------------------------------------ Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration & more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php ------------------------------------------------------------------------------ Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration & more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php