posts - 133,  comments - 12,  views - 14万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

本次程序编写主要为了将pdf word等文档转换为图片后设置不同的打印排版

前提 目标文件夹中的图片高宽都是一致的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// <summary>
/// 合并图片
/// </summary>
/// <param name="savedictory">文件保存目录</param>
/// <param name="singlePageNumber">单页排版</param>
/// <param name="path">文件列表</param>
///
public static void MergedImage(string[] path, SinglePageNumber singlePageNumber,string savedictory)
{
    //获取目录路径列表
    var i = path.Length % (int)singlePageNumber > 0 ? (path.Length / (int)singlePageNumber + 1) : path.Length / (int)singlePageNumber;
    for (int s = 0; s < i; s++)
    {
        List<Bitmap> bitmapList = new List<Bitmap>();
        List<string> imgPathTake = path.Skip((s - 1) * (int)singlePageNumber).Take((int)singlePageNumber).ToList();
        foreach (var pathItem in imgPathTake)
            bitmapList.Add(new Bitmap(pathItem));
        //2=1x2 4=2x2 6=2x3 9=3x3 16=4x4
        Bitmap map = null;
        switch (singlePageNumber)
        {
            case SinglePageNumber.two:
                map = mergeImage(2, 1, bitmapList);
                break;
            case SinglePageNumber.four:
                map = mergeImage(2, 2, bitmapList);
 
                break;
            case SinglePageNumber.six:
                map = mergeImage(3, 2, bitmapList);
 
                break;
            case SinglePageNumber.nine:
                map = mergeImage(3, 3, bitmapList);
 
                break;
            case SinglePageNumber.Sixteen:
                map = mergeImage(4, 4, bitmapList);
                break;
            default:
                break;
        }
        map.Save(savedictory + "\\" + s+".png");
    }
 
 
}
/// <summary>
/// 合并图片文件 传入的图片宽高要一致
/// </summary>
/// <param name="wi">一排有几张</param>
/// <param name="hi">一列有几张</param>
/// <param name="mapList"></param>
public static Bitmap mergeImage(int wi, int hi, List<Bitmap> mapList)
{
 
    int width = mapList.First().Width;
    int height = mapList.First().Height;
    Bitmap bitmap = new Bitmap(width * wi, height * hi);
    //x表示图片在第几行y表示图片在第几列
    int mapIndex = 0;
    for (int y = 1; y <= hi; y++)
    {
        for (int x = 1; x <= wi; x++)
    {
            if (mapIndex == mapList.Count )
            {
                foreach (var item in mapList)
                    item.Dispose();
                return bitmap;
            }
            var map = mapList[mapIndex];
            mapIndex++;
            for (int w = 1; w <= width; w++)
                for (int h = 1; h <= height; h++)
                {
                    var color = map.GetPixel(w - 1, h - 1);
                    bitmap.SetPixel(((x-1)*width)+w - 1, ((y - 1) * height) + h - 1, color);
                }
 
        }
    }
    foreach (var item in mapList)
        item.Dispose();
    return bitmap;
 
}
1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// 每版打印几页 单页的不用处理
/// </summary>
public enum SinglePageNumber
 
{
    two = 2,
    four = 4,
    six = 6,
    nine = 9,
    Sixteen = 16,
}

  

  

设置图片排版为2*3的效果

1
2
3
string dictory = @"D:\test\testpdf2convert\";
       string[] path = System.IO.Directory.GetFiles(dictory);
       ImagePrintHp.MergedImage(path, SinglePageNumber.six, dictory);

  效果

2*2效果

 

posted on   xuelei被占用了  阅读(864)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示