示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PdfObjectConfig"/> class.
 /// </summary>
 public PdfObjectConfig()
 {
     UseIndex                      = true;
     AllowCompression              = false;
     Tags                          = new SystemTagsCollection();
     CompressionThreshold          = int.MinValue;
     DeletePhysicalFilesAfterMerge = true;
     GlobalReplacements            = new GlobalReplacementsCollection();
 }
示例#2
0
        // Generates document
        public static void Generate(ILogger logger, YesNo useTestMode = YesNo.No)
        {
            #region Initialize timer
            var sw = new Stopwatch();
            sw.Start();
            #endregion

            #region page-1

            var page1 = new PdfInput
            {
                AutoUpdateChanges = true,
                Input             = "~/Resources/Sample-04/file-sample-1.pdf"
            };

            // Inserts report title
            page1.Replace(new ReplaceText(
                              new WithTextObject
            {
                Text           = "#TITLE#",
                NewText        = "Lorem ipsum",
                UseTestMode    = useTestMode,
                TextOffset     = PointF.Empty,
                Style          = TextStylesTable["ReportTitle"],
                ReplaceOptions = ReplaceTextOptions.AccordingToMargins
            }));


            // Inserts bar-chart image
            using (var barGraph = PdfImage.FromFile("~/Resources/Sample-04/Images/bar-chart.png"))
            {
                page1.Replace(new ReplaceText(
                                  new WithImageObject
                {
                    Text           = "#BAR-CHART#",
                    UseTestMode    = useTestMode,
                    ImageOffset    = PointF.Empty,
                    Style          = ImagesStylesTable["Default"],
                    ReplaceOptions = ReplaceTextOptions.AccordingToMargins,
                    Image          = barGraph
                }));
            }

            #endregion

            #region page-2

            var page2 = new PdfInput
            {
                AutoUpdateChanges = true,
                Input             = "~/Resources/Sample-04/file-sample-2.pdf"
            };

            // Inserts html table
            page2.Replace(new ReplaceText(
                              new WithTableObject
            {
                Text           = "#DATA-TABLE#",
                UseTestMode    = useTestMode,
                TableOffset    = PointF.Empty,
                Style          = PdfTableStyle.Default,
                ReplaceOptions = ReplaceTextOptions.FromPositionToRightMargin,
                Table          = PdfTable.CreateFromHtml(GenerateHtmlDatatable())
            }));

            #endregion

            #region page-3

            var page3 = new PdfInput
            {
                AutoUpdateChanges = true,
                Input             = "~/Resources/Sample-04/file-sample-3.pdf"
            };

            #endregion

            #region page-4

            var page4 = new PdfInput
            {
                AutoUpdateChanges = true,
                Input             = "~/Resources/Sample-04/file-sample-4.pdf"
            };

            // Inserts image
            using (var image = PdfImage.FromFile("~/Resources/Sample-04/Images/image-1.jpg"))
            {
                page4.Replace(new ReplaceText(
                                  new WithImageObject
                {
                    Text           = "#IMAGE1#",
                    UseTestMode    = useTestMode,
                    ImageOffset    = PointF.Empty,
                    Style          = ImagesStylesTable["Center"],
                    ReplaceOptions = ReplaceTextOptions.AccordingToMargins,
                    Image          = image
                }));
            }

            #endregion

            #region merge

            // Defines global text replacements to replace > header text
            var globalReplacements = new GlobalReplacementsCollection
            {
                new WithTextObject
                {
                    Text           = "#HEADER-TEXT#",
                    NewText        = "Report Name - Lorem ipsum dolor",
                    Style          = TextStylesTable["Header"],
                    ReplaceOptions = ReplaceTextOptions.FromLeftMarginToNextElement,
                    UseTestMode    = useTestMode,
                    TextOffset     = PointF.Empty
                }
            };

            var files = new PdfObject(new PdfObjectConfig {
                GlobalReplacements = globalReplacements
            })
            {
                Items = new List <PdfInput>
                {
                    new PdfInput {
                        Index = 0, Input = page1
                    },
                    new PdfInput {
                        Index = 1, Input = page2
                    },
                    new PdfInput {
                        Index = 2, Input = page3
                    },
                    new PdfInput {
                        Index = 3, Input = page4
                    },
                }
            };

            var mergeResult = files.TryMergeInputs();
            if (!mergeResult.Success)
            {
                logger.Info("   > Error creating output merge result");
                logger.Info($"     > Error: {mergeResult.Errors.AsMessages().ToStringBuilder()}");
                return;
            }

            #endregion

            #region save

            // Saves merged result to disk
            var saveResult = mergeResult.Result.Action(new SaveToFile {
                OutputPath = "~/Output/Sample04/Sample-04"
            });
            var ts = sw.Elapsed;
            sw.Stop();

            if (!saveResult.Success)
            {
                logger.Info("   > Error while saving to disk");
                logger.Info($"     > Error: {saveResult.Errors.AsMessages().ToStringBuilder()}");
                logger.Info($"   > Elapsed time: { ts.Hours:00}:{ ts.Minutes:00}:{ ts.Seconds:00}.{ ts.Milliseconds / 10:00}");
                return;
            }

            logger.Info("   > Saved to disk correctly");
            logger.Info("     > Path: ~/Output/Sample04/Sample-04.pdf");
            logger.Info($"   > Elapsed time: { ts.Hours:00}:{ ts.Minutes:00}:{ ts.Seconds:00}.{ ts.Milliseconds / 10:00}");

            #endregion
        }