コード例 #1
0
        public PDFCreate()
        {
            //document = new Document(PageSize.A4.Rotate());
            document = new Document(PageSize.A4);
            ms       = new MemoryStream();
            writer   = PdfWriter.GetInstance(document, ms);//new FileStream("d:\\abc.pdf",FileMode.Create,FileAccess.Write));
            worker   = new HTMLWorker(document);
            Dictionary <string, object> dicProvider = new Dictionary <string, object>();

            dicProvider.Add(HTMLWorker.FONT_PROVIDER, new NewFontProvider());
            worker.SetProviders(dicProvider);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: miyamukg/markdown-editor
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();

            sf.Filter  = "MDファイル|*.md";
            sf.Filter += "|HTMLファイル|*.html";
            sf.Filter += "|PDFファイル|*.pdf";

            //ダイアログを表示する

            if (sf.ShowDialog() == DialogResult.OK)
            {
                switch (sf.FilterIndex)
                {
                case 1:
                    System.IO.Stream stream;
                    stream = sf.OpenFile();
                    if (stream != null)
                    {
                        //ファイルに書き込む
                        StreamWriter sw = new StreamWriter(stream);
                        sw.Write(txtMarkdown.Text);
                        //閉じる
                        sw.Close();
                        stream.Close();
                    }
                    break;

                case 2:
                    System.IO.Stream _stream;
                    _stream = sf.OpenFile();
                    if (_stream != null)
                    {
                        //ファイルに書き込む
                        StreamWriter sw = new StreamWriter(_stream);
                        sw.Write(webBrowser1.DocumentText);
                        //閉じる
                        sw.Close();
                        _stream.Close();
                    }
                    break;

                case 3:
                    //Byte[] result = null;
                    //using (MemoryStream ms = new MemoryStream())
                    //{
                    //    PdfDocument pdfDocument = new PdfDocument();
                    //    PdfDocument pdf = PdfGenerator.GeneratePdf(webBrowser1.DocumentText, PdfSharp.PageSize.A4, 60);
                    //    pdf.Save(ms);
                    //    result = ms.ToArray();
                    //    //var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(webBrowser1.DocumentText, PdfSharp.PageSize.A4);
                    //    //pdf.Save(ms);
                    //    //result = ms.ToArray();
                    //}
                    //using (BinaryWriter w = new BinaryWriter(File.OpenWrite(sf.FileName)))
                    //{
                    //    w.Write(result);
                    //}
                    Font fnt = new Font(BaseFont.CreateFont(@"c:\windows\fonts\meiryo.ttc,0", BaseFont.IDENTITY_H, true), 40);

                    Dictionary <string, object> dicProvider = new Dictionary <string, object>();
                    dicProvider.Add(HTMLWorker.FONT_PROVIDER, new NewFontProvider(returnValue));

                    string html = webBrowser1.DocumentText;

                    string     outfilepath = sf.FileName;
                    FileStream fsoutput    = new FileStream(outfilepath, FileMode.Create);
                    TextReader reader      = new StringReader(html);

                    // step 1: creation of a document-object
                    Rectangle pageSize = new Rectangle(PageSize.A4);
                    switch (returnValue)
                    {
                    case "normal":
                        pageSize.BackgroundColor = new BaseColor(255, 255, 255);
                        break;

                    case "dark":
                        pageSize.BackgroundColor = new BaseColor(0, 0, 0);
                        break;

                    case "paradise":
                        pageSize.BackgroundColor = new BaseColor(84, 141, 212);
                        break;

                    default:
                        pageSize.BackgroundColor = new BaseColor(255, 255, 255);
                        break;
                    }

                    Document document = new Document(pageSize, 30, 30, 30, 30);


                    // step 2:
                    // we create a writer that listens to the document
                    // and directs a XML-stream to a file
                    PdfWriter writer = PdfWriter.GetInstance(document, fsoutput);

                    // step 3: we create a worker parse the document
                    HTMLWorker worker = new HTMLWorker(document);
                    worker.SetProviders(dicProvider);

                    // step 4: we open document and start the worker on the document
                    document.Open();
                    worker.StartDocument();

                    // step 5: parse the html into the document
                    worker.Parse(reader);

                    // step 6: close the document and the worker
                    worker.EndDocument();
                    worker.Close();
                    document.Close();

                    Console.ReadLine();

                    break;
                }
            }
        }