locked
Adding a PDF as a new layer with itextsharp RRS feed

  • Question

  • User-1526362045 posted

    Hi,

    I'm working on a solution for a printer, who prints the same brochues for many customers all based on the same master template.  He wants to automate the proof-creation process, by allowing the customers to submit their artwork as a 4-page PDF, and the solution  would automatically insert each page as a new layer on top of the original "master" PDF page, together producing a representation of what the final piece will look like.

    Our solution is built on asp.net (vb).

    His current solution accomplishes this by working with InDesign Scripting.  An instance of INDD is opened.  Both files are brought in page by page as a separate layers, and then the result is printed to a new PDF.   This is very inefficient for many reasons.

    I'm sure there is a way to accomplish this with some PDF manipulation functions.   iTextsharp seems pretty powerful; but with lack of documentation I have no clue if this is even possible.

    Can anyone provide me with an example of how this can be done? Or, if it can't, can any of the other PDF api out there accomplish this?


    Thanks in advance for your help!


    Sunday, July 25, 2010 1:36 AM

Answers

  • User-861155893 posted

    oddly been working on some pdf gen using iTextSharp


    Here is an aspx that outputs the result as byte[] to Response stream


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.IO;
    using System.Text;
    
    public partial class tst : CartBasePage
    {
        byte[] contents;
        PdfGenerator gen;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
    
                gen = new PdfGenerator(Request.PhysicalApplicationPath + "/icst/pdf/test.pdf");
                Document doc = gen.CreateDocument();
    
                PdfWriter writer = gen.PdfDocumentWriter;
    
                gen.DocumentEvents.OnStartPageEventHandler.Add(new PdfDocumentEvents.OnStartPageDelegate(PageStartEventHandler));
                gen.DocumentEvents.OnEndPageEventHandler.Add(new PdfDocumentEvents.OnEndPageDelegate(PageEndEventHandler));
    
                gen.AddPdfTemplate(1);//YOU WANT TO LOOK AT THIS
    
                doc.Open();
                doc.AddBlankLine(1);
                Paragraph p = doc.AddParagraph("This is a new Paragraph!!!");
                
                //doc.NewPage();//next thing will be moved to new page
    
                doc.AddChapter(1, "This is a start chapter");
                doc.AddParagraph("This is something new to add");
                doc.AddParagraph("This is something else should be on same page");
                doc.AddTitle("My Invoice");
                
                contents = gen.GetCompleteDocument();
            }
            catch (Exception ex)
            {
                //
            }
            Response.ContentType = "Application/pdf";
            //Response.AddHeader("Content-Disposition", "attachment; filename=\"test.pdf\""); // if you want it as a download
            Response.BinaryWrite(contents);
            Response.End();
            //ph.SetRenderMethodDelegate(RenderPdf);
        }
    
        protected void PageStartEventHandler(PdfWriter writer, Document document)
        {
            //string file = Request.GetApplicationHost() + VirtualPathUtility.ToAbsolute("~/css/screen/images/add-icon.png");
            //Paragraph im = new Paragraph();
            //im.Add(iTextSharp.text.Image.GetInstance(new Uri(file)));
            //document.Add(im);
        }
    
        protected void PageEndEventHandler(PdfWriter writer, Document document)
        {
            //gen.PdfCBytes.SetFontAndSize(BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 20.0f);
            //gen.PdfCBytes.SetRGBColorFill(33, 33, 33);
            //gen.PdfCBytes.SetRGBColorStroke(33, 33, 33);
            //gen.PdfCBytes.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Page " + document.PageNumber, 50.0f, 50.0f, 0.0f);
            //gen.PdfCBytes.Stroke();
        }
    }

    Extension Methods


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using System.IO;
    using iTextSharp.text.xml.simpleparser;
    
    /// <summary>
    /// Summary description for PdfGeneratorExtensionMethods
    /// </summary>
    public static class PdfGeneratorExtensionMethods
    {
    
        public static Paragraph AddParagraph(this Document document, string Text){
            Paragraph p = new Paragraph();
            p.Add(new Paragraph(Text));
            document.Add(p);
            return p;
        }
    
        public static Paragraph AddBlankLine(this Paragraph paragraph, int NumberOfLines)
        {
            for (int i = 0; i < NumberOfLines; i++)
            {
                paragraph.Add(new Paragraph(" "));
            }
    
            return paragraph;
        }
    
        public static Paragraph AddBlankLine(this Document document, int NumberOfLines)
        {
            Paragraph paragraph = new Paragraph();
    
            for (int i = 0; i < NumberOfLines; i++)
            {
                paragraph.Add(new Paragraph(" "));
            }
    
            document.Add(paragraph);
    
            return paragraph;
        }
    
        public static Chapter AddChapter(this Document document, int ChapterNumber, string ChapterAnchorTitle)
        {
            Anchor anchor = new Anchor(ChapterAnchorTitle);
            anchor.Name = ChapterAnchorTitle;
    
            Chapter chapter = new Chapter(new Paragraph(anchor), ChapterNumber);
    
            document.Add(chapter);
    
            return chapter;
        }
    
        public static Section AddSection(this Document document, Chapter chapter)
        {
            Section section = document.AddSection(chapter);
            return section;
        }
    
        public static Chunk AddChunk(this Document document, string Content)
        {
            Chunk NewChunk = new Chunk(Content);
            document.Add(NewChunk);
            return NewChunk;
        }
    }


    Wrapper



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using iTextSharp.text.pdf;
    using System.IO;
    using iTextSharp.text;
    
    
    /// <summary>
    /// Summary description for PdfGenerator
    /// </summary>
    public class PdfGenerator
    {
    
        /// <summary>
        /// Allow single document to be loaded containing all the template pages
        /// </summary>
        public int TemplateFilePageNumber { get; set; }
        public string TemplateFileLocation { get; set; }
    
        private PdfReader Reader { get; set; }
        private Document CurrentDocument { get; set; }
        private MemoryStream OutputStream { get; set; }
        public PdfWriter PdfDocumentWriter { get; set; }
        private PdfImportedPage PdfTemplatePage { get; set; }
        private PdfContentByte _PdfCBytes;
        public PdfContentByte PdfCBytes { get { if (_PdfCBytes == null) { _PdfCBytes = PdfDocumentWriter.DirectContent; return _PdfCBytes; } else return _PdfCBytes; } set { _PdfCBytes = value; } }
        public PdfDocumentEvents DocumentEvents { get; set; }
    
        public PdfGenerator()
        {
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateFile"></param>
        public PdfGenerator(String TemplateFileLocation)
        {
            this.TemplateFileLocation = TemplateFileLocation;
            Reader = new PdfReader(TemplateFileLocation);
        }
    
        /// <summary>
        /// Add a template page (i.e. one with the company logo etc) to the page at position 0:0
        /// </summary>
        /// <param name="TemplateDocumentPageNumber"></param>
        public void AddPdfTemplate(int TemplateDocumentPageNumber)
        {
            if (Reader != null)
            {
                if (CurrentDocument == null)
                {
                    CreateDocument();
                }
                PdfTemplatePage = PdfDocumentWriter.GetImportedPage(Reader, TemplateDocumentPageNumber);
    
                if (!CurrentDocument.IsOpen())
                {
                    CurrentDocument.Open();
                }
                else
                {
                    //this may or may not be a problem we shall see if we need to allow calling the .open method directly
                    throw new NotSupportedException("The Document appears to already be open...");
                }
    
                PdfCBytes = PdfDocumentWriter.DirectContent;
                PdfCBytes.AddTemplate(PdfTemplatePage, 0.0f, 0.0f);
            }
            else
            {
                throw new NotSupportedException("The reader was null");
            }
        }
    
        /// <summary>
        /// Overload to allow absolute poistioning of the template on the page
        /// </summary>
        /// <param name="TemplateDocumentPageNumber"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void AddPdfTemplate(int TemplateDocumentPageNumber, float x, float y)
        {
            if (Reader != null)
            {
                if (CurrentDocument == null)
                {
                    CreateDocument();
                }
                PdfTemplatePage = PdfDocumentWriter.GetImportedPage(Reader, TemplateDocumentPageNumber);
    
                if (!CurrentDocument.IsOpen())
                {
                    CurrentDocument.Open();
                }
                else
                {
                    //this may or may not be a problem we shall see if we need to allow calling the .open method directly
                    throw new NotSupportedException("The Document appears to already be open...");
                }
    
                PdfCBytes = PdfDocumentWriter.DirectContent;
                PdfCBytes.AddTemplate(PdfTemplatePage, x, y);
            }
            else
            {
                throw new NotSupportedException("The reader was null");
            }
        }
    
        public Document CreateDocument()
        {
            CurrentDocument = new Document();
            OutputStream = new MemoryStream();
            PdfDocumentWriter = PdfWriter.GetInstance(CurrentDocument, OutputStream);
            if (DocumentEvents == null) DocumentEvents = new PdfDocumentEvents();
            PdfDocumentWriter.PageEvent = DocumentEvents;
            return CurrentDocument;
        }
    
        public byte[] GetCompleteDocument()
        {
            if (CurrentDocument.IsOpen())
            {
                CurrentDocument.Close();
            }
            return OutputStream.ToArray();
        }
    }
    
    /// <summary>
    /// EventsClass for PdfWriter. The methods in this class are called on specific events such as PageEnd etc.
    /// 
    /// To hook these events outside of the generator pass in a matching delegate method to the Lists of eventhandlers. they will be called in the order you assign them.
    /// </summary>
    public class PdfDocumentEvents : PdfPageEventHelper
    {
        public delegate void OnParagraphDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnStartPageDelegate(PdfWriter writer, Document document);
        public delegate void OnSectionDelegate(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title);
        public delegate void OnChapterDelegate(PdfWriter writer, Document document, float paragraphPosition, Paragraph title);
    
        public delegate void OnParagraphEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnEndPageDelegate(PdfWriter writer, Document document);
        public delegate void OnSectionEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnChapterEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
    
        public List<OnParagraphDelegate> OnParagraphEventHandler { get; set; }
        public List<OnParagraphEndDelegate> OnParagraphEndEventHandler { get; set; }
        public List<OnStartPageDelegate> OnStartPageEventHandler { get; set; }
        public List<OnEndPageDelegate> OnEndPageEventHandler { get; set; }
        public List<OnSectionDelegate> OnSectionEventHandler { get; set; }
        public List<OnSectionEndDelegate> OnSectionEndEventHandler { get; set; }
        public List<OnChapterDelegate> OnChapterEventHandler { get; set; }
        public List<OnChapterEndDelegate> OnChapterEndEventHandler { get; set; }
    
        public PdfDocumentEvents()
        {
            if (OnParagraphEventHandler == null) OnParagraphEventHandler = new List<OnParagraphDelegate>();
            if (OnParagraphEndEventHandler == null) OnParagraphEndEventHandler = new List<OnParagraphEndDelegate>();
            if (OnStartPageEventHandler == null) OnStartPageEventHandler = new List<OnStartPageDelegate>();
            if (OnEndPageEventHandler == null) OnEndPageEventHandler = new List<OnEndPageDelegate>();
            if (OnSectionEventHandler == null) OnSectionEventHandler = new List<OnSectionDelegate>();
            if (OnSectionEndEventHandler == null) OnSectionEndEventHandler = new List<OnSectionEndDelegate>();
            if (OnChapterEventHandler == null) OnChapterEventHandler = new List<OnChapterDelegate>();
            if (OnChapterEndEventHandler == null) OnChapterEndEventHandler = new List<OnChapterEndDelegate>();
        }
    
        public override void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnParagraphDelegate del in OnParagraphEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnParagraphEndDelegate del in OnParagraphEndEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            foreach (OnStartPageDelegate del in OnStartPageEventHandler)
            {
                del.Invoke(writer, document);
            }
        }
    
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            foreach (OnEndPageDelegate del in OnEndPageEventHandler)
            {
                del.Invoke(writer, document);
            }
        }
    
        public override void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
        {
            foreach (OnSectionDelegate del in OnSectionEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition, depth, title);
            }
        }
    
        public override void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnSectionEndDelegate del in OnSectionEndEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
        {
            foreach (OnChapterDelegate del in OnChapterEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition, title);
            }
        }
    
        public override void OnChapterEnd(PdfWriter writer, Document document, float position)
        {
            foreach (OnChapterEndDelegate del in OnChapterEndEventHandler)
            {
                del.Invoke(writer, document, position);
            }
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 25, 2010 6:49 PM
  • User-861155893 posted

    <strike>Document.Compress = true; // default compression though I would not compress for a print application (bigger filesize is better)</strike>

    Corrected above. Document.Compress is a switch to turn off compression for debug puproses. Compression is enabled by default in iTextSharp and you probably have to do some heavy work to start playing with all the compression settings... this said though I highly recommend you dont compress the file as it is to be used for print

    You are correct. When importing you can check filesize and use the AddTemplate(Page, float x, float y) to offset and center


    Document.SetMargin or something... Ive not played with it too much but I should think this would do the trick.


    Can you mark answer that worked for you please also :) Thanks


    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 26, 2010 3:50 AM

All replies

  • User-861155893 posted

    oddly been working on some pdf gen using iTextSharp


    Here is an aspx that outputs the result as byte[] to Response stream


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.IO;
    using System.Text;
    
    public partial class tst : CartBasePage
    {
        byte[] contents;
        PdfGenerator gen;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
    
                gen = new PdfGenerator(Request.PhysicalApplicationPath + "/icst/pdf/test.pdf");
                Document doc = gen.CreateDocument();
    
                PdfWriter writer = gen.PdfDocumentWriter;
    
                gen.DocumentEvents.OnStartPageEventHandler.Add(new PdfDocumentEvents.OnStartPageDelegate(PageStartEventHandler));
                gen.DocumentEvents.OnEndPageEventHandler.Add(new PdfDocumentEvents.OnEndPageDelegate(PageEndEventHandler));
    
                gen.AddPdfTemplate(1);//YOU WANT TO LOOK AT THIS
    
                doc.Open();
                doc.AddBlankLine(1);
                Paragraph p = doc.AddParagraph("This is a new Paragraph!!!");
                
                //doc.NewPage();//next thing will be moved to new page
    
                doc.AddChapter(1, "This is a start chapter");
                doc.AddParagraph("This is something new to add");
                doc.AddParagraph("This is something else should be on same page");
                doc.AddTitle("My Invoice");
                
                contents = gen.GetCompleteDocument();
            }
            catch (Exception ex)
            {
                //
            }
            Response.ContentType = "Application/pdf";
            //Response.AddHeader("Content-Disposition", "attachment; filename=\"test.pdf\""); // if you want it as a download
            Response.BinaryWrite(contents);
            Response.End();
            //ph.SetRenderMethodDelegate(RenderPdf);
        }
    
        protected void PageStartEventHandler(PdfWriter writer, Document document)
        {
            //string file = Request.GetApplicationHost() + VirtualPathUtility.ToAbsolute("~/css/screen/images/add-icon.png");
            //Paragraph im = new Paragraph();
            //im.Add(iTextSharp.text.Image.GetInstance(new Uri(file)));
            //document.Add(im);
        }
    
        protected void PageEndEventHandler(PdfWriter writer, Document document)
        {
            //gen.PdfCBytes.SetFontAndSize(BaseFont.CreateFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 20.0f);
            //gen.PdfCBytes.SetRGBColorFill(33, 33, 33);
            //gen.PdfCBytes.SetRGBColorStroke(33, 33, 33);
            //gen.PdfCBytes.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Page " + document.PageNumber, 50.0f, 50.0f, 0.0f);
            //gen.PdfCBytes.Stroke();
        }
    }

    Extension Methods


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using System.IO;
    using iTextSharp.text.xml.simpleparser;
    
    /// <summary>
    /// Summary description for PdfGeneratorExtensionMethods
    /// </summary>
    public static class PdfGeneratorExtensionMethods
    {
    
        public static Paragraph AddParagraph(this Document document, string Text){
            Paragraph p = new Paragraph();
            p.Add(new Paragraph(Text));
            document.Add(p);
            return p;
        }
    
        public static Paragraph AddBlankLine(this Paragraph paragraph, int NumberOfLines)
        {
            for (int i = 0; i < NumberOfLines; i++)
            {
                paragraph.Add(new Paragraph(" "));
            }
    
            return paragraph;
        }
    
        public static Paragraph AddBlankLine(this Document document, int NumberOfLines)
        {
            Paragraph paragraph = new Paragraph();
    
            for (int i = 0; i < NumberOfLines; i++)
            {
                paragraph.Add(new Paragraph(" "));
            }
    
            document.Add(paragraph);
    
            return paragraph;
        }
    
        public static Chapter AddChapter(this Document document, int ChapterNumber, string ChapterAnchorTitle)
        {
            Anchor anchor = new Anchor(ChapterAnchorTitle);
            anchor.Name = ChapterAnchorTitle;
    
            Chapter chapter = new Chapter(new Paragraph(anchor), ChapterNumber);
    
            document.Add(chapter);
    
            return chapter;
        }
    
        public static Section AddSection(this Document document, Chapter chapter)
        {
            Section section = document.AddSection(chapter);
            return section;
        }
    
        public static Chunk AddChunk(this Document document, string Content)
        {
            Chunk NewChunk = new Chunk(Content);
            document.Add(NewChunk);
            return NewChunk;
        }
    }


    Wrapper



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using iTextSharp.text.pdf;
    using System.IO;
    using iTextSharp.text;
    
    
    /// <summary>
    /// Summary description for PdfGenerator
    /// </summary>
    public class PdfGenerator
    {
    
        /// <summary>
        /// Allow single document to be loaded containing all the template pages
        /// </summary>
        public int TemplateFilePageNumber { get; set; }
        public string TemplateFileLocation { get; set; }
    
        private PdfReader Reader { get; set; }
        private Document CurrentDocument { get; set; }
        private MemoryStream OutputStream { get; set; }
        public PdfWriter PdfDocumentWriter { get; set; }
        private PdfImportedPage PdfTemplatePage { get; set; }
        private PdfContentByte _PdfCBytes;
        public PdfContentByte PdfCBytes { get { if (_PdfCBytes == null) { _PdfCBytes = PdfDocumentWriter.DirectContent; return _PdfCBytes; } else return _PdfCBytes; } set { _PdfCBytes = value; } }
        public PdfDocumentEvents DocumentEvents { get; set; }
    
        public PdfGenerator()
        {
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateFile"></param>
        public PdfGenerator(String TemplateFileLocation)
        {
            this.TemplateFileLocation = TemplateFileLocation;
            Reader = new PdfReader(TemplateFileLocation);
        }
    
        /// <summary>
        /// Add a template page (i.e. one with the company logo etc) to the page at position 0:0
        /// </summary>
        /// <param name="TemplateDocumentPageNumber"></param>
        public void AddPdfTemplate(int TemplateDocumentPageNumber)
        {
            if (Reader != null)
            {
                if (CurrentDocument == null)
                {
                    CreateDocument();
                }
                PdfTemplatePage = PdfDocumentWriter.GetImportedPage(Reader, TemplateDocumentPageNumber);
    
                if (!CurrentDocument.IsOpen())
                {
                    CurrentDocument.Open();
                }
                else
                {
                    //this may or may not be a problem we shall see if we need to allow calling the .open method directly
                    throw new NotSupportedException("The Document appears to already be open...");
                }
    
                PdfCBytes = PdfDocumentWriter.DirectContent;
                PdfCBytes.AddTemplate(PdfTemplatePage, 0.0f, 0.0f);
            }
            else
            {
                throw new NotSupportedException("The reader was null");
            }
        }
    
        /// <summary>
        /// Overload to allow absolute poistioning of the template on the page
        /// </summary>
        /// <param name="TemplateDocumentPageNumber"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void AddPdfTemplate(int TemplateDocumentPageNumber, float x, float y)
        {
            if (Reader != null)
            {
                if (CurrentDocument == null)
                {
                    CreateDocument();
                }
                PdfTemplatePage = PdfDocumentWriter.GetImportedPage(Reader, TemplateDocumentPageNumber);
    
                if (!CurrentDocument.IsOpen())
                {
                    CurrentDocument.Open();
                }
                else
                {
                    //this may or may not be a problem we shall see if we need to allow calling the .open method directly
                    throw new NotSupportedException("The Document appears to already be open...");
                }
    
                PdfCBytes = PdfDocumentWriter.DirectContent;
                PdfCBytes.AddTemplate(PdfTemplatePage, x, y);
            }
            else
            {
                throw new NotSupportedException("The reader was null");
            }
        }
    
        public Document CreateDocument()
        {
            CurrentDocument = new Document();
            OutputStream = new MemoryStream();
            PdfDocumentWriter = PdfWriter.GetInstance(CurrentDocument, OutputStream);
            if (DocumentEvents == null) DocumentEvents = new PdfDocumentEvents();
            PdfDocumentWriter.PageEvent = DocumentEvents;
            return CurrentDocument;
        }
    
        public byte[] GetCompleteDocument()
        {
            if (CurrentDocument.IsOpen())
            {
                CurrentDocument.Close();
            }
            return OutputStream.ToArray();
        }
    }
    
    /// <summary>
    /// EventsClass for PdfWriter. The methods in this class are called on specific events such as PageEnd etc.
    /// 
    /// To hook these events outside of the generator pass in a matching delegate method to the Lists of eventhandlers. they will be called in the order you assign them.
    /// </summary>
    public class PdfDocumentEvents : PdfPageEventHelper
    {
        public delegate void OnParagraphDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnStartPageDelegate(PdfWriter writer, Document document);
        public delegate void OnSectionDelegate(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title);
        public delegate void OnChapterDelegate(PdfWriter writer, Document document, float paragraphPosition, Paragraph title);
    
        public delegate void OnParagraphEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnEndPageDelegate(PdfWriter writer, Document document);
        public delegate void OnSectionEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
        public delegate void OnChapterEndDelegate(PdfWriter writer, Document document, float paragraphPosition);
    
        public List<OnParagraphDelegate> OnParagraphEventHandler { get; set; }
        public List<OnParagraphEndDelegate> OnParagraphEndEventHandler { get; set; }
        public List<OnStartPageDelegate> OnStartPageEventHandler { get; set; }
        public List<OnEndPageDelegate> OnEndPageEventHandler { get; set; }
        public List<OnSectionDelegate> OnSectionEventHandler { get; set; }
        public List<OnSectionEndDelegate> OnSectionEndEventHandler { get; set; }
        public List<OnChapterDelegate> OnChapterEventHandler { get; set; }
        public List<OnChapterEndDelegate> OnChapterEndEventHandler { get; set; }
    
        public PdfDocumentEvents()
        {
            if (OnParagraphEventHandler == null) OnParagraphEventHandler = new List<OnParagraphDelegate>();
            if (OnParagraphEndEventHandler == null) OnParagraphEndEventHandler = new List<OnParagraphEndDelegate>();
            if (OnStartPageEventHandler == null) OnStartPageEventHandler = new List<OnStartPageDelegate>();
            if (OnEndPageEventHandler == null) OnEndPageEventHandler = new List<OnEndPageDelegate>();
            if (OnSectionEventHandler == null) OnSectionEventHandler = new List<OnSectionDelegate>();
            if (OnSectionEndEventHandler == null) OnSectionEndEventHandler = new List<OnSectionEndDelegate>();
            if (OnChapterEventHandler == null) OnChapterEventHandler = new List<OnChapterDelegate>();
            if (OnChapterEndEventHandler == null) OnChapterEndEventHandler = new List<OnChapterEndDelegate>();
        }
    
        public override void OnParagraph(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnParagraphDelegate del in OnParagraphEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnParagraphEndDelegate del in OnParagraphEndEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            foreach (OnStartPageDelegate del in OnStartPageEventHandler)
            {
                del.Invoke(writer, document);
            }
        }
    
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            foreach (OnEndPageDelegate del in OnEndPageEventHandler)
            {
                del.Invoke(writer, document);
            }
        }
    
        public override void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title)
        {
            foreach (OnSectionDelegate del in OnSectionEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition, depth, title);
            }
        }
    
        public override void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition)
        {
            foreach (OnSectionEndDelegate del in OnSectionEndEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition);
            }
        }
    
        public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
        {
            foreach (OnChapterDelegate del in OnChapterEventHandler)
            {
                del.Invoke(writer, document, paragraphPosition, title);
            }
        }
    
        public override void OnChapterEnd(PdfWriter writer, Document document, float position)
        {
            foreach (OnChapterEndDelegate del in OnChapterEndEventHandler)
            {
                del.Invoke(writer, document, position);
            }
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 25, 2010 6:49 PM
  • User-1526362045 posted

    Thanks so much.  A bit over my head but I was able to learn the concept.

    Questions:


    1. Is it possible to set the compression level when creating the new PDF? (I'm thinking higher compression
    / smaller file for proof, little compression for the press-ready copy).

    2. Various custoemrs will submit various sized PDF files (some will have bleeds, others won't). I suppose
    I'd need to check for the importing page size, and if larger than the template's page size I need to set the absolute
    positions at a negative number.  Is that correct?

    3. How does one deal with Bleeds/crop marks? Is it possible to automatically create crop marks 18p into the page
    in every direction? Or to at least have it recognized as just bleed area by Acrobat reader? I can't have them in the original
    template as they may then become covered with the customer's info.


    Thanks again for your help!

    Monday, July 26, 2010 3:29 AM
  • User-861155893 posted

    <strike>Document.Compress = true; // default compression though I would not compress for a print application (bigger filesize is better)</strike>

    Corrected above. Document.Compress is a switch to turn off compression for debug puproses. Compression is enabled by default in iTextSharp and you probably have to do some heavy work to start playing with all the compression settings... this said though I highly recommend you dont compress the file as it is to be used for print

    You are correct. When importing you can check filesize and use the AddTemplate(Page, float x, float y) to offset and center


    Document.SetMargin or something... Ive not played with it too much but I should think this would do the trick.


    Can you mark answer that worked for you please also :) Thanks


    Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 26, 2010 3:50 AM