启用使用c#将网页保存为pdf格式,这是我的代码 [英] Enable to save webpage in pdf using c# below is my code

查看:144
本文介绍了启用使用c#将网页保存为pdf格式,这是我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Data;
using System.Data.SqlClient;
using System.Web.Profile;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
using System.Linq;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web;
using iTextSharp.text.xml;
using System.Xml;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows;
using System.Messaging;

using System.ComponentModel;
protected override void Render(HtmlTextWriter writer)
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);


    }
 public void CreatePDFDocument(string strHtml)
    {
        Table tbl = new Table();

        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test10.pdf", FileMode.Create));
       StringReader se = new StringReader(data.InnerText);
     
        Paragraph paragraph = new Paragraph("''" + se + "''");
        doc.Open();
        doc.Add(paragraph);
       
        doc.Close();
    }



[edit]来自OP的重复问题:

您好,
我只想使用c#将当前网页另存为图像,
当我从当前网页创建pdf文件时,它将显示html表单,其中包含我不想显示的内容,而我只想显示与网页相同的内容.
[/edit]



[edit]From OP''s duplicate question:

Hello ,
I just want to save a current web page as image using c# ,
and i when i create a pdf file from the current webpage it will show the html form with content that i dont want to show i just want to show same as webpage.
[/edit]

推荐答案

您好,Sharad,

我没有检查代码的问题,但是您可以使用下面的代码将html转换为pdf,它对我来说很好,我希望它也对您有用:).

我使用ITextSharp dll版本5.1.2.0

Hi Sharad,

I didn''t checked whatz the problem with your code, but you can use below code to convert html to pdf, it works fine for me and I hope it will work for you also :).

I use ITextSharp dll version 5.1.2.0

private void Converter(string htmlText, string path)
        {
            Document doc = new Document(PageSize.A4);
            try
            {
                iTextSharp.text.pdf.PdfWriter pdf = (iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create)));
                //pdf.SetEncryption(iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "user password", "owner password", iTextSharp.text.pdf.PdfWriter.ALLOW_COPY);
                doc.Open();

                //make an arraylist ....with STRINGREADER since its no IO reading file...
                System.Collections.Generic.List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
                //add the collection to the document
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    doc.Add((IElement)htmlarraylist[k]);
                }
                // or add the collection to an paragraph 
                // if you add it to an existing non emtpy paragraph it will insert it from
                //the point youwrite -
                //Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
                //mypara.IndentationLeft = 36;
                //mypara.InsertRange(0, htmlarraylist);
                //doc.Add(mypara);
                doc.Close();
            }
            catch (Exception ex)
            {
                //Log error
                try
                {
                    doc.Close();
                }
                catch (Exception e)
                {
                    //log document closing error
                }
            }
        }


在我的情况下,这是简单的内联css,并且这段代码可以很好地工作.为了拥有一个单独的样式表文件,我认为您需要使用"ParseToList"方法的一些重载,其中之一带有样式表参数.

我为此使用了null,因为我没有使用外部CSS.

对于图像而言,此代码可以正常工作,直到获得绝对路径(URL或绝对目录文件路径)为止.给定相对路径时,它会开始哭泣,因此,我建议您在将其通过转换器方法传递之前,先更正html中的图像src值.

希望对您有帮助.
well in my case it was simple inline css and this piece of code worked perfectly fine their. For having a separate style sheet file, I think you need to use some overload of "ParseToList" method, one of them takes a stylesheet parameter.

I have passed null for that as i haven''t used an external css.

For image thing, this code works fine till it gets absolute paths(URLs or absolute directory file path). It starts crying when given relative path, so I''ll suggest you to correct image src values in html before passing it in converter method.

Hope this helps you.


这篇关于启用使用c#将网页保存为pdf格式,这是我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆