Hi,
I am working on parsing pdf layers from a pdf file into separate images.
It was really hard to find an library that does this and now I am digging into iTextSharp which seems like it's handling layers.
public static void CreatePDF( string fileName, string destinationFolder )
{
PdfReader reader = new PdfReader( fileName );
PdfDictionary ocProps = reader.Catalog.GetAsDict( PdfName.OCPROPERTIES );
PdfDictionary occd = ocProps.GetAsDict( PdfName.D );
PdfArray order = occd.GetAsArray( PdfName.ORDER );
List<PdfObject> layers = new List<PdfObject>();
for( int i = 0; i < order.Size; i++ )
{
layers.Add( order[ i ] );
}
for( int i = 0; i < layers.Count; i++ )
{
order.Remove( 0 );
}
for( int i = 0; i < layers.Count; i++ )
{
order.Add( layers[ i ] );
PdfStamper stamper = new PdfStamper( reader, new System.IO.FileStream( destinationFolder + "\\test" + i + ".pdf", System.IO.FileMode.CreateNew ) );
stamper.Close();
order.Remove( 0 );
}
reader.Close();
}
This is what I did for saving each layers into separate pdf files. Problem is that it creates one-layer pdf in the first loop but it has whole layers as background so it is not perfect one-layer pdf and it occurs error int he second loop which says I need to load the original pdf again.
Does anyone have any tips for separating layers into pdf or image from a multi-layer pdf?
Please help!