首页 > 其他分享 >CSharp: UglyToad.PdfPig int .net8

CSharp: UglyToad.PdfPig int .net8

时间:2024-01-26 18:12:06浏览次数:22  
标签:int builder UglyToad System net8 PdfPig using page

 

/*
 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());
         }

  

标签:int,builder,UglyToad,System,net8,PdfPig,using,page
From: https://www.cnblogs.com/geovindu/p/17990393

相关文章

  • Springcloud学习笔记61---Spring MVC的拦截器HandlerInterceptor
    1. HandlerMethod介绍HandlerMethod它作为SpringMVC的非公开API,可能绝大多数小伙伴都对它比较陌生,但我相信你对它又不是那么的生疏,因为你可能没用过但肯定见过。比如SpringMVC的拦截器HandlerInterceptor的拦截方法的第三个入参Objecthandler,虽然它是Object类型,但其实绝大部......
  • OpenHarmony—使用继承而非intersection type
    使用继承而非intersectiontype规则:arkts-no-intersection-types级别:错误目前ArkTS不支持intersectiontype,可以使用继承作为替代方案。TypeScriptinterfaceIdentity{id:numbername:string}interfaceContact{email:stringphoneNumber:string}type......
  • winter 2024 day2
    2023中国大学生程序设计竞赛(CCPC)新疆赛区(重现赛H数学思路:有四平方和定理知道,任意正整数可表示为不超过四个整数的平方和。 并且n的范围为1e5,可以枚举出f(x)值为1、2、3、4的平方数组合情况。也可以dp,f[i]=min(f[i],f[i-k*k]+1)#include<bits/stdc++.h>usingnamespacestd......
  • vue print.js 打印 此处打印不包含页面的页码 (打印方法二)
    <template><divclass="modalContainerprintAsset"ref="modalContainer"><divv-for="(items,index)intableDataPrint":key=indexstyle="page-break-after:always;zoom:1"ref="show......
  • idea maven sprint boot打包常见问题
    ideaSpringBoot多模块打包(Unabletofindmainclass和父子依赖打包的错误) 1、子模块打包没有main方法就不要用spring-boot-maven-plugin来build了,因为它是需要main方法才能打包的<!--使用springboot的maven插件会报找不到主类错误--><build><plugins>......
  • JAVA学习笔记--使用Inte IDEA
    使用IntellijIDEA编写代码新建项目创建新项目选择创建一个空项目并输入项目名弹出ProjecStructure窗口先关闭新建一个模板(Module)并输入模板名打开前面关闭的ProjecStructure窗口修改以下信息(注意:安装的是JDK8则按照以下信息修改,若安装的是JDK其他版本则......
  • winter 2024 day3
    SMUWinter2024round1AB.SumofMedians思路:贪心的想,只有中位数有贡献,并且知道了中位数的位置以及中位数左边的数的个数l和中位数右边的数的个数r,那么对于一个不递减的数组,要取出最大的中位数,即取出l个最小的数和r个最大的数,中位数即为第r+1大的数。直到数取完为止。......
  • 《SAIS Supervising and Augmenting Intermediate Steps for Document-Level Relation
    代码 原文地址 预备知识:1.什么是标记索引(tokenindices)?标记索引是一种用于表示文本中的单词或符号的数字编码。它们可以帮助计算机理解和处理自然语言。例如,假如有一个字典{"我":1,"是":2,"Bing":3,".":4},那么文本"我是Bing."的标记索引就是[1,2,3,4]。不同的模......
  • webstorm报错:ESLint: TypeError: this.libOptions.parse is not a function
     解决办法:把 {**/*,*}.{js,ts,jsx,tsx,html,vue}换成{**/*,*}.(js,ts,jsx,tsx,html,vue) 原文:https://stackoverflow.com/questions/73509984/eslint-typeerror-this-liboptions-parse-is-not-a-functionMyfriends,ifyouareusing Webstorm orany Jetbrains p......
  • vue print.js 批量打印功能
    批量打印 :1.用到print.js  自行安装  安装完成后 引用  importprintJSfrom'print-js';2.用到深拷贝深拷贝代码:(可以将此代码放在一个页面中,对此进行引用即可,例如:放在until文件中,引用代码 import{deepClone}from'@/utils/index')exportfunctiondeepClone......