예제 #1
0
        public static void EmbedEdiconData(string srcPdfFileName, string ediconXmlData, string dstPdfFileName)
        {
            //
            PdfReader  reader  = null;
            PdfStamper stamper = null;
            FileStream destPdfFileFileStream = null;

            //
            try
            {
                reader = new PdfReader(srcPdfFileName);
                destPdfFileFileStream = new FileStream(dstPdfFileName, FileMode.OpenOrCreate);
                stamper = new PdfStamper(reader, destPdfFileFileStream);
                PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(stamper.Writer, null /*srcDataFileName*/, EDICON_EMBED_FILENAME, Encoding.UTF8.GetBytes(ediconXmlData));
                stamper.AddFileAttachment(EDICON_EMBED_FILENAME, pfs);
            }
            catch (Exception ex)
            {
                Console.WriteLine("EmbedEdiconData error: " + ex.Message);
                throw;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                //
                if (stamper != null)
                {
                    stamper.Close();
                }
                //
                if (destPdfFileFileStream != null)
                {
                    destPdfFileFileStream.Close();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Converts the specified html documents to a single pdf file.
        /// </summary>
        /// <param name="htmlDocuments"></param>
        /// <param name="pdfAttachments"></param>
        /// <param name="pdfConverterLicenseKey"></param>
        /// <returns></returns>
        public static byte[] ConvertHtmlToPdf(IEnumerable <string> htmlDocuments, ICollection <PdfAttachment> pdfAttachments, string pdfConverterLicenseKey)
        {
            //TODO: Add the ability to customize layout and margins.

            PdfConverter pdfConverter = new PdfConverter
            {
                PdfStandardSubset = PdfStandardSubset.Pdf_A_1b
            };

            if (String.IsNullOrEmpty(pdfConverterLicenseKey) == false)
            {
                pdfConverter.LicenseKey = pdfConverterLicenseKey;
            }

            Winnovative.WnvHtmlConvert.PdfDocument.Document doc = null;
            try
            {
                if (htmlDocuments != null)
                {
                    foreach (var htmlDocument in htmlDocuments)
                    {
                        if (doc == null)
                        {
                            doc = pdfConverter.GetPdfDocumentObjectFromHtmlString(htmlDocument);
                        }
                        else
                        {
                            doc.AppendDocument(pdfConverter.GetPdfDocumentObjectFromHtmlString(htmlDocument));
                        }
                    }
                }

                if (doc == null)
                {
                    throw new InvalidOperationException("No PDF Document Object was created.");
                }

                //Use iTextSharp to add all attachments
                if (pdfAttachments != null && pdfAttachments.Any())
                {
                    var reader = new PdfReader(doc.Save());

                    using (var outputPdfStream = new MemoryStream())
                    {
                        var stamper = new PdfStamper(reader, outputPdfStream);
                        foreach (var pdfAttachment in pdfAttachments)
                        {
                            stamper.AddFileAttachment(pdfAttachment.Description, pdfAttachment.Data, pdfAttachment.FileName, pdfAttachment.FileDisplayName);
                        }
                        stamper.Close();
                        outputPdfStream.Flush();
                        return(outputPdfStream.GetBuffer());
                    }
                }
                else
                {
                    return(doc.Save());
                }
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                }
            }
        }
예제 #3
0
파일: Report.cs 프로젝트: marlonnn/TSP_FCT
        public void GeneratePdf(TestedRacks tr)
        {
            PdfReader  rdr;
            PdfStamper stamper;
            BaseFont   baseFont;

            string pdfFile  = Util.GetBasePath() + "//Report//" + tr.Key + ".pdf";
            string dataFile = Util.GetBasePath() + "//Report//Data//" + tr.Key + ".trs";
            string logFile  = Util.GetBasePath() + "//Report//Data//" + tr.Key + ".log";

            LogHelper.GetLogger <Report>().Debug(pdfFile);
            LogHelper.GetLogger <Report>().Debug(dataFile);
            LogHelper.GetLogger <Report>().Debug(logFile);

            try
            {
                baseFont = BaseFont.CreateFont(fontFile, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
                MessageBox.Show("报告系统的资源文件载入失败,请与开发人员联系。");
                return;
            }

            try
            {
                rdr = new PdfReader(Util.GetBasePath() + templetFile);
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
                MessageBox.Show(string.Format("模板文件载入失败,请检查'{0}'是否存在。",
                                              Util.GetBasePath() + templetFile));
                return;
            }

            try
            {
                stamper = new PdfStamper(rdr, new System.IO.FileStream(pdfFile, System.IO.FileMode.Create));
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
                MessageBox.Show(string.Format("报告文件'{0}'创建失败,请关闭软件重新生成报告。", pdfFile));
                rdr.Close();
                return;
            }

            try
            {
                stamper.AcroFields.AddSubstitutionFont(baseFont);

                SetFieldValue(stamper, "pageHead", reportTitle);
                SetFieldValue(stamper, "Head", reportTitle);
                stamper.AcroFields.SetFieldProperty("Head", "textsize", 20.0f, null);

                SetHeadFieldValue(stamper, "ver", string.Format("{0} Build:{1}", version.Ver, version.Build));
                SetHeadFieldValue(stamper, "data", tr.Key + ".trs");
                SetHeadFieldValue(stamper, "gDate", Util.FormateDateTime2(DateTime.Now));

                SetFieldValue(stamper, "tester", tr.Tester);
                SetFieldValue(stamper, "startTime", Util.FormateDateTime(tr.StartTime));
                SetFieldValue(stamper, "SN", tr.GetSN());
                SetFieldValue(stamper, "runningTime", Util.FormateDurationSecondsMaxHour(tr.RunningTime));
                if (tr.IsPass())
                {
                    SetFieldValue(stamper, "IsPass", "PASS");
                    stamper.AcroFields.SetFieldProperty("IsPass", "textsize", 38.0f, null);
                    stamper.AcroFields.SetFieldProperty("IsPass", "textcolor", BaseColor.BLUE, null);
                }
                else
                {
                    SetFieldValue(stamper, "IsPass", "FAIL");
                    stamper.AcroFields.SetFieldProperty("IsPass", "textsize", 38.0f, null);
                    stamper.AcroFields.SetFieldProperty("IsPass", "textcolor", BaseColor.RED, null);
                }
                string testType    = "";
                string boardName   = "";
                string isBoardPass = "";
                foreach (Rack r in tr.Racks)
                {
                    if (r.IsTested)
                    {
                        testType += r.Name + "\n";
                        foreach (Board b in r.Boards)
                        {
                            if (!b.IsTested)
                            {
                                continue;
                            }
                            boardName   += b.Name + "\n";
                            isBoardPass += b.IsPassed ? "PASS\n" : "FAIL\n";
                        }
                    }
                }

                testType    += endString;
                boardName   += endString;
                isBoardPass += endString;
                SetFieldValue(stamper, "testType", testType);
                SetFieldValue(stamper, "boardName", boardName);
                SetFieldValue(stamper, "isBoardPass", isBoardPass);

                if (File.Exists(logFile))
                {
                    stamper.AddFileAttachment("Diagnostics logs", null, logFile, "log.txt");
                }

                stamper.FormFlattening = true;//不允许编辑
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
            }

            try
            {
                stamper.Close();
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
            }

            try
            {
                rdr.Close();
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
            }

            try
            {
                System.Diagnostics.Process.Start(pdfFile);
            }
            catch (Exception ee)
            {
                LogHelper.GetLogger <Report>().Error(ee.Message);
                LogHelper.GetLogger <Report>().Error(ee.StackTrace);
            }
        }