private void button1_Click(object sender, EventArgs e)
        {
            string location = "../../Data/CustomInvoice.pdf";
            //Load the PDF document
            PdfLoadedDocument page1Document = new PdfLoadedDocument(location);

            //Created new Pdf Document
            PdfDocument doc = new PdfDocument();
            //Gets the form from Loaded document
            PdfLoadedForm form = page1Document.Form;
            //Gets the Form fields
            PdfLoadedFormFieldCollection field = form.Fields;

            // fill the form fields.
            for (int i = 0; i < field.Count; i++)
            {
                string name = (form.Fields[i].Name);
                (form.Fields[name]).Flatten = true;
                (form.Fields[name]).ToolTip = "sample";
            }

            //flattens the whole form.
            form.Flatten = true;
            //Append the loaded document
            doc.Append(page1Document);
            //Save the document and dispose it
            doc.Save("Converted-" + DateTime.Now.ToString("mmssddMMyyyy") + ".pdf");
            doc.Close();

            label1.Text = "Done. Please check Bin folder.";
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "MergeDocuments.pdf";

            using (PdfDocument pdf = new PdfDocument(@"..\Sample data\form.pdf"))
            {
                pdf.Append(@"..\Sample data\jfif3.pdf");

                pdf.Save(pathToFile);
            }

            Console.WriteLine($"The output is located in {Environment.CurrentDirectory}");
        }
Exemple #3
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "MergeDocuments.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.Open("Sample data/form.pdf");
                pdf.Append("Sample data/jfif3.pdf");

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
Exemple #4
0
        private static void Merger(string reportType, List <string> reports, string sourcePath, string dropPath, string currentTime, string reportName)
        {
            var         combineFile = false;
            PdfDocument Pdf         = new PdfDocument();

            Console.WriteLine(String.Format("Starting {0} Report Merging...", reportType));
            foreach (string file in reports)
            {
                if (File.Exists(sourcePath + file))
                {
                    Console.WriteLine("Appending : " + file);
                    combineFile = true;
                    Pdf.Append(new PdfImportedDocument(sourcePath + file));
                }
            }
            if (combineFile)
            {
                combineFile = false;
                Console.WriteLine("Saving : " + String.Format(reportName, currentTime));
                Pdf.Save(dropPath + String.Format(reportName, currentTime));
                Console.WriteLine("Done");
            }
            Pdf.Close();
        }
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["start_batch"] != null)
            {
                context.Session["in_batch"] = true;
                context.Session["batch"]    = new List <PdfDocument>();
                return;
            }

            if (context.Request["end_batch"] != null)
            {
                // Handle batching.
                var batch         = context.Session["batch"] as List <PdfDocument>;
                var finalDocument = new PdfDocument();
                foreach (var doc in batch)
                {
                    finalDocument.Append(doc);
                }
                using (var ms_out = new MemoryStream())
                {
                    finalDocument.Save(ms_out);
                    string outputFilename = "Test.pdf";
                    if (context.Request["filename"] != null)
                    {
                        outputFilename = context.Request["filename"];
                    }
                    context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", outputFilename));
                    context.Response.AddHeader("Content-Length", ms_out.Length.ToString());
                    context.Response.ContentType = "application/pdf";
                    context.Response.BinaryWrite(ms_out.GetBuffer());
                }
                context.Session.Remove("in_batch");
                context.Session.Remove("batch");
                return;
            }

            bool inBatch = (bool?)context.Session["in_batch"] ?? false;

            if (context.Request["html"] == null)
            {
                throw new InvalidDataException("Requires 'html' value in POST data.");
            }
            var converter = new HtmlToPdf();

            converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
            converter.Options.PdfPageSize        = context.Request["pageSize"] != null && context.Request["pageSize"] == "11x17" ? PdfPageSize.Letter11x17 : PdfPageSize.Letter;
            converter.Options.MarginBottom       = 32;
            converter.Options.MarginLeft         = 32;
            converter.Options.MarginRight        = 32;
            converter.Options.MarginTop          = 32;
            var document = converter.ConvertHtmlString(context.Request["html"]);

            if (context.Request["bookmarkName"] != null)
            {
                document.AddBookmark(context.Request["bookmarkName"], new PdfDestination(document.Pages[0], new PointF(0, 0)));
            }
            if (inBatch)
            {
                (context.Session["batch"] as List <PdfDocument>).Add(document);
            }
            else
            {
                using (var ms_out = new MemoryStream())
                {
                    document.Save(ms_out);
                    string outputFilename = "Test.pdf";
                    if (context.Request["filename"] != null)
                    {
                        outputFilename = context.Request["filename"];
                    }
                    context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", outputFilename));
                    context.Response.AddHeader("Content-Length", ms_out.Length.ToString());
                    context.Response.ContentType = "application/pdf";
                    context.Response.BinaryWrite(ms_out.GetBuffer());
                }
            }
        }
        public ActionResult <string> HtmlToPdfList(int PageSize = 1)
        {
            //测试Html素材
            string      htmlString = @"<!DOCTYPE html>
						<html>
						<head>
						    <meta charset='utf-8' />
						    <title></title>
						     <style>
						       body {
						         margin: 0;
						         padding: 0;
						         min-width: 2000px;
						      }
						      .m_table {
						         border-collapse: separate;
						         margin: 100px auto 0;
						         min-width: 1900px;
						         text-align: center;
						         font: 500 17px '微软雅黑';
						         border-spacing: 0;
							     border: 1px solid #EBEEF5;
						      }
						
						      .m_table th {
						         background-color: #F7F3F7;
						      }
						
						      .m_table th,
						      .m_table td {
						         border-right: 1px solid #EBEEF5;
								 border-bottom: 1px solid #EBEEF5; 
						         padding: 5px;
						         height: 60px;
						         width: 60px;
						      }
						   </style>
						</head>
						
						<body>
						    
						    <table class='m_table'> 
						        <tr>
						            <th colspan='9' style='text-align:center;font-size:28px;'>期末学生综合评价表1</th>
						        </tr>
								<tr>
						            <th colspan='9' style='text-align:center;font-size:23px;'>班级:一年级一班  姓名:测试  班主任:测试</th> 
						        </tr>
						        <tr>
						            <th  rowspan='2' style='text-align:center;font-size:24px;'>学科</th>
						            <th  colspan='2'  rowspan='2' style='text-align:center;font-size:24px;'>过程性评价</th>
						            <th colspan='2' rowspan='2' style='text-align:center;font-size:24px;'>表现性评价</th>
						            <th colspan='2'  style='text-align:center;font-size:20px;'>考试性评价</th>
						            <th colspan='2' style='text-align:center;font-size:20px;'>综合性评价</th>
						        </tr>
						        <tr> 
						            <th   style='text-align:center;font-size:18px;'>卷面分</th>
						            <th  style='text-align:center;font-size:18px;'>权重分</th>
						            <th   style='text-align:center;font-size:18px;'>总分</th>
						            <th  style='text-align:center;font-size:18px;'>等级</th>
						        </tr>
						        <tr>
								<td> 语文 </td>
								<td> 作业评价/4 </td>
								<td> 课堂表现/5 </td>
								<td> 学科必选/9 </td>
								<td> 学科自选/9.6 </td>
								<td> 99 </td>
								<td> 69.3 </td>
								<td> 96.9 </td>
								<td> A </td>
								</tr>
								
								<tr><td> 数学 </td><td> 作业评价/4 </td><td> 课堂表现/4 </td><td> 学科必选/10 </td><td> 学科自选/7 </td><td> 98 </td><td> 68.6 </td><td> 93.6 </td><td> A </td></tr><tr><td> 英语 </td><td> 作业评价/4.5 </td><td> 课堂表现/5 </td><td> 学科必选/1.7 </td><td> 学科自选/1.7 </td><td> 59 </td><td> 41.3 </td><td> 54.2 </td><td> D </td></tr><tr><td> 英语 </td><td> 作业评价/4.5 </td><td> 课堂表现/5 </td><td> 学科必选/1.7 </td><td> 学科自选/1.7 </td><td> 59 </td><td> 41.3 </td><td> 54.2 </td><td> D </td></tr><tr><td> 英语 </td><td> 作业评价/4.5 </td><td> 课堂表现/5 </td><td> 学科必选/1.7 </td><td> 学科自选/1.7 </td><td> 59 </td><td> 41.3 </td><td> 54.2 </td><td> D </td></tr><tr><td> 英语 </td><td> 作业评价/4.5 </td><td> 课堂表现/5 </td><td> 学科必选/1.7 </td><td> 学科自选/1.7 </td><td> 59 </td><td> 41.3 </td><td> 54.2 </td><td> D </td></tr>
						    </table>  
						</body>
						</html>"                        ;
            PdfDocument docHtml    = null;

            for (int j = 0; j < PageSize; j++)
            {
                HtmlToPdf Renderer = new HtmlToPdf();
                //设置Pdf参数
                Renderer.Options.PdfPageOrientation = PdfPageOrientation.Landscape; //设置页面方式-横向  PdfPageOrientation.Portrait  竖向
                Renderer.Options.PdfPageSize        = PdfPageSize.A4;               //设置页面大小,30种页面大小可以选择
                Renderer.Options.MarginTop          = 10;                           //上下左右边距设置
                Renderer.Options.MarginBottom       = 10;
                Renderer.Options.MarginLeft         = 10;
                Renderer.Options.MarginRight        = 10;
                //设置更多额参数可以去HtmlToPdfOptions里面选择设置

                if (docHtml == null)
                {
                    docHtml = Renderer.ConvertHtmlString(htmlString);                    //根据html内容导出PDF
                }
                else
                {
                    //在上一个pdf元素页面下面追加Pdf页面,官方文档对于一个pdf文件打印多页的处理提供了分页符,在你想打印一页的元素外面加上   <div style="font-size: 28px; page-break-after: always">元素
                    //也就是分页符,但是试用感觉效果并不理想,下面这个Append追加一个pdf页面效果会更好点,但是可能会损耗一些性能
                    docHtml.Append(Renderer.ConvertHtmlString(htmlString));
                }
            }
            string webRootPath  = hostingEnv.ContentRootPath;                                 //获取项目运行绝对路径
            var    path         = $"/ExportPDF/{DateTime.Now.ToString("yyyyMMdd")}/";         //文件相对路径
            var    savepathHtml = $"{webRootPath}{path}{Guid.NewGuid().ToString()}-Html.pdf"; //保存绝对路径

            if (!Directory.Exists(Path.GetDirectoryName(webRootPath + path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(webRootPath + path));
            }
            docHtml.Save(savepathHtml);
            return(savepathHtml);
        }