pdf增值税发票如何获取里面的发票编号

news/2024/9/23 2:29:08/

第一种直接读取pdf文件获取里面文字

第二种将pdf转成图片识别里面的二维码,获取调用百度图片识别接口。

 

二维码识别依赖

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
        </dependency>

pdf读取所需依赖

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jempbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jempbox</artifactId>
            <version>1.8.16</version>
        </dependency>

 

/**
     * 
     * pdf转图片
     * 
     * @author 
     */
    public static void pdfFileToImage()
    {
        // pdf文件
        File pdffile = new File("C:/Users/Luo-ping/Desktop/顺丰电子发票.pdf");
        // 转成的 png 文件存储全路径及文件名
        String targetPath = "D:/test.png";
        try
        {
            FileInputStream instream = new FileInputStream(pdffile);
            InputStream byteInputStream = null;

            PDDocument doc = PDDocument.load(instream);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            if (pageCount > 0)
            {
                BufferedImage image = renderer.renderImage(0, 2.0f);
                image.flush();
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imOut;
                imOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(image, "png", imOut);
                byteInputStream = new ByteArrayInputStream(bs.toByteArray());
                byteInputStream.close();
            }

            File uploadFile = new File(targetPath);
            FileOutputStream fops;
            fops = new FileOutputStream(uploadFile);
            fops.write(readInputStream(byteInputStream));
            fops.flush();
            fops.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static byte[] readInputStream(InputStream inStream) throws Exception
    {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1)
        {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }

    /**
     * 
     * 识别图中二维码
     * 
     * @author 
     * @return
     */
    public static String extractImages()
    {
        String filename = "D:/test.png";
        String returnResult = "";
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        File file = new File(filename);
        try
        {
            BufferedImage image = ImageIO.read(file);
            // 定义二维码参数
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            // 获取读取二维码结果
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            Result result = null;
            result = multiFormatReader.decode(binaryBitmap, hints);
            returnResult = result.getText();
            System.err.println(returnResult);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return returnResult;
    }

 

 

直接读取pdf

/**
     * 读PDF文件,使用了pdfbox开源项目
     * 
     * @param fileName
     * 
     */
    public static void readPDF(String fileName)
    {
        File file = new File(fileName);
        FileInputStream in = null;
        try
        {
            in = new FileInputStream(fileName);
            // 新建一个PDF解析器对象
            PDFParser parser = new PDFParser(new RandomAccessFile(file, "rw"));
            // 对PDF文件进行解析
            parser.parse();
            // 获取解析后得到的PDF文档对象
            PDDocument pdfdocument = parser.getPDDocument();
            // 新建一个PDF文本剥离器
            PDFTextStripper stripper = new PDFTextStripper();
            // 从PDF文档对象中剥离文本
            String result = stripper.getText(pdfdocument);
            FileWriter fileWriter = new FileWriter(new File("pdf.txt"));
            fileWriter.write(result);
            fileWriter.flush();
            fileWriter.close();
            System.out.println("PDF文件的文本内容如下:");
            System.out.println(result);

        }
        catch (Exception e)
        {
            System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败!" + e);
            e.printStackTrace();
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e1)
                {
                }
            }
        }
    }

 

 


http://www.ppmy.cn/news/690795.html

相关文章

发票代码规则

一、增值税专用发票的分类代码 根据国税函发[1995]18号文件规定&#xff0c;增值税专用发票的分类代码用10位阿拉伯数字表示。各位数字分别代表地区简称、制版年度、批次、版本的语言文字、几联发票、发票的金额版本号等。 具体表示方法&#xff1a; 1、第l&#xff0d;4位 …

发票代码的含义(专,普)

文章目录 国税函[1995]18号 国家税务总局关于统一编印1995年增值税专用发票代码的通知国税函[2004]521号 国家税务总局关于统一全国普通发票分类代码和发票号码的通知 其他原文地址 这是个好问题。 目前比较有影响力的通知有2个&#xff1a; 国税函[1995]18号 专票 国税函[2004…

发票代码和发票号码知识点

知识点&#xff1a;发票代码和发票号码每一项无法单独标识一张发票&#xff0c;只有这两个组合之后才能标识一张发票。 如图&#xff1a; 下面资料介绍发票代码和发票号码的组成&#xff1a; 引用地址&#xff1a;http://www.chinatax.gov.cn/n810341/n810765/n812193/n813008/…

VScode当中下载liveServer后无法正常打开浏览器进行查看

一、直接使用“http://127.0.0.1:5500/”进行访问 二、通过修改环境变量 1、对liveServer插件的下载进行确认&#xff1b; 2、尝试重新下载liveServer插件&#xff1b; 3、对电脑Path环境变量进行修改 &#xff08;1&#xff09;打开“此电脑” &#xff08;2&#xff09…

如何在 macOS Monterey 上使用Live Text

在iPhone上用Live text从图片中提取文本似乎是最常用的功能&#xff0c;但在macOS Monterey上&#xff0c;你有成千上万张照片等着你去用。 苹果的Live Text演示显示&#xff0c;如果你在拍照&#xff0c;你的iPhone或Mac现在可以选择其中的文本。无论是一张菜单的照片&#x…

【HTTP Live Streaming】(四)苹果公司提供的7款 hls 工具

一、目标 了解apple官方提供的工具&#xff0c;可以帮助我们细分视频流并创建成功传输所需的播放列表。 二、介绍 有几种工具可以帮助您设置HTTP Live Streaming服务&#xff0c;下面分别介绍&#xff1a; 1.Media Stream Segmenter&#xff08;mediastreamsegmenter&#xff0…

MySQL数据库复合查询

文章目录 一、多表查询二、自连接三、子查询1.单行子查询2.多行子查询3.多列子查询4.在from子句中使用子查询 四、合并查询 一、多表查询 在实际开发中&#xff0c;我们需要查询的数据往往会来自不同的表&#xff0c;所以需要进行多表查询。下面我们用一个简单的公司管理系统&…

苹果软件版测试周期,苹果推出iOS测试更新周期的第四个beta版本

原标题&#xff1a;苹果推出iOS测试更新周期的第四个beta版本 苹果于今日放出了当前软件测试更新周期的第四个beta版本&#xff0c;其中包括了iOS 9.3、IS X 10.11.4 El Capitan、watchOS 2.2、以及tvOS 9.2。苹果似乎坚持了每两周一次的发布频率&#xff0c;尽管版本编号意味着…