Beispiel #1
0
        /// <summary>
        /// Generate Pdf. If data source is set it will use it. Otherwise it will use callbacks
        /// </summary>
        /// <param name="fileName"></param>
        /// <exception cref="PdfFactoryEngineException">Thrown when engine failed generating for some reason</exception>
        /// <exception cref="TemplateUsageException">Thrown when template file attached is not correct for some reason</exception>
        public void GeneratePdf(string fileName)
        {
            try
            {
                this.SetDefaultCallbacks();

                if (TemplateFileName.Length == 0)
                {
                    throw new TemplateUsageException(Strings.TemplateFileMissing);
                }

                if (Logging)
                {
                    Logger.LogNotice("SetLogging to pdffactory engine...");
                }
                PdfFactory.SetLogging(this.Logging, this.LoggingLevel);
                if (Logging)
                {
                    Logger.LogNotice("Initialize Generator...");
                }
                PdfFactory.InitializeGenerator(this.companyName.Trim(), this.serialNumber.Trim());
                if (Logging)
                {
                    Logger.LogNotice("Attach template from file...");
                }
                if (TemplateStream != null)
                {
                    PdfFactory.AttachTemplate(TemplateStream);
                }
                else
                {
                    PdfFactory.AttachTemplate(TemplateFileName);
                }

                if (Logging)
                {
                    Logger.LogNotice("Set InitializeDataStream callback...");
                }
                PdfFactory.SetInitializeDataStreamCallback(OnInitializeDataStream);
                PdfFactory.SetReadDataCallback(OnReadData);
                PdfFactory.SetRequestDataCallback(OnRequestData);
                if (Logging)
                {
                    Logger.LogNotice("GenerateToFile call...");
                }
                PdfFactory.Generate(fileName);
                if (Logging)
                {
                    Logger.LogNotice("Shutdown Generator...");
                }
                PdfFactory.ShutdownGenerator();
                if (Logging)
                {
                    Logger.LogNotice("GeneratePdf - Success...");
                }
            }
            catch (Exception ex)
            {
                if (this.Logging)
                {
                    Logger.LogException(ex);
                }
                // re-throw exception
                throw ex;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generate Pdf. If data source is set it will use it. Otherwise it will use callbacks
        /// </summary>
        /// <param name="fileName"></param>
        /// <exception cref="PdfFactoryEngineException">Thrown when engine failed generating for some reason</exception>
        /// <exception cref="TemplateUsageException">Thrown when template file attached is not correct for some reason</exception>
        public byte[] GeneratePdfToMemory(ref int dataSize)
        {
            try
            {
                this.SetDefaultCallbacks();

                if (TemplateFileName.Length == 0)
                {
                    throw new TemplateUsageException(Strings.TemplateFileMissing);
                }

                if (Logging)
                {
                    Logger.LogNotice("SetLogging to pdffactory engine...");
                }
                PdfFactory.SetLogging(this.Logging, this.LoggingLevel);

                if (Logging)
                {
                    Logger.LogNotice("Initialize Generator...");
                }
                PdfFactory.InitializeGenerator(this.companyName, this.serialNumber);

                if (Logging)
                {
                    Logger.LogNotice("Attach template from file...");
                }
                if (TemplateStream != null)
                {
                    PdfFactory.AttachTemplate(TemplateStream);
                }
                else
                {
                    PdfFactory.AttachTemplate(TemplateFileName);
                }

                if (Logging)
                {
                    Logger.LogNotice("Set InitializeDataStream callback...");
                }
                PdfFactory.SetInitializeDataStreamCallback(OnInitializeDataStream);
                PdfFactory.SetReadDataCallback(OnReadData);
                PdfFactory.SetRequestDataCallback(OnRequestData);

                if (Logging)
                {
                    Logger.LogNotice("GenerateToFile call...");
                }
                byte[] resultBuffer = PdfFactory.Generate(ref dataSize);

                if (Logging)
                {
                    Logger.LogNotice("Shutdown Generator...");
                }
                PdfFactory.ShutdownGenerator();

                if (Logging)
                {
                    Logger.LogNotice("GeneratePdf - Success...");
                }
                return(resultBuffer);
            }
            catch (Exception ex)
            {
                if (this.Logging)
                {
                    Logger.LogException(ex);
                }
                throw ex;
            }
        }