使用iText将图像添加到PDF [英] Add Image to a PDF using iText

查看:176
本文介绍了使用iText将图像添加到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText将图像添加到PDF文件.这些文件从.TXT转换为PDF,并应该添加图像:

I am trying to add an Image to a PDF file using iText. The files are converted from .TXT to PDF and an image is supposed to be added:

//Convert Reports to PDF
File conv = new File("Report Forms/");
String[] filesconv = conv.list(only);
for (int c = 0; c < filesconv.length; c++) {
    PdfWriter writer = null;
    try {
        Document output = new Document(PageSize.B4);
        Rectangle page = output.getPageSize();
        page.setBackgroundColor(new java.awt.Color(255, 255, 255));
        FileInputStream fs = new FileInputStream("Report Forms/" + filesconv[c]);
        FileOutputStream file = new FileOutputStream(new File("Report Forms/" + name + " " + exa + " FORM " + level + " " + year + ".pdf"));
        DataInputStream in =new DataInputStream(fs);
        BufferedReader br = new BufferedReader(new InputStreamReader( in ));

        writer = PdfWriter.getInstance(output, file);
        writer.createXmpMetadata();

        //define the  font for the disclaimer.
        com.lowagie.text.Font fontfooter = new
        com.lowagie.text.Font(com.lowagie.text.Font.COURIER, 12, com.lowagie.text.Font.BOLD);
        fontfooter.setColor(new java.awt.Color(0, 52, 154));

        output.newPage();
        writer.setPageEvent(new CustomBorder());
        output.open();
        writer.open();

        FileInputStream fsname = new FileInputStream("Report Forms/" + filesconv[c]);
        BufferedReader brname = new BufferedReader(new InputStreamReader(fsname));
        for (int p = 0; p < 0; p++) {
            brname.readLine();
        }
        String custname = brname.readLine();
        // System.out.println(custname);
        com.lowagie.text.Font f1 = new com.lowagie.text.Font(com.lowagie.text.Font.COURIER, 12, com.lowagie.text.Font.BOLD);
        f1.setColor(Color.BLACK);
        output.add(new Paragraph(new Phrase(custname, f1)));
        Image image = Image.getInstance("Student Images/" + num + ".png");
        image.setAlignment(Image.ALIGN_RIGHT);
        image.setAbsolutePosition(450f, 10f);
        image.scalePercent(60, 50);
        Chunk chunk = new Chunk(image, 0, -20);
        HeaderFooter header = new HeaderFooter(new Phrase(chunk), false);
        header.setAlignment(Element.ALIGN_RIGHT);
        header.setBorder(Rectangle.NO_BORDER);
        output.setHeader(header);

        String line = "";
        int lineNo = br.read();
        while ((line = br.readLine()) != null) {
            for (lineNo = 0; br.ready(); lineNo++) {
                if (lineNo % 2 == 1) {
                    line = br.readLine();
                    com.lowagie.text.Font f2 = new com.lowagie.text.Font(com.lowagie.text.Font.COURIER, 12, com.lowagie.text.Font.BOLD);
                    f2.setColor(Color.BLACK);
                    Paragraph p1 = new Paragraph(line, f2);
                    p1.setAlignment(Element.TABLE);
                    PdfPTable table = new PdfPTable(1);
                    table.setWidthPercentage(100);
                    PdfPCell cell = new PdfPCell();
                    cell.setBackgroundColor(new java.awt.Color(247, 246, 243));
                    cell.setBorder(Rectangle.NO_BORDER);
                    cell.setUseBorderPadding(true);
                    cell.setPadding(0);
                    cell.setBorderColor(new java.awt.Color(247, 246, 243));
                    cell.addElement(p1);
                    table.addCell(cell);
                    output.add(table);

                } else {
                    line = br.readLine();
                    com.lowagie.text.Font f2 = new com.lowagie.text.Font(com.lowagie.text.Font.COURIER, 12, com.lowagie.text.Font.BOLD);
                    f2.setColor(new java.awt.Color(0, 52, 154));
                    Paragraph p1 = new Paragraph(line, f2);
                    p1.setAlignment(Element.TABLE);
                    PdfPTable table = new PdfPTable(1);
                    table.setWidthPercentage(100);
                    PdfPCell cell = new PdfPCell();
                    cell.setBorder(Rectangle.NO_BORDER);
                    cell.setBorderWidthBottom(1f);

                    cell.setUseBorderPadding(true);
                    cell.setPadding(0);
                    cell.setBorderColor(new java.awt.Color(255, 255, 255));
                    cell.addElement(p1);
                    table.addCell(cell);
                    // output.add(page);
                    output.add(table);
                }
            }
        }
        output.close();
        file.close();
        fs.close();
        fsname.close(); in .close();
        writer.close();
    } catch(Exception ce) {
        JOptionPane.showMessageDialog(this, ce, "Error", JOptionPane.PLAIN_MESSAGE);
    }
}

如果我不添加任何图像,则转换进行得很好,但是当我添加图像时,将以0KB生成PDF文件,并且当您尝试打开该文件时,会出现错误,因为文件已损坏,无法打开该文件. /p>

If I am not adding any image the conversion is happening fine but when I add image the PDF files are getting generated with 0KB and when you try to open, error comes that the file could not be opened because it is dammaged.

推荐答案

这可以帮助您将图像以及图像标题添加到pdf.

May this help you for adding image and also image caption to pdf.

Image image1 = Image.getInstance("sample.png");
image1.setAlignment(Element.ALIGN_CENTER);
image1.scaleAbsolute(450, 250);
//Add to document
document.add(image1);
Paragraph imgCaption = new Paragraph(" Sample image-1", imgcaption);
Summary.setAlignment(Element.ALIGN_LEFT);
Summary.setSpacingAfter(10f);
document.add(imgCaption);

这篇关于使用iText将图像添加到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆