0

I have a process that receives unlayered and layered PDF files. For the unlayered PDF files, I'll add a layer named "cut". For the layered PDF files, I need to check to see if there is already a layer named "cut" and if so, do not add the "cut" layer. Using ABCPDF 8, how can I get the names of all the layers in a PDF to determine if there is a layer named "cut"?

rsine
  • 91
  • 1
  • 7

3 Answers3

2

I found iTextSharp has an easy way to get the names of the layers. Here is a snippet of code on how to do it:

tempOutputFile = System.IO.Path.GetTempFileName();
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfFile);

iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(tempOutputFile, System.IO.FileMode.Create));

System.Collections.Generic.Dictionary<string, iTextSharp.text.pdf.PdfLayer> layers = pdfStamper.GetPdfLayers();

pdfStamper.Close();
pdfReader.Close();

System.IO.File.Delete(tempOutputFile);

The key for the layers dictionary is the name of the layer. Easy as that!

Alexandru Severin
  • 6,021
  • 11
  • 48
  • 71
rsine
  • 91
  • 1
  • 7
0

ABCpdf Version 10 contains a project called OCGLayers which shows you how to do this.

For example to get all the named layers you would use code of the following form:

        Page page = ... get a page ...
        List<Group> groups = oc.GetGroups(page);
        List<int> indents = new List<int>();
        oc.SortGroupsForPresentation(groups, indents);
        for (int i = 0; i < groups.Count; i++) {
            Group group = groups[i];
            string indent = new string(' ', indents[i] * 3);
            layersCheckedListBox.Items.Add(indent + group.EntryName.Text, group.Visible);
        }

The project also contains code showing how to redact layers. This may be useful given your described task.

0
 public Dictionary<String, PdfLayer> GetPdfLayerNames()
        {
            PdfReader reader1 = new PdfReader("D:\\pdf\\ClaimOut4e0907cbdb6845549458e82900db7be0.pdf");
            PdfStamper stamper1 = new PdfStamper(reader1, new FileStream("D:\\new_stamper.pdf", FileMode.Append));
            Dictionary<String, PdfLayer> layers = stamper1.GetPdfLayers();
            stamper1.Close();
            reader1.Close();
            return layers;          
        }

Using this you can get names of all layers from Pdf Where sting in dictionary is name of Layer within pdf

Mahesh
  • 567
  • 2
  • 14