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.

I realize this code is incomplete but the crux of the issue is to understand 
how to 1) fully copy resources from one page to another and 2) how to delete 
all but the necessary resources and save via stamper.

This is what I have so far (code also attached).

PdfReader pdfReader = new PdfReader(inputFile); 

PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader); 
int numberOfPages = pdfReader.NumberOfPages; 
Listener listener = new Listener(); 
int i = 0; 
while (i < numberOfPages) 
{ 
i++; 
parser.ProcessContent(i, listener); 
} 

using (FileStream fs = new FileStream(outputFile, FileMode.Create, 
FileAccess.Write, FileShare.None)) 
{ 
using (PdfStamper stamper = new PdfStamper(pdfReader, fs)) 
{ 
PdfDictionary pg1res = null; 
int PageCount = pdfReader.NumberOfPages; 
for (int x = 1; x <= PageCount; x++) 
{ 
PdfDictionary dictionary = pdfReader.GetPageN(x); 
PdfDictionary res = 
(PdfDictionary)PdfReader.GetPdfObject(dictionary.Get(PdfName.RESOURCES)); 
PdfDictionary xobj = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)); 
if (x == 1) 
//The plan was to save the resources from the first page and then apply them to 
other pages but I couldn't get it to work right.
    pg1res = pdfReader.GetPageResources(x); 

foreach (PdfName name in xobj.Keys) 
{ 
PdfObject obj = xobj.Get(name); 
if (obj.IsIndirect()) 
{ 
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj); 
if (tg == null) continue; 
PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)); 
if (PdfName.IMAGE.Equals(type) && (listener.ImageNumbers[x-1] == 
((PdfIndirectReference)obj).Number)) 
{ 
//This is definitely the object I want to keep for this particular page
int xRefIndex = 
Convert.ToInt32(((PdfIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
 
//PdfObject pdfObj = pdfReader.GetPdfObject(xRefIndex); 
//PdfStream pdfStream = (PdfStream)pdfObj; 

//This mess below I'm guessing is totally wrong
PdfReader.KillIndirect(obj); 
byte[] imgBytes = listener.ImageInfo[xRefIndex]; 
Image img = Image.GetInstance(imgBytes); 
stamper.Writer.AddDirectImageSimple(img, (PRIndirectReference)obj); 
//break; 
} 
else 
{ 
//This appears to remove the resources from the dictionary but it's not saved 
via the stamper
tg.Remove(name); 
} 
} 
} 
} 
} 
} 


Thanks,
Darren
 




On Monday, November 17, 2014 8:59 AM, mkl <m...@wir-sind-cool.org> wrote:
Darren,

FDnC Red wrote

> The only way around this problem I can see is to parse the content stream
> on each page looking for the image names (/I1, /I2, /I3, etc) and make a
> PDF with only those images.
> 
> Do you see any other way to split this correctly?

Essentially that's it. You might want to make that a two step process, first
apply a stamper to the PDF and for each page replace the reference to the
global resources dictionary by a new, individual one which only links
resources of that page. In a second step you can now apply a solution of
your choice to do the actual PDF splitting.

Regards,   Michael



--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Resources-All-on-First-Page-tp4660563p4660566.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=157005751&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
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SplitWeirdPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //string inputFile = Path.GetFullPath(args[0]);
            //string outputFile = 
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
"output.pdf");

            //PdfReader pdfReader = new PdfReader(inputFile);
            
            //using (FileStream fs = new FileStream(outputFile, 
FileMode.Create, FileAccess.Write, FileShare.None))
            //{
            //    using (PdfStamper stamper = new PdfStamper(pdfReader, fs))
            //    {
            //        int PageCount = pdfReader.NumberOfPages;
            //        for (int x = 1; x <= PageCount; x++)
            //        {
            //            PdfContentByte cb = stamper.GetOverContent(x);
            //            iTextSharp.text.Rectangle rectangle = 
pdfReader.GetPageSizeWithRotation(x);
            //            rectangle.BackgroundColor = BaseColor.BLACK;
            //            cb.Rectangle(rectangle);
            //        }
            //    }
            //}

            string inputFile = Path.GetFullPath(args[0]);
            string outputFile = 
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
"output.pdf");

            PdfReader pdfReader = new PdfReader(inputFile);
            //Document pdfDocument = new 
Document(pdfReader.GetPageSizeWithRotation(1));
            //PdfCopy pdfCopy = new PdfCopy(pdfDocument,

            PdfReaderContentParser parser = new 
PdfReaderContentParser(pdfReader);
            int numberOfPages = pdfReader.NumberOfPages;
            Listener listener = new Listener();
            int i = 0;
            while (i < numberOfPages)
            {
                i++;
                parser.ProcessContent(i, listener);
            }

            using (FileStream fs = new FileStream(outputFile, FileMode.Create, 
FileAccess.Write, FileShare.None))
            {
                using (PdfStamper stamper = new PdfStamper(pdfReader, fs))
                {
                    PdfDictionary pg1res = null;
                    int PageCount = pdfReader.NumberOfPages;
                    for (int x = 1; x <= PageCount; x++)
                    {
                        PdfDictionary dictionary = pdfReader.GetPageN(x);
                        PdfDictionary res = 
(PdfDictionary)PdfReader.GetPdfObject(dictionary.Get(PdfName.RESOURCES));
                        PdfDictionary xobj = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                        if (x == 1)
                            pg1res = pdfReader.GetPageResources(x);
                        
                        foreach (PdfName name in xobj.Keys)
                        {
                            PdfObject obj = xobj.Get(name);
                            if (obj.IsIndirect())
                            {
                                PdfDictionary tg = 
(PdfDictionary)PdfReader.GetPdfObject(obj);
                                if (tg == null) continue;
                                PdfName type = 
(PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                                if (PdfName.IMAGE.Equals(type) && 
(listener.ImageNumbers[x-1] == ((PdfIndirectReference)obj).Number))
                                {
                                    int xRefIndex = 
Convert.ToInt32(((PdfIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                                    PdfObject pdfObj = 
pdfReader.GetPdfObject(xRefIndex);
                                    PdfStream pdfStream = (PdfStream)pdfObj;

                                    PdfReader.KillIndirect(obj);
                                    byte[] imgBytes = 
listener.ImageInfo[xRefIndex];
                                    Image img = Image.GetInstance(imgBytes);
                                    stamper.Writer.AddDirectImageSimple(img, 
(PRIndirectReference)obj);
                                    //break;
                                }
                                else
                                {
                                    tg.Remove(name);   
                                }
                            }
                        }
                    }
                }
            }
        }

        //public static PdfDictionary GetAllResources(PdfReader reader, int 
pagenumber)
        //{
        //    PdfDictionary resources = new PdfDictionary(PdfName.RESOURCES);
        //    PdfDictionary dictionary = reader.GetPageN(pagenumber);
        //    PdfDictionary res = 
(PdfDictionary)PdfReader.GetPdfObject(dictionary.Get(PdfName.RESOURCES));
        //    PdfDictionary fontDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.FONT));
        //    PdfDictionary xObjectDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
        //    PdfDictionary colorDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.COLOR));
        //    PdfDictionary patternDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.PATTERN));
        //    PdfDictionary shadingDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.SHADING));
        //    PdfDictionary extGStateDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.EXTGSTATE));
        //    PdfDictionary propertyDictionary = 
(PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.PROPERTIES));
        //    resources.Put(PdfName.PROCSET, new PdfLiteral("[/PDF /Text 
/ImageB /ImageC /ImageI]"));
        //    resources.Put(PdfName.FONT, fontDictionary);
        //    resources.Put(PdfName.XOBJECT, xObjectDictionary);
        //    resources.Put(PdfName.COLOR, colorDictionary);
        //    resources.Put(PdfName.PATTERN, patternDictionary);
        //    resources.Put(PdfName.SHADING, shadingDictionary);
        //    resources.Put(PdfName.EXTGSTATE, extGStateDictionary);
        //    resources.Put(PdfName.PROPERTIES, propertyDictionary);
        //    return resources;
        //}
        public static PdfDictionary SetOriginalResources(PdfDictionary 
resources, PdfDictionary origRes)
        {
            Hashtable forbiddenNames = new Hashtable();
            Hashtable usedNames = new Hashtable();
            if (resources == null)
                return (PdfDictionary)null;
            PdfDictionary originalResources = new PdfDictionary();
            //originalResources.Merge(resources);
            //PdfDictionary originalResources = origRes;

            //PdfDictionary originalResources = new 
PdfDictionary(PdfName.RESOURCES);
            //originalResources = origRes;
            //originalResources.Merge(resources);
            //originalResources.Merge(origRes);
            foreach (PdfName key in resources.Keys)
            {
                PdfObject sub = PdfReader.GetPdfObject(resources.Get(key));
                if (sub != null && sub.IsDictionary())
                {
                    PdfDictionary dic = (PdfDictionary)sub;
                    foreach (PdfName name in dic.Keys)
                    {
                        forbiddenNames[name] = null;
                    }
                    PdfDictionary dic2 = new PdfDictionary();
                    dic2.Merge(dic);
                    originalResources.Put(key, dic2);
                }
            }
            return originalResources;
        }
    }

    public class Listener : IRenderListener
    {
        public List<int> ImageNumbers = new List<int>();
        public Dictionary<int, byte[]> ImageInfo = new Dictionary<int, 
byte[]>();
        public void BeginTextBlock()
        {
        }

        public void RenderText(TextRenderInfo info)
        {
        }

        public void EndTextBlock()
        {
        }

        public void RenderImage(ImageRenderInfo renderInfo)
        {
            PdfImageObject image = renderInfo.GetImage();
            if (image == null)
                return;
            ImageNumbers.Add(renderInfo.GetRef().Number);
            ImageInfo.Add(renderInfo.GetRef().Number, 
renderInfo.GetImage().GetImageAsBytes());
        }
    }
}
------------------------------------------------------------------------------
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

Reply via email to