Howdy,
The code below works but the resulting file is almost twice the size of the
original. I could figure out no other way to get the layers from the
original to be duplicated in the copy. Is there a better way?
r,
dennis      


 private void DuplicateWithLayers(string BaseFile)
        {
            MemoryStream baos = new MemoryStream();
            Document document = null;
            PdfSmartCopy pdfcopy = null;
            PdfStamper stamper = null;
            int added_page = 0;

            PdfReader pdfreader = new PdfReader(BaseFile);
            pdfreader.ConsolidateNamedDestinations();
            document = new Document(pdfreader.GetPageSizeWithRotation(1));
            pdfcopy = new PdfSmartCopy(document, baos);
            stamper = new PdfStamper(pdfreader, baos);
            stamper.Writer.PdfVersion = PdfWriter.VERSION_1_6;
            // open the document
            document.Open();
            pdfreader.SelectPages("o,e");
            int pages_num = pdfreader.NumberOfPages;
            if (pages_num > 0)
            {
                Dictionary<string, PdfLayer> Layers =
stamper.GetPdfLayers();
                for (int _i = 0; _i < pages_num; _i++)
                {
                    pdfcopy.AddPage(pdfcopy.GetImportedPage(pdfreader, (_i +
1)));
                    added_page = added_page + 1;
                    if (_i == 0)
                    {
                        PdfContentByte cbUnder = stamper.GetUnderContent(_i
+ 1);
                        foreach (KeyValuePair<string, PdfLayer> l in Layers)
                        {
                            cbUnder.BeginLayer(l.Value);
                            cbUnder.EndLayer();
                        }
                    }
                }
            }
            pdfcopy.ViewerPreferences = (PdfWriter.PageLayoutSinglePage |
PdfWriter.PageModeUseOutlines | PdfWriter.PageModeUseOC |
PdfWriter.VERSION_1_6);
            // close it ...
            stamper.Close();
            byte[] tmp_byteArray = baos.ToArray();
            FileStream fos = new FileStream("C:\\Xtest.pdf",
FileMode.OpenOrCreate, FileAccess.Write);
            fos.Write(tmp_byteArray, 0, tmp_byteArray.Length);
            fos.Flush();
            fos.Close();
            pdfreader.Close();
            baos.Dispose();
        }

--
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Duplicating-Layers-OCG-via-PdfStamper-tp4200662p4200662.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
10 Tips for Better Server Consolidation
Server virtualization is being driven by many needs.  
But none more important than the need to reduce IT complexity 
while improving strategic productivity.  Learn More! 
http://www.accelacomm.com/jaw/sdnl/114/51507609/
_______________________________________________
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

Reply via email to