【问题标题】:Issue Aligning Text with the Image Using MigraDoc使用 MigraDoc 将文本与图像对齐的问题
【发布时间】:2016-09-19 11:34:50
【问题描述】:

我有这种格式的标题--

标题”“图片”“标题”

以下是我用来实现此目的的代码 sn-p -

    Paragraph header = section.Headers.Primary.AddParagraph("Heading");            
    header.Format.Font.Bold = true;
    header.AddTab();
    Image image = header.AddImage("../../Images/logo.png");
    image.Height = Unit.FromMillimeter(6);
    header.AddFormattedText("Title", TextFormat.NotBold);

我需要对齐我的“图像”和“标题”,使标题相对于图像的高度垂直居中对齐,我该如何实现?

非常感谢任何指针/代码 sn-p。

【问题讨论】:

    标签: c# pdf-generation migradoc


    【解决方案1】:

    您可以使用表格将所有信息放入特定结构中:

    // create document
    Document MigraDokument = new Document();
    
    // create section. 
    Section section = MigraDokument.AddSection();            
    section.PageSetup.PageFormat = PageFormat.A4;
    
    // create a table
    Table t = section.AddTable();
    // size to use for the image and the image cell in the table
    int size = 6;
    
    // create 3 columns
    Column column_header = t.AddColumn("6cm");
    column_header.Format.Alignment = ParagraphAlignment.Center;
    
    Column column_image = t.AddColumn(Unit.FromMillimeter(size));
    column_image.Format.Alignment = ParagraphAlignment.Center;
    
    Column column_text = t.AddColumn("4cm");
    column_text.Format.Alignment = ParagraphAlignment.Center;
    
    // Add 1 row to fill it with the content
    Row r = t.AddRow();
    
    // add you Header
    Paragraph header = r.Cells[0].AddParagraph("Heading");
    header.Format.Font.Bold = true;
    header.AddTab();
    
    // add the image            
    Image image = r.Cells[1].AddImage("../../logo.png"); 
    image.Height = Unit.FromMillimeter(size);
    
    // Add your Title
    r.Cells[2].AddParagraph("Title");
    
    // allign all of them
    r.Cells[0].VerticalAlignment = VerticalAlignment.Center;
    r.Cells[1].VerticalAlignment = VerticalAlignment.Center;
    r.Cells[2].VerticalAlignment = VerticalAlignment.Center;
    

    在我的文档中,结果如下所示:

    【讨论】:

    • 谢谢,该方法有助于获得所需的结果,因此我将接受作为答案,如果不使用表格和使用“图像”的一些对齐属性和“段落文本”(如果有)
    【解决方案2】:

    感谢@MongZhu 推荐表格方式,贴出我现在使用的代码sn-p,仅供参考。

            Table table = section.Headers.Primary.AddTable();
            table.AddColumn("11cm");
            table.AddColumn("2cm");
            table.AddColumn("8cm");
    
            Row row = table.AddRow();
            row.VerticalAlignment = VerticalAlignment.Center;
            Paragraph header = row.Cells[0].AddParagraph("Heading");
            header.Format.Font.Bold = true;                        
            Image image = row.Cells[1].AddImage("../../Images/logo.png");
            image.Height = Unit.FromMillimeter(6);
            row.Cells[2].AddParagraph("Title");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-21
      • 2011-01-14
      • 2021-11-09
      • 2016-06-19
      • 2023-03-14
      • 2012-08-25
      相关资源
      最近更新 更多