示例#1
0
        /// <summary>
        /// Created PDF document from given content. See https://github.com/CommunityHiQ/Frends.Community.PDFWriter
        /// </summary>
        /// <param name="outputFile"></param>
        /// <param name="documentSettings"></param>
        /// <param name="content"></param>
        /// <param name="options"></param>
        /// <returns>Object { bool Success, string FileName }</returns>
        public static Output CreatePdf([PropertyTab] FileProperties outputFile,
                                       [PropertyTab] DocumentSettings documentSettings,
                                       [PropertyTab] DocumentContent content,
                                       [PropertyTab] Options options)
        {
            try
            {
                var document = new Document();
                if (!string.IsNullOrWhiteSpace(documentSettings.Title))
                {
                    document.Info.Title = documentSettings.Title;
                }
                if (!string.IsNullOrWhiteSpace(documentSettings.Author))
                {
                    document.Info.Author = documentSettings.Author;
                }

                // Get the selected page size
                Unit width, height;
                PageSetup.GetPageSize(documentSettings.Size.ConvertEnum <PageFormat>(), out width, out height);

                var section = document.AddSection();
                SetupPage(section.PageSetup, width, height, documentSettings);

                // index for stylename
                var elementNumber = 0;
                // add page elements
                foreach (var pageElement in content.Contents)
                {
                    var styleName = $"style_{elementNumber}";
                    var style     = document.Styles.AddStyle(styleName, "Normal");
                    switch (pageElement.ContentType)
                    {
                    case ElementType.Image:
                        AddImage(section, pageElement, width, style);
                        break;

                    case ElementType.PageBreak:
                        section = document.AddSection();
                        SetupPage(section.PageSetup, width, height, documentSettings);
                        break;

                    case ElementType.Header:
                        SetFont(style, pageElement);
                        SetParagraphStyle(style, pageElement);
                        AddHeaderFooterContent(section, pageElement, style, true);
                        break;

                    case ElementType.Footer:
                        SetFont(style, pageElement);
                        SetParagraphStyle(style, pageElement);
                        AddHeaderFooterContent(section, pageElement, style, false);
                        break;

                    case ElementType.Table:
                        AddTable(section, pageElement, width);
                        break;

                    default:
                        SetFont(style, pageElement);
                        SetParagraphStyle(style, pageElement);
                        AddTextContent(section, pageElement, style);
                        break;
                    }

                    elementNumber++;
                }

                string fileName      = Path.Combine(outputFile.Directory, outputFile.FileName);
                int    fileNameIndex = 1;
                while (File.Exists(fileName) && outputFile.FileExistsAction != FileExistsActionEnum.Overwrite)
                {
                    switch (outputFile.FileExistsAction)
                    {
                    case FileExistsActionEnum.Error:
                        throw new Exception($"File {fileName} already exists.");

                    case FileExistsActionEnum.Rename:
                        fileName = Path.Combine(outputFile.Directory, $"{Path.GetFileNameWithoutExtension(outputFile.FileName)}_({fileNameIndex}){Path.GetExtension(outputFile.FileName)}");
                        break;
                    }
                    fileNameIndex++;
                }
                // save document

                var pdfRenderer = new PdfDocumentRenderer(outputFile.Unicode)
                {
                    Document = document
                };

                pdfRenderer.RenderDocument();

                if (!options.UseGivenCredentials)
                {
                    pdfRenderer.PdfDocument.Save(fileName);
                }
                else
                {
                    var domainAndUserName = GetDomainAndUserName(options.UserName);
                    Impersonation.RunAsUser(new UserCredentials(domainAndUserName[0], domainAndUserName[1], options.Password), LogonType.NewCredentials, () => pdfRenderer.PdfDocument.Save(fileName));
                }

                return(new Output {
                    Success = true, FileName = fileName
                });
            }
            catch (Exception ex)
            {
                if (options.ThrowErrorOnFailure)
                {
                    throw ex;
                }

                return(new Output {
                    Success = false
                });
            }
        }
示例#2
0
 /// <summary>
 /// Set page settings
 /// </summary>
 /// <param name="setup"></param>
 /// <param name="pageWidth"></param>
 /// <param name="pageHeight"></param>
 /// <param name="documentSettings"></param>
 private static void SetupPage(PageSetup setup, Unit pageWidth, Unit pageHeight, DocumentSettings documentSettings)
 {
     setup.Orientation  = documentSettings.Orientation.ConvertEnum <Orientation>();
     setup.PageHeight   = pageHeight;
     setup.PageWidth    = pageWidth;
     setup.LeftMargin   = new Unit(documentSettings.MarginLeftInCm, UnitType.Centimeter);
     setup.TopMargin    = new Unit(documentSettings.MarginTopInCm, UnitType.Centimeter);
     setup.RightMargin  = new Unit(documentSettings.MarginRightInCm, UnitType.Centimeter);
     setup.BottomMargin = new Unit(documentSettings.MarginBottomInCm, UnitType.Centimeter);
 }