public void WritePdf_TableWidthTooLarge() { var contentJson = @"{ ""PageSize"": ""A4"", ""PageOrientation"": ""Portrait"", ""Title"": ""Dokumentti"", ""Author"": ""FRENDS"", ""MarginLeftInCm"": 2.5, ""MarginTopInCm"": 2.5, ""MarginRightInCm"": 2.5, ""MarginBottomInCm"": 2.5, ""DocumentElements"": [ { ""HasHeaderRow"": false, ""TableType"": ""Header"", ""StyleSettings"": { ""FontFamily"": ""Times New Roman"", ""FontSizeInPt"": 10, ""FontStyle"": ""Regular"", ""LineSpacingInPt"": 0, ""Alignment"": ""Left"", ""SpacingBeforeInPt"": 0, ""SpacingAfterInPt"": 0, ""BorderWidthInPt"": 0, ""BorderStyle"": ""None"" }, ""Columns"": [ { ""Name"": ""Sarake 1"", ""WidthInCm"": 22, ""Type"": ""Text"" }, { ""Name"": ""Sarake 2"", ""WidthInCm"": 7, ""Type"": ""Text"" }, { ""Name"": ""Sarake 3"", ""WidthInCm"": 2, ""Type"": ""PageNum"" } ], ""RowData"": [ { ""Sarake 1"": ""Jotain tekstiä"", ""Sarake 2"": ""Tämä on keskimmäisen sarakkeen teksti"", ""Sarake 3"": """" } ] }]}"; var content = new DocumentContent { ContentJson = contentJson }; var result = Assert.Throws <Exception>(() => PdfTask.CreatePdf(_fileProperties, content, _options)); Assert.IsFalse(File.Exists(_destinationFullPath)); }
public void WritePdf_ImageNotFound() { var contentJson = @"{ ""PageSize"": ""A4"", ""PageOrientation"": ""Portrait"", ""Title"": ""Dokumentti"", ""Author"": ""FRENDS"", ""MarginLeftInCm"": 2.5, ""MarginTopInCm"": 2.5, ""MarginRightInCm"": 2.5, ""MarginBottomInCm"": 2.5, ""DocumentElements"": [ { ""ImagePath"": ""C:\\img\\logo.jpg"", ""Alignment"": ""Left"", ""LockAspectRatio"": false, ""ImageWidthInCm"": 2.5, ""ImageHeightInCm"": 2.5 } ]}"; var content = new DocumentContent { ContentJson = contentJson }; var result = Assert.Throws <FileNotFoundException>(() => PdfTask.CreatePdf(_fileProperties, content, _options)); Assert.IsFalse(File.Exists(_destinationFullPath)); }
public void WritePdf_DoesNotFailIfContentIsEmpty() { var contentJson = @"{ ""PageSize"": ""A4"", ""PageOrientation"": ""Portrait"", ""Title"": ""Dokumentti"", ""Author"": ""FRENDS"", ""MarginLeftInCm"": 2.5, ""MarginTopInCm"": 2.5, ""MarginRightInCm"": 2.5, ""MarginBottomInCm"": 2.5, ""DocumentElements"": [ ] }"; var content = new DocumentContent { ContentJson = contentJson }; var result = PdfTask.CreatePdf(_fileProperties, content, _options); Assert.IsTrue(File.Exists(_destinationFullPath)); Assert.IsTrue(result.Success); Assert.IsNotNull(result.ResultAsByteArray); }
public void WritePdf_HeaderGraphics() { var logoPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"TestFiles\logo.png").Replace("\\", "\\\\"); var contentJson = @"{ ""PageSize"": ""A4"", ""PageOrientation"": ""Portrait"", ""Title"": ""Dokumentti"", ""Author"": ""FRENDS"", ""MarginLeftInCm"": 2.5, ""MarginTopInCm"": 2.5, ""MarginRightInCm"": 2.5, ""MarginBottomInCm"": 2.5, ""DocumentElements"": [ { ""HasHeaderRow"": false, ""TableType"": ""Header"", ""StyleSettings"": { ""FontFamily"": ""Times New Roman"", ""FontSizeInPt"": 10, ""FontStyle"": ""Regular"", ""LineSpacingInPt"": 0, ""Alignment"": ""Left"", ""SpacingBeforeInPt"": 0, ""SpacingAfterInPt"": 0, ""BorderWidthInPt"": 0, ""BorderStyle"": ""None"" }, ""Columns"": [ { ""Name"": ""Sarake 1"", ""WidthInCm"": 2.5, ""Type"": ""Image"" }, { ""Name"": ""Sarake 2"", ""WidthInCm"": 7, ""Type"": ""Text"" }, { ""Name"": ""Sarake 3"", ""WidthInCm"": 2, ""Type"": ""PageNum"" } ], ""RowData"": [ { ""Sarake 1"": """ + logoPath + @""", ""Sarake 2"": ""Tämä on keskimmäisen sarakkeen teksti"", ""Sarake 3"": """" } ] }]}"; var content = new DocumentContent { ContentJson = contentJson }; var result = PdfTask.CreatePdf(_fileProperties, content, _options); Assert.IsTrue(File.Exists(_destinationFullPath)); Assert.IsTrue(result.Success); Assert.IsNotNull(result.ResultAsByteArray); }
public void TestSetup() { _folder = Path.Combine(Path.GetTempPath(), "pdfwriter_tests"); _destinationFullPath = Path.Combine(_folder, _fileName); if (!Directory.Exists(_folder)) { Directory.CreateDirectory(_folder); } _fileProperties = new FileProperties { Directory = _folder, FileName = _fileName, FileExistsAction = FileExistsActionEnum.Error, Unicode = true, SaveToDisk = true }; _options = new Options { UseGivenCredentials = false, ThrowErrorOnFailure = true, GetResultAsByteArray = true }; var contentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"TestFiles\ModelDocument.json"); var contentDefinition = File.ReadAllText(contentPath); _content = new DocumentContent { ContentJson = contentDefinition }; }
/// <summary> /// Creates PDF document from given content. See https://github.com/CommunityHiQ/Frends.Community.PdfFromTemplate /// </summary> /// <param name="outputFile"></param> /// <param name="content"></param> /// <param name="options"></param> /// <returns>Object { bool Success, string FileName, byte[] ResultAsByteArray }</returns> public static Output CreatePdf([PropertyTab] FileProperties outputFile, [PropertyTab] DocumentContent content, [PropertyTab] Options options) { try { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); DocumentDefinition docContent = JsonConvert.DeserializeObject <DocumentDefinition>(content.ContentJson); var document = new Document(); if (!string.IsNullOrWhiteSpace(docContent.Title)) { document.Info.Title = docContent.Title; } if (!string.IsNullOrWhiteSpace(docContent.Author)) { document.Info.Author = docContent.Author; } PageSetup.GetPageSize(docContent.PageSize.ConvertEnum <PageFormat>(), out Unit width, out Unit height); var section = document.AddSection(); SetupPage(section.PageSetup, width, height, docContent); // index for stylename var elementNumber = 0; // add page elements foreach (var pageElement in docContent.DocumentElements) { var styleName = $"style_{elementNumber}"; var style = document.Styles.AddStyle(styleName, "Normal"); switch (pageElement.ElementType) { case ElementTypeEnum.Paragraph: SetFont(style, ((ParagraphDefinition)pageElement).StyleSettings); SetParagraphStyle(style, ((ParagraphDefinition)pageElement).StyleSettings, false); AddTextContent(section, ((ParagraphDefinition)pageElement).Text, style); break; case ElementTypeEnum.Image: AddImage(section, (ImageDefinition)pageElement, width); break; case ElementTypeEnum.Table: SetFont(style, ((TableDefinition)pageElement).StyleSettings); SetParagraphStyle(style, ((TableDefinition)pageElement).StyleSettings, true); AddTable(section, (TableDefinition)pageElement, width, style); break; case ElementTypeEnum.PageBreak: section = document.AddSection(); SetupPage(section.PageSetup, width, height, docContent); break; default: 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 (outputFile.SaveToDisk) { if (!options.UseGivenCredentials) { pdfRenderer.PdfDocument.Save(fileName); } else { var domainAndUserName = GetDomainAndUserName(options.UserName); using (Impersonation.LogonUser(domainAndUserName[0], domainAndUserName[1], options.Password, LogonType.NewCredentials)) { pdfRenderer.PdfDocument.Save(fileName); } } } byte[] resultAsBytes = null; if (options.GetResultAsByteArray) { using (MemoryStream stream = new MemoryStream()) { pdfRenderer.PdfDocument.Save(stream, false); resultAsBytes = stream.ToArray(); } } return(new Output { Success = true, FileName = fileName, ResultAsByteArray = resultAsBytes }); } catch (Exception ex) { if (options.ThrowErrorOnFailure) { throw; } return(new Output { Success = false, ErrorMessage = ex.Message }); } }