Пример #1
0
        public void Build(ITemplate template, string outputpath)
        {
            PdfDocument pdf      = new PdfDocument(new PdfWriter(outputpath));
            Document    document = new Document(pdf);
            var         elements = template.GetElements();

            foreach (var element in elements)
            {
                if (element is AreaBreak)
                {
                    AreaBreak areaBreak = (AreaBreak)element;
                    document.Add(areaBreak);
                }
                else if (element is IBlockElement)
                {
                    IBlockElement blockElement = (IBlockElement)element;
                    document.Add(blockElement);
                }
                else if (element is Image)
                {
                    Image image = (Image)element;
                    document.Add(image);
                }
            }
            document.Flush();
            document.Close();
        }
Пример #2
0
        protected internal override LayoutArea UpdateCurrentArea(LayoutResult overflowResult)
        {
            AreaBreak areaBreak = overflowResult != null && overflowResult.GetAreaBreak() != null?overflowResult.GetAreaBreak
                                      () : null;

            MoveToNextPage();
            while (areaBreak != null && areaBreak.GetAreaType() == AreaBreakType.LAST_PAGE && currentPageNumber < document
                   .GetPdfDocument().GetNumberOfPages())
            {
                MoveToNextPage();
            }
            PageSize customPageSize = areaBreak != null?areaBreak.GetPageSize() : null;

            while (document.GetPdfDocument().GetNumberOfPages() >= currentPageNumber && document.GetPdfDocument().GetPage
                       (currentPageNumber).IsFlushed())
            {
                currentPageNumber++;
            }
            PageSize lastPageSize = EnsureDocumentHasNPages(currentPageNumber, customPageSize);

            if (lastPageSize == null)
            {
                lastPageSize = new PageSize(document.GetPdfDocument().GetPage(currentPageNumber).GetPageSize());
            }
            return(currentArea = new LayoutArea(currentPageNumber, document.GetPageEffectiveArea(lastPageSize)));
        }
Пример #3
0
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document  document = new Document(pdf);
            Paragraph p        = new Paragraph().Add("Be prepared to read a story about a London lawyer " + "named Gabriel John Utterson who investigates strange "
                                                     + "occurrences between his old friend, Dr. Henry Jekyll, " + "and the evil Edward Hyde.");

            document.Add(p);
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            //Set column parameters
            float offSet       = 36;
            float gutter       = 23;
            float columnWidth  = (PageSize.A4.GetWidth() - offSet * 2) / 2 - gutter;
            float columnHeight = PageSize.A4.GetHeight() - offSet * 2;

            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle
                                                        (offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            document.Add(new AreaBreak(AreaBreakType.LAST_PAGE));
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetFont(font).SetHyphenation(new HyphenationConfig("en"
                                                                                                                  , "uk", 3, 3));
            StreamReader sr = File.OpenText(SRC);
            String       line;
            bool         title    = true;
            AreaBreak    nextPage = new AreaBreak(AreaBreakType.NEXT_AREA);

            while ((line = sr.ReadLine()) != null)
            {
                p = new Paragraph(line);
                if (title)
                {
                    p.SetFont(bold).SetFontSize(12);
                    title = false;
                }
                else
                {
                    p.SetFirstLineIndent(36);
                }
                if (String.IsNullOrEmpty(line))
                {
                    document.Add(nextPage);
                    title = true;
                }
                document.Add(p);
            }
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            document.SetRenderer(new DocumentRenderer(document));
            document.Add(new AreaBreak(AreaBreakType.LAST_PAGE));
            p = new Paragraph().Add("This was the story about the London lawyer " + "named Gabriel John Utterson who investigates strange "
                                    + "occurrences between his old friend, Dr. Henry Jekyll, " + "and the evil Edward Hyde. THE END!");
            document.Add(p);
            //Close document
            document.Close();
        }
Пример #4
0
 /* (non-Javadoc)
 * @see com.itextpdf.layout.renderer.DocumentRenderer#updateCurrentArea(com.itextpdf.layout.layout.LayoutResult)
 */
 protected override LayoutArea UpdateCurrentArea(LayoutResult overflowResult) {
     AreaBreak areaBreak = overflowResult != null ? overflowResult.GetAreaBreak() : null;
     if (areaBreak is HtmlPageBreak) {
         HtmlPageBreakType htmlPageBreakType = ((HtmlPageBreak)areaBreak).GetBreakType();
         if (shouldTrimFirstBlankPagesCausedByBreakBeforeFirstElement && currentArea != null && overflowResult.GetStatus
             () == LayoutResult.NOTHING && currentArea.IsEmptyArea() && currentArea.GetPageNumber() == 1) {
             // Remove blank page that was added just to have area for elements to layout on.
             // Now we will add a page with dimensions and all the stuff that is requested by page-break-before
             document.GetPdfDocument().RemovePage(1);
             currentPageNumber = 0;
             overflowResult = null;
             currentArea = null;
             shouldTrimFirstBlankPagesCausedByBreakBeforeFirstElement = false;
             if (HtmlPageBreakType.LEFT.Equals(htmlPageBreakType) && !IsPageLeft(1) || HtmlPageBreakType.RIGHT.Equals(htmlPageBreakType
                 ) && !IsPageRight(1)) {
                 evenPagesAreLeft = !evenPagesAreLeft;
             }
         }
         // hack to change the "evenness" of the first page without adding an unnecessary blank page
         anythingAddedToCurrentArea = anythingAddedToCurrentArea || overflowResult != null && overflowResult.GetStatus
             () == LayoutResult.PARTIAL;
         if (HtmlPageBreakType.ALWAYS.Equals(htmlPageBreakType)) {
             LayoutArea nextArea = currentArea;
             if (anythingAddedToCurrentArea || currentArea == null) {
                 nextArea = base.UpdateCurrentArea(overflowResult);
             }
             anythingAddedToCurrentArea = false;
             return nextArea;
         }
         else {
             if (HtmlPageBreakType.LEFT.Equals(htmlPageBreakType)) {
                 LayoutArea nextArea = currentArea;
                 if (anythingAddedToCurrentArea || currentArea == null || !IsPageLeft(currentPageNumber)) {
                     do {
                         nextArea = base.UpdateCurrentArea(overflowResult);
                     }
                     while (!IsPageLeft(currentPageNumber));
                 }
                 anythingAddedToCurrentArea = false;
                 return nextArea;
             }
             else {
                 if (HtmlPageBreakType.RIGHT.Equals(htmlPageBreakType)) {
                     LayoutArea nextArea = currentArea;
                     if (anythingAddedToCurrentArea || currentArea == null || !IsPageRight(currentPageNumber)) {
                         do {
                             nextArea = base.UpdateCurrentArea(overflowResult);
                         }
                         while (!IsPageRight(currentPageNumber));
                     }
                     anythingAddedToCurrentArea = false;
                     return nextArea;
                 }
             }
         }
     }
     anythingAddedToCurrentArea = false;
     return base.UpdateCurrentArea(overflowResult);
 }
Пример #5
0
 /// <summary>Terminates the current element, usually a page.</summary>
 /// <remarks>
 /// Terminates the current element, usually a page. Sets the next element
 /// to be the size specified in the argument.
 /// </remarks>
 /// <param name="areaBreak">
 /// an
 /// <see cref="iText.Layout.Element.AreaBreak"/>
 /// , optionally with a specified size
 /// </param>
 /// <returns>this element</returns>
 public virtual iText.Layout.Document Add(AreaBreak areaBreak)
 {
     CheckClosingStatus();
     childElements.Add(areaBreak);
     EnsureRootRendererNotNull().AddChild(areaBreak.CreateRendererSubTree());
     if (immediateFlush)
     {
         childElements.JRemoveAt(childElements.Count - 1);
     }
     return(this);
 }
Пример #6
0
 protected internal override void OnBuild(PdfElementContainer pdfDoc, object model)
 {
     model = GetModel(model);
     if (!FirstPage)
     {
         var page = new AreaBreak(AreaBreakType.NEXT_PAGE);
         pdfDoc.Add(page);
     }
     // iText.Layout.Element.LineSeparator
     base.OnBuild(pdfDoc, model);
 }
Пример #7
0
        public virtual void CreatePdf(String dest)
        {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4);
            //Set column parameters
            float offSet       = 27;
            float gutter       = 18;
            float columnWidth  = (PageSize.A4.GetWidth() - offSet * 2) / 3 - gutter;
            float columnHeight = PageSize.A4.GetHeight() - offSet * 2;

            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet + gutter * 0.5f, offSet, columnWidth, columnHeight
                                                                  ), new Rectangle(offSet + columnWidth + gutter * 1.5f, offSet, columnWidth, columnHeight), new Rectangle
                                                        (offSet + columnWidth * 2 + gutter * 2.5f, offSet, columnWidth, columnHeight) };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3));
            StreamReader sr        = File.OpenText(SRC);
            bool         chapter   = false;
            Div          div       = new Div();
            AreaBreak    areaBreak = new AreaBreak(AreaBreakType.NEXT_AREA);
            String       line;

            while ((line = sr.ReadLine()) != null)
            {
                div = new Div().SetFont(font).SetFontSize(11).SetMarginBottom(18);
                div.Add(new Paragraph(line).SetFont(bold).SetFontSize(12).SetMarginBottom(0));
                while ((line = sr.ReadLine()) != null)
                {
                    div.Add(new Paragraph(line).SetMarginBottom(0).SetFirstLineIndent(36));
                    if (String.IsNullOrEmpty(line))
                    {
                        document.Add(div);
                        if (chapter)
                        {
                            document.Add(areaBreak);
                        }
                        div     = new Div();
                        chapter = true;
                        break;
                    }
                }
            }
            document.Add(div);
            //Close document
            document.Close();
        }
Пример #8
0
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            //Set column parameters
            float offSet       = 36;
            float gutter       = 23;
            float columnWidth  = (PageSize.A4.GetWidth() - offSet * 2) / 2 - gutter;
            float columnHeight = PageSize.A4.GetHeight() - offSet * 2;

            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle
                                                        (offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
            DocumentRenderer renderer = new C02E11_JekyllHydeV7.MyColumnRenderer(document, columns);

            document.SetRenderer(renderer);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetFont(font).SetHyphenation(new HyphenationConfig("en"
                                                                                                                  , "uk", 3, 3));
            StreamReader sr = File.OpenText(SRC);
            String       line;
            Paragraph    p;
            bool         title    = true;
            AreaBreak    nextPage = new AreaBreak(AreaBreakType.NEXT_PAGE);

            while ((line = sr.ReadLine()) != null)
            {
                p = new Paragraph(line);
                if (title)
                {
                    p.SetFont(bold).SetFontSize(12);
                    title = false;
                }
                else
                {
                    p.SetFirstLineIndent(36);
                }
                if (String.IsNullOrEmpty(line))
                {
                    document.Add(nextPage);
                    title = true;
                }
                document.Add(p);
            }
            renderer.Flush();
            document.Close();
        }
Пример #9
0
        protected internal override LayoutArea UpdateCurrentArea(LayoutResult overflowResult)
        {
            FlushWaitingDrawingElements();
            LayoutTaggingHelper taggingHelper = this.GetProperty <LayoutTaggingHelper>(Property.TAGGING_HELPER);

            if (taggingHelper != null)
            {
                taggingHelper.ReleaseFinishedHints();
            }
            AreaBreak areaBreak = overflowResult != null && overflowResult.GetAreaBreak() != null?overflowResult.GetAreaBreak
                                      () : null;

            if (areaBreak != null && areaBreak.GetAreaType() == AreaBreakType.LAST_PAGE)
            {
                while (currentPageNumber < document.GetPdfDocument().GetNumberOfPages())
                {
                    MoveToNextPage();
                }
            }
            else
            {
                MoveToNextPage();
            }
            PageSize customPageSize = areaBreak != null?areaBreak.GetPageSize() : null;

            while (document.GetPdfDocument().GetNumberOfPages() >= currentPageNumber && document.GetPdfDocument().GetPage
                       (currentPageNumber).IsFlushed())
            {
                currentPageNumber++;
            }
            PageSize lastPageSize = EnsureDocumentHasNPages(currentPageNumber, customPageSize);

            if (lastPageSize == null)
            {
                lastPageSize = new PageSize(document.GetPdfDocument().GetPage(currentPageNumber).GetTrimBox());
            }
            return(currentArea = new RootLayoutArea(currentPageNumber, GetCurrentPageEffectiveArea(lastPageSize)));
        }
Пример #10
0
        /*
         * public static File GetPDF(string html, string destFile = @"/Data/PdfOutput/pdfFile")
         * {
         *
         * }*/
        /*
         * // pdfHTML specific code
         * ConverterProperties converterProperties = new ConverterProperties();
         * HtmlConverter.convertToPdf(new FileInputStream(htmlSource), new FileOutputStream(pdfDest), converterProperties);
         * **/
        public static void ExportarPDF2(string html, string FileName)
        {
            var destFile = FileName;
            var fStream  = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            // var pdfTemp = new MemoryStream();
            // Stream dest = new Stream;
            //Initialize PDF writer
            PdfWriter writer = new PdfWriter(fStream);

            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(writer);

            // Initialize document
            Document document = new Document(pdf);
            var      elem     = new Text(html);
            var      area     = new AreaBreak();

            // area.SetTextRenderingMode()
            // Add paragraph to the document
            // document.Add();

            //Close document
            document.Close();
        }
Пример #11
0
 /// <summary>Terminates the current element, usually a page.</summary>
 /// <remarks>
 /// Terminates the current element, usually a page. Sets the next element
 /// to be the size specified in the argument.
 /// </remarks>
 /// <param name="areaBreak">
 /// an
 /// <see cref="iText.Layout.Element.AreaBreak"/>
 /// , optionally with a specified size
 /// </param>
 /// <returns>this element</returns>
 public virtual iText.Layout.Document Add(AreaBreak areaBreak)
 {
     childElements.Add(areaBreak);
     EnsureRootRendererNotNull().AddChild(areaBreak.CreateRendererSubTree());
     return(this);
 }
Пример #12
0
 public virtual iText.Layout.Layout.LayoutResult SetAreaBreak(AreaBreak areaBreak)
 {
     this.areaBreak = areaBreak;
     return(this);
 }
Пример #13
0
 /// <summary>Creates an AreaBreakRenderer.</summary>
 /// <param name="areaBreak">
 /// the
 /// <see cref="iText.Layout.Element.AreaBreak"/>
 /// that will be rendered by this object
 /// </param>
 public AreaBreakRenderer(AreaBreak areaBreak)
 {
     this.areaBreak = areaBreak;
 }
Пример #14
0
 public void Add(AreaBreak areaBreak)
 {
     elements.Add(areaBreak);
 }