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
[email protected]
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