Esempio n. 1
0
            public async Task <Table> CreateTable(Model.PageContentItem item)
            {
                if (item?.Rows == null)
                {
                    return(null);
                }

                HorizontalAlignment defaultContentHorizontalAlignment = item.DefaultContentHorizontalAlignment?.ToEnum(HorizontalAlignment.None) ?? HorizontalAlignment.None;
                VerticalAlignment   defaultContentVerticalAlignment   = item.DefaultContentVerticalAlignment?.ToEnum(VerticalAlignment.None) ?? VerticalAlignment.None;
                List <Row>          rows = new List <Row>();

                foreach (var r in item.Rows)
                {
                    List <Cell> cells = new List <Cell>();
                    foreach (var c in r.Cells)
                    {
                        Cell                 cell       = null;
                        List <TextRect>      paragraphs = new List <TextRect>();
                        List <ImageFragment> images     = new List <ImageFragment>();
                        if (!string.IsNullOrEmpty(c.Url))
                        {
                            await using (var imageStream = new MemoryStream())
                            {
                                var imageInfo = await fetchImage(new Uri(c.Url), imageStream);

                                images.Add(new ImageFragment(ImageFile: imageInfo.FilePath, FixWidth: imageInfo.Width, FixHeight: imageInfo.Height));
                            }
                        }
                        if (!string.IsNullOrEmpty(c.Text))
                        {
                            paragraphs.Add(new TextRect(HttpUtility.HtmlDecode(c.Text)));
                        }
                        if (c.TextLines != null && c.TextLines.Count > 0)
                        {
                            paragraphs.AddRange(c.TextLines.Select(l => new TextRect(HttpUtility.HtmlDecode(l))));
                        }
                        //if (paragraphs.Count == 0)
                        //    paragraphs.Add(new TextRect(""));


                        cell = new Cell(Paragraphs: paragraphs, Images: images)
                        {
                            DefaultCellTextState = GetFontState(c.Font),
                            RowSpan           = c.RowSpan,
                            ColSpan           = c.ColSpan,
                            Alignment         = c.HorizontalAlignment?.ToEnum(HorizontalAlignment.None) ?? defaultContentHorizontalAlignment,
                            VerticalAlignment = c.VerticalAlignment?.ToEnum(VerticalAlignment.None) ?? defaultContentVerticalAlignment,
                            //BackgroundImageFile = "c:\\_\\code_img.png"
                        };

                        if (c.Border != null)
                        {
                            cell.Border = BorderInfo(new[] { c.Border, item.Border });
                        }
                        if (c.Margin != null)
                        {
                            cell.Margin = MarginInfo(c?.Margin, item?.DefaultContentMargin);
                        }

                        cells.Add(cell);
                    }
                    rows.Add(new Row(Cells: cells));
                }
                var   max_cells = item.Rows.Select(c => c.Cells.Count).Max();
                Table table     = new Table(Rows: rows);

                if (item?.DefaultContentMargin != null)
                {
                    table.DefaultCellPadding = MarginInfo(item?.DefaultContentMargin);
                }
                if (item?.ColumnWidths != null && item?.ColumnWidths.Count > 0)
                {
                    table.ColumnWidths = string.Join(" ", item?.ColumnWidths);
                }
                else
                {
                    table.DefaultColumnWidth = "100";
                }
                table.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;
                //table.Left = 20;
                //table.Top = 50;
                //table.ColumnWidths = string.Join(' ', Enumerable.Repeat(Convert.ToInt32(((_dims.MaxX /*- 30*/) / max_cells)).ToString(), max_cells));
                if (item.Border != null)
                {
                    table.DefaultCellBorder = BorderInfo(item.Border);
                }
                return(table);
            }
Esempio n. 2
0
            internal async Task AddContentItem(int pageNumber, Model.PageContentItem contentItem)
            {
                if (contentItem == null)
                {
                    throw new ArgumentException($"empty content item");
                }
                if (!string.IsNullOrEmpty(contentItem?.Text) || contentItem?.TextLines?.Count > 0)
                {
                    if (contentItem.Location == null || (contentItem?.Location?.Empty() ?? false))
                    {
                        throw new ArgumentException($"no location for ${contentItem?.Text}");
                    }
                    var ha   = contentItem?.HorizontalAlignment?.ToEnum(TextHorizontalAlignment.Left) ?? TextHorizontalAlignment.Left;
                    var va   = contentItem?.VerticalAlignment?.ToEnum(VerticalAlignment.None) ?? VerticalAlignment.None;
                    var wrap = contentItem?.WrapMode?.ToEnum(WrapMode.NoWrap) ?? WrapMode.NoWrap;

                    List <Segment> segments = new List <Segment>();
                    if (!string.IsNullOrEmpty(contentItem?.Text))
                    {
                        segments.Add(new Segment(Value: HttpUtility.HtmlDecode(contentItem?.Text), TextState: GetFontState(contentItem?.Font)));
                    }
                    if (contentItem?.TextLines?.Count > 0)
                    {
                        segments.AddRange(contentItem?.TextLines.Select(s => new Segment(Value: HttpUtility.HtmlDecode(s), TextState: GetFontState(contentItem?.Font))));
                    }

                    var paragraph = new Paragraph(Lines: new List <TextLine>()
                    {
                        new TextLine(Segments: segments)
                    },
                                                  Rectangle: Rect(contentItem.Location), HorizontalAlignment: ha, WrapMode: wrap, VerticalAlignment: va);
                    await _taskMeasurer.Run(() => _pdfApi.PutAddTextAsync(pageNumber: pageNumber, paragraph: paragraph, name: FileName, storage: Storage, folder: Folder)
                                            , "050_AddText");
                }
                else if (!string.IsNullOrEmpty(contentItem?.Url))
                {
                    if (contentItem.Location == null || (contentItem?.Location?.Empty() ?? false))
                    {
                        throw new ArgumentException($"no location for ${contentItem.Url}");
                    }

                    await using (var imageStream = new MemoryStream())
                    {
                        var imageInfo = await fetchImage(new Uri(contentItem.Url), imageStream);

                        if (imageStream.Length > 0 || !string.IsNullOrEmpty(imageInfo.FilePath))
                        {
                            imageStream.Position = 0;

                            await _taskMeasurer.Run(() => _pdfApi.PostInsertImageAsync(pageNumber: pageNumber
                                                                                       , llx: Convert.ToInt32(Math.Truncate(Llx(contentItem.Location).Value))
                                                                                       , lly: Convert.ToInt32(Math.Truncate(Lly(contentItem.Location).Value))
                                                                                       , urx: Convert.ToInt32(Math.Truncate(Urx(contentItem.Location).Value))
                                                                                       , ury: Convert.ToInt32(Math.Truncate(Ury(contentItem.Location).Value))
                                                                                       , imageFilePath: imageInfo.FilePath
                                                                                       , image: imageStream.Length > 0 ? imageStream : null
                                                                                       , name: FileName, storage: Storage, folder: Folder)
                                                    , "060_InsertImage");
                        }
                    }
                }
                else if (contentItem?.Rows != null)
                {
                    await _taskMeasurer.RunSync(async() => await _pdfApi.PostPageTablesAsync(pageNumber: pageNumber, tables: new List <Table>()
                    {
                        await CreateTable(contentItem)
                    }
                                                                                             , name: FileName, storage: Storage, folder: Folder)
                                                , "070_AddPageTables");
                }
            }