CSharp: UglyToad.PdfPig in donet 8.0

 

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 IDE: VS 2022 17.5
 OS: windows 10
 .net: 8.0
  生成PDF文档,从PDF文档中获取文字内容  控制台下测试
 */
 
// See https://aka.ms/new-console-template for more information
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UglyToad.PdfPig;
using UglyToad.PdfPig.AcroForms;
using UglyToad.PdfPig.AcroForms.Fields;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Outline;
using System.IO;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.Fonts.Standard14Fonts;
using UglyToad.PdfPig.Fonts.SystemFonts;
using UglyToad.PdfPig.Writer;
using System.Drawing;
using System.Drawing.Text;
 
 
 
        Console.WriteLine("Hello,CSharp World! Geovin Du,geovindu, 涂聚文\n\t");
 
        try {
 
            PdfDocumentBuilder builder = new PdfDocumentBuilder();
 
            //string fontfile = Server.MapPath("fonts/MHeiHK-Light.TTF");
            //byte[] robotoBytes = File.ReadAllBytes(fontfile);
            // PdfDocumentBuilder.AddedFont MHeiHK = builder.AddTrueTypeFont(robotoBytes);
 
            // 读取宋体字体文件到字节数组 中文必须是中文字体,相应文字语言,用相关的字体   simsunb.ttf
            byte[] simSunFontBytes;
            using (FileStream fontFileStream = File.OpenRead("C:\\Windows\\Fonts\\STSONG.TTF"))
            {
                simSunFontBytes = new byte[fontFileStream.Length];
                fontFileStream.Read(simSunFontBytes, 0, simSunFontBytes.Length);
            }
 
 
 
 
 
            // 添加支持中文的字体 
            PdfDocumentBuilder.AddedFont font = builder.AddTrueTypeFont(simSunFontBytes);
 
 
 
 
 
            PdfDocumentBuilder.AddedFont helvetica = builder.AddStandard14Font(Standard14Font.Helvetica);
            PdfDocumentBuilder.AddedFont helveticaBold = builder.AddStandard14Font(Standard14Font.HelveticaBold);
 
            //  PdfDocumentBuilder.AddedFont song = builder.AddStandard14Font(Standard14Font.simsunb);
 
 
            PdfPageBuilder page = builder.AddPage(PageSize.A4);
 
            PdfPoint closeToTop = new PdfPoint(15, page.PageSize.Top - 25);
 
            page.AddText("My first PDF document!", 12, closeToTop, helvetica);
 
            page.AddText("Hello CSharp World!,Geovin Du!", 10, closeToTop.Translate(0, -15), helveticaBold);
 
            page = builder.AddPage(PageSize.A4);
 
            page.AddText("geovindu!", 12, closeToTop, helvetica); //中文用中文系统字体
 
            page = builder.AddPage(PageSize.A4);
 
 
            //写入
            page.AddText("你好,这是一个PDF文档。涂聚文欢迎你!", 12, new PdfPoint(25, 520), font);
 
            //byte[] b = builder.Build();
 
            string fiel = "file.pdf";
            File.WriteAllBytes(fiel, builder.Build());
            Console.WriteLine("文档生成ok\n\t");
            //从PDF文件中读取文字内容
            string fileout ="1.pdf";
            using (PdfDocument document = PdfDocument.Open(fileout))
            {
                foreach (UglyToad.PdfPig.Content.Page pagedu in document.GetPages())
                {
                    IEnumerable<Word> words = pagedu.GetWords();
                    foreach (Word word in words)
                    {
                        Console.WriteLine(word.Text);
                    }
                }
            }
            Console.WriteLine("\n\t从PDF文件中读取文字内容ok");
 
 
 
         }
         catch(Exception ex)
         {
             Console.WriteLine(ex.Message.ToString());
         }

  


https://github.com/BobLd/PdfPig/tree/table-extractor-2
https://github.com/kba/hocr-spec
https://github.com/kba/hocrjs

 

Concurrency in .NET
https://github.com/rikace/fConcBook
https://dotnetcurry.com/dotnet/1360/concurrent-programming-dotnet-core
https://www.csharptutorial.net/csharp-concurrency/
https://www.oreilly.com/library/view/concurrency-in-net/9781617292996/
https://blog.christian-schou.dk/blog/concurrency-vs-parallelism-vs-asynchronous/

Concurrency in C++
https://www.codeproject.com/Articles/1271904/Programming-Concurrency-in-Cplusplus-Part-1
https://www.codeproject.com/Articles/1278737/Programming-Concurrency-in-Cplusplus-Part-2
https://www.modernescpp.org/wp-content/uploads/2023/04/Concurrency.pdf
https://www.codeproject.com/Tips/5376066/Solving-Fizz-Buzz-in-Csharp-and-Cplusplus
https://www.classes.cs.uchicago.edu/archive/2013/spring/12300-1/labs/lab6/


concurrency in Java

 

https://github.com/RadekKoubsky/java-concurrency-in-practice-examples
https://github.com/LeonardoZ/java-concurrency-patterns

concurrency in python
https://stackabuse.com/concurrency-in-python/
https://github.com/ro6ley/python-concurrency-example

 

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 IDE: VS 2022 17.6
 OS: windows 10
 .NET 8.0
  FROM https://github.com/BobLd/PdfPig
 https://github.com/UglyToad/PdfPig/wiki/Document-Layout-Analysis
https://github.com/UglyToad/PdfPig/issues/617
 
 */
 
 
namespace ConsoleAppPdfDemo
{
 
 
 
    using UglyToad.PdfPig.Content;
    using UglyToad.PdfPig.Core;
    using UglyToad.PdfPig.Fonts.Standard14Fonts;
    using UglyToad.PdfPig.Writer;
    using UglyToad.PdfPig;
    using UglyToad.PdfPig.DocumentLayoutAnalysis.TableExtractor;
    using System.Diagnostics;
    //using static System.Net.Mime.MediaTypeNames;
    using System.Drawing;
    using System.Net;
 
 
    /// <summary>
    ///
    /// </summary>
    internal class Program
    {
        private static double cmToPdfUnits(double cm) => cm / 2.54 * 72;
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
 
            Console.WriteLine("Hello,CSharp World! Geovin Du,geovindu, 涂聚文\n\t");
              
            try
            {
 
                PdfDocumentBuilder builder = new PdfDocumentBuilder();
 
                //string fontfile = Server.MapPath("fonts/MHeiHK-Light.TTF");
                //byte[] robotoBytes = File.ReadAllBytes(fontfile);
                // PdfDocumentBuilder.AddedFont MHeiHK = builder.AddTrueTypeFont(robotoBytes);
 
                // 读取宋体字体文件到字节数组 中文必须是中文字体,相应文字语言,用相关的字体   simsunb.ttf
                byte[] simSunFontBytes;
                using (FileStream fontFileStream = File.OpenRead("C:\\Windows\\Fonts\\STSONG.TTF"))
                {
                    simSunFontBytes = new byte[fontFileStream.Length];
                    fontFileStream.Read(simSunFontBytes, 0, simSunFontBytes.Length);
                }
 
 
                string baseurl = Environment.CurrentDirectory.ToString() + "\\";
 
 
                // 添加支持中文的字体 
                PdfDocumentBuilder.AddedFont font = builder.AddTrueTypeFont(simSunFontBytes);
 
 
 
 
 
                PdfDocumentBuilder.AddedFont helvetica = builder.AddStandard14Font(Standard14Font.Helvetica);
                PdfDocumentBuilder.AddedFont helveticaBold = builder.AddStandard14Font(Standard14Font.HelveticaBold);
 
                //  PdfDocumentBuilder.AddedFont song = builder.AddStandard14Font(Standard14Font.simsunb);
 
                //第1页
                PdfPageBuilder page = builder.AddPage(PageSize.A4);
 
                PdfPoint closeToTop = new PdfPoint(15, page.PageSize.Top - 25);
 
                page.AddText("My first PDF document!言语成了邀功尽责的功臣,还需要行为每日值班吗?", 12, closeToTop, font);
 
                page.AddText("Hello CSharp World!,Geovin Du!涂聚文,geovindu", 10, closeToTop.Translate(0, -15), font);
 
                var imgstream = new FileStream(baseurl+ @"images\logo.jpg", FileMode.Open);
 
                var imgX = cmToPdfUnits(2.5);
                var imgY = cmToPdfUnits(14);
                var imgWidth = cmToPdfUnits(16);
                var imgHeight = cmToPdfUnits(12);
                page.AddJpeg(imgstream, new PdfRectangle(imgX, imgY, imgX + imgWidth, imgY + imgHeight)); //.jpg
 
 
                //第二页
                page = builder.AddPage(PageSize.A4);
 
                page.AddText("geovindu!", 12, new PdfPoint(15, 815), font); //中文用中文系统字体  845
                page.AddText("励学篇", 12, new PdfPoint(15, 800), font);
                page.AddText("宋  赵恒", 12, new PdfPoint(15, 785), font);
                page.AddText("富家不用买良田,书中自有千钟粟。", 12, new PdfPoint(15, 770), font);
                page.AddText("安居不用架高堂,书中自有黄金屋。", 12, new PdfPoint(15, 755), font);
                page.AddText("出门莫恨无人随,书中车马多如簇。", 12, new PdfPoint(15, 740), font);
                page.AddText("娶妻莫恨无良媒,书中自有颜如玉。", 12, new PdfPoint(15, 725), font);
                page.AddText("男儿欲遂平生志,五经勤向窗前读。", 12, new PdfPoint(15, 710), font);
                page.AddText("", 12, new PdfPoint(15, 695), font);
                page.AddText("", 12, new PdfPoint(15, 780), font);
                page.AddText("", 12, new PdfPoint(15, 765), font);
 
                
 
                //第3页
                page = builder.AddPage(PageSize.A4);
 
 
                //写入
                page.AddText("你好,这是一个PDF文档。涂聚文欢迎你!", 12, new PdfPoint(25, 520), font);
 
                //byte[] b = builder.Build();
 
                string fiel = "geovindu" + DateTime.Now.ToString("yyyyMMHHmmss") + ".pdf";
                File.WriteAllBytes(fiel, builder.Build());
                Console.WriteLine("文档生成ok\n\t");
 
 
                //从PDF文件中读取文字内容
                string fileout = "1.pdf";
                using (PdfDocument document = PdfDocument.Open(fileout))
                {
                    foreach (UglyToad.PdfPig.Content.Page pagedu in document.GetPages())
                    {
                        IEnumerable<Word> words = pagedu.GetWords();
                        foreach (Word word in words)
                        {
                            Console.WriteLine(word.Text);
                        }
                    }
                }
                Console.WriteLine("\n\t从PDF文件中读取文字内容ok");
 
 
                //預覽文件
                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(fiel)
                    {
                        UseShellExecute = true
                    }
                };
 
                process.Start();
                process.WaitForExit();
 
 
 
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}

  

https://github.com/BobLd/PdfPig/tree/table-extractor-2
https://github.com/BobLd/PdfPig/tree/table-extractor

 

posted @   ®Geovin Du Dream Park™  阅读(219)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2023-01-26 CSharp: Add,Edit,Del,Select in donet using Entity Framework
2022-01-26 CSharp: itext7.* create pdf file
< 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
点击右上角即可分享
微信分享提示