コード例 #1
0
        public static void CreatePDF(System.IO.Stream stream, string htmlMarkup)
        {
            if (null == stream || string.IsNullOrEmpty(htmlMarkup))
            {
                return;
            }

            System.IO.MemoryStream m        = new System.IO.MemoryStream();
            iText.Document         document = new iText.Document();
            iText.pdf.PdfWriter    writer   = iText.pdf.PdfWriter.GetInstance(document, m);

            HTMLDocument pseudoDoc = new HTMLDocument();

            ((IHTMLDocument2)pseudoDoc).write(htmlMarkup);

            document.Open();

            IHTMLElementCollection children = (IHTMLElementCollection)pseudoDoc.getElementById("pdfContainer").children;

            foreach (IHTMLElement el in children)
            {
                IHTMLElement theEl = el;

                if (null == theEl.className)
                {
                    continue;
                }

                string className = theEl.className.ToLowerInvariant();

                if (className == "ignore" || string.IsNullOrEmpty(className))
                {
                    continue;
                }

                string tagName = theEl.tagName.ToLowerInvariant();

                string innerText = theEl.innerText;

                if (string.IsNullOrEmpty(innerText))
                {
                    theEl     = (IHTMLElement)((IHTMLElementCollection)el.children).item(null, 0);
                    tagName   = theEl.tagName.ToLowerInvariant();
                    innerText = theEl.innerText;
                }

                if (tagName == "div" || tagName == "span")
                {
                    document.Add(new iText.Paragraph(innerText));
                    continue;
                }

                if (tagName == "hr")
                {
                    if (className == "pagebreak")
                    {
                        document.NewPage();
                    }
                    else
                    {
                        iText.pdf.PdfPTable t = new iText.pdf.PdfPTable(1);
                        t.HorizontalAlignment             = iText.Element.ALIGN_CENTER;
                        t.WidthPercentage                 = 100f; // this would be the 100 from setHorizontalLine
                        t.SpacingAfter                    = 5f;
                        t.SpacingBefore                   = 0f;
                        t.DefaultCell.UseVariableBorders  = true;
                        t.DefaultCell.VerticalAlignment   = iText.Element.ALIGN_MIDDLE;
                        t.DefaultCell.HorizontalAlignment = iText.Element.ALIGN_CENTER;
                        t.DefaultCell.Border              = iText.Image.BOTTOM_BORDER; // This generates the line
                        t.DefaultCell.BorderWidth         = 1f;                        // this would be the 1 from setHorizontalLine
                        t.DefaultCell.Padding             = 0;
                        t.AddCell("");
                        document.Add(t);
                    }
                }

                if (tagName == "img")
                {
                    iText.Image img = iText.Image.GetInstance(theEl.getAttribute("src", 0).ToString());
                    img.ScaleToFit(document.PageSize.Width - 100, document.PageSize.Height - 100);
                    document.Add(img);
                    continue;
                }
            }

            pseudoDoc.close();
            pseudoDoc = null;

            document.Close();

            writer.Flush();

            byte[] data = m.GetBuffer();

            try
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                stream.Close();
            }
            catch
            {
                throw;
            }
            finally
            {
                Array.Clear(data, 0, data.Length);
                data     = null;
                document = null;
                writer   = null;
                m.Dispose();
                m = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }