Example #1
0
        public static CreationResult Create(
            IBinary binMem,
            int startPage       = -1,
            int endPage         = -1,
            int startIdx        = -1,
            int endIdx          = -1,
            int parentElementId = -1,
            int readPage        = 0,
            Point readPoint     = default,
            ViewModes viewMode  = PDFConst.DefaultViewMode,
            int pageMargin      = PDFConst.DefaultPageMargin,
            float zoom          = PDFConst.DefaultZoom,
            bool shouldDisplay  = true,
            string subtitle     = null)
        {
            PDFElement pdfEl;
            string     title;
            string     author;
            string     creationDate;
            string     filePath;

            try
            {
                filePath = binMem.GetFilePath("pdf");

                if (File.Exists(filePath) == false)
                {
                    return(CreationResult.FailBinaryMemberFileMissing);
                }

                pdfEl = new PDFElement
                {
                    BinaryMemberId = binMem.Id,
                    FilePath       = filePath,
                    StartPage      = startPage,
                    EndPage        = endPage,
                    StartIndex     = startIdx,
                    EndIndex       = endIdx,
                    ReadPage       = readPage,
                    ReadPoint      = readPoint,
                    ViewMode       = viewMode,
                    PageMargin     = pageMargin,
                    Zoom           = zoom,
                };

                pdfEl.GetInfos(out string pdfTitle,
                               out author,
                               out creationDate);

                title = pdfEl.ConfigureTitle(pdfTitle, subtitle);
            }
            catch (Exception ex)
            {
                LogTo.Error(ex, "Exception thrown while creating new PDF element");
                return(CreationResult.FailUnknown);
            }

            string elementHtml = string.Format(CultureInfo.InvariantCulture,
                                               PDFConst.ElementFormat,
                                               title,
                                               binMem.Name,
                                               pdfEl.GetJsonB64());

            IElement parentElement =
                parentElementId > 0
          ? Svc.SM.Registry.Element[parentElementId]
          : null;

            var elemBuilder =
                new ElementBuilder(ElementType.Topic,
                                   elementHtml)
                .WithParent(parentElement)
                .WithTitle(subtitle ?? title)
                .WithPriority(PDFState.Instance.Config.PDFExtractPriority)
                .WithReference(
                    r => r.WithTitle(title)
                    .WithAuthor(author)
                    .WithDate(creationDate)
                    .WithSource("PDF")
                    .WithLink("..\\" + Svc.SM.Collection.MakeRelative(filePath))
                    );

            if (shouldDisplay == false)
            {
                elemBuilder = elemBuilder.DoNotDisplay();
            }

            return(Svc.SM.Registry.Element.Add(out _, ElemCreationFlags.CreateSubfolders, elemBuilder)
        ? CreationResult.Ok
        : CreationResult.FailCannotCreateElement);
        }