如何生成与图像在android的PDF文件?图像、文件、android、PDF

2023-09-12 04:12:43 作者:☆完美少年☆

我可以使用的iText 生成Android应用程序的PDF文件,因此生成的PDF文件,但是, 的图像不包括的PDF文件格式。

解决方案

 进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.net.URL;
进口java.util.Date;

进口com.itextpdf.text.Anchor;
进口com.itextpdf.text.BadElementException;
进口com.itextpdf.text.BaseColor;
进口com.itextpdf.text.Chapter;
进口com.itextpdf.text.Document;
进口com.itextpdf.text.DocumentException;
进口com.itextpdf.text.Element;
进口com.itextpdf.text.Font;
进口com.itextpdf.text.Image;
进口com.itextpdf.text.List;
进口com.itextpdf.text.ListItem;
进口com.itextpdf.text.Paragraph;
进口com.itextpdf.text.Phrase;
进口com.itextpdf.text.Section;
进口com.itextpdf.text.pdf.PdfImportedPage;
进口com.itextpdf.text.pdf.PdfPCell;
进口com.itextpdf.text.pdf.PdfPTable;
进口com.itextpdf.text.pdf.PdfReader;
进口com.itextpdf.text.pdf.PdfWriter;

进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Environment;
进口android.widget.TextView;

公共类主要扩展活动{

    私有静态字符串文件= Environment.getExternalStorageDirectory()+文件分割符+firstPdf.pdf;
    私有静态字体catFont =新的字体(Font.FontFamily.TIMES_ROMAN,18,
            Font.BOLD);
    私有静态字体redFont =新的字体(Font.FontFamily.TIMES_ROMAN,12,
            Font.NORMAL,BaseColor.RED);
    私有静态字体subFont =新的字体(Font.FontFamily.TIMES_ROMAN,16,
            Font.BOLD);
    私有静态字体smallBold =新的字体(Font.FontFamily.TIMES_ROMAN,12,
            Font.BOLD);
    TextView的TXT1;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        TXT1 =(TextView中)findViewById(R.id.textView1);

        尝试 {
            文献文件=新的文件();
            PdfWriter.getInstance(文件,新的FileOutputStream(文件));
            document.open();
            addMetaData(文件);
            addTitlePage(文件);
            addContent(文件);
            //的createImage();
            document.close();

        }赶上(例外五){
            e.printStackTrace();
        }
    }

    私有静态无效addMetaData(文档文件){
        document.addTitle(我的第一个PDF);
        document.addSubject(使用iText的);
        document.addKeywords(爪哇,PDF格式,iText的);
        document.addAuthor(拉尔斯·沃格尔);
        document.addCreator(拉尔斯·沃格尔);
    }

    私有静态无效addTitlePage(文档文件)
            抛出DocumentException {
        第preface =新的第();
        //我们添加一个空行
        addEmptyLine(preface,1);
        //让我们写一个很大的标题
        preface.add(新段(下称文件的标题,catFont));

        addEmptyLine(preface,1);
        //将创建:_name,_date:报告生成
        preface.add(新段(
                报告生成:+ System.getProperty(user.name)+,+新的Date()// $ NON-NLS-1 $ // $ NON-NLS-2 $ // $ NON- NLS-3 $
                smallBold));
        addEmptyLine(preface,3);
        preface.add(新段(
                本文档介绍的东西,这是非常重要的,
                smallBold));

        addEmptyLine(preface,8);

        preface.add(新段(
                这份文件是preliminary版本,而不是按照您的许可协议或与vogella.de ;-)任何其他协议。
                redFont));

        document.add(preface);
        //开始了新的一页
        document.newPage();
    }

    私有静态无效addContent(文档文件)抛出DocumentException {
        锚索锚具=新主播(评估程序,catFont);
        anchor.setName(估计APP);

        //第二个参数是本章的数量
        第二章的CATPart =新章(新段(锚),1);

        段分段=新段(子类别1,subFont);
        第subCatPart = catPart.addSection(分段);
        subCatPart.add(新段(你好));

        分段=新段(子类别2,subFont);
        subCatPart = catPart.addSection(分段);
        subCatPart.add(新段(第1款));
        subCatPart.add(新段(第2款));
        subCatPart.add(新段(第3款));

        //添加列表
        createList(subCatPart);
        逐段=新的第();
        addEmptyLine(段落,5);
        subCatPart.add(段);

        //添加表
        CREATETABLE(subCatPart);

        //现在加入这一切的文档
        document.add(CATPart中);

        //下一节
        锚=新主播(第二章,catFont);
        anchor.setName(第二章);

        //第二个参数是本章的数量
        的CATPart =新章(新段(锚),1);

        分段=新段(子类别,subFont);
        subCatPart = catPart.addSection(分段);
        subCatPart.add(新段(这是一个非常重要的信息));

        //现在加入这一切的文档
        document.add(CATPart中);

    }

    私有静态无效CREATETABLE(第subCatPart)
            抛出BadElementException {
        PdfPTable表=新PdfPTable(3);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell C1 =新PdfPCell(新词组(项目名称:));
        c1.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
        table.addCell(C1);

        C1 =新PdfPCell(新词(测试001));
        c1.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
        table.addCell(C1);

        C1 =新PdfPCell(新词());
        c1.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
        table.addCell(C1);
        table.setHeaderRows(1);

        table.addCell(日期:);
        table.addCell(1.1);
        table.addCell();
        table.addCell(劳动价格:);
        table.addCell(2.2);
        table.addCell();
        table.addCell(劳动力成本);
        table.addCell(3.2);
        table.addCell(3.3);

        subCatPart.add(表);

    }

    私有静态无效createList(第subCatPart){
        列表列表=新名单(真,假,10);
        list.add(新的ListItem(第一点));
        list.add(新的ListItem(第二点));
        list.add(新的ListItem(第三点));
        subCatPart.add(名单);
    }

    私有静态无效addEmptyLine(逐段,INT编号){
        的for(int i = 0; I<数;我++){
            paragraph.add(新段());
        }
    }
 

I can generate PDF file in android application using iText , so PDF document is generated but, image is not included in PDF file.

解决方案 android图片处理总结 PDF 下载

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class Main extends Activity {

    private static String FILE = Environment.getExternalStorageDirectory()+File.separator+"firstPdf.pdf";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
            Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
    TextView txt1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txt1=(TextView) findViewById(R.id.textView1);

        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            //createImage();
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private static void addTitlePage(Document document)
            throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Title of the document", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph(
                "Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph(
                "This document describes something which is very important ",
                smallBold));

        addEmptyLine(preface, 8);

        preface.add(new Paragraph(
                "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.de ;-).",
                redFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private static void addContent(Document document) throws DocumentException {
        Anchor anchor = new Anchor("ESTIMATING APP", catFont);
        anchor.setName("ESTIMATING APP");

        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), 1);

        Paragraph subPara = new Paragraph("Subcategory 1", subFont);
        Section subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("Hello"));

        subPara = new Paragraph("Subcategory 2", subFont);
        subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("Paragraph 1"));
        subCatPart.add(new Paragraph("Paragraph 2"));
        subCatPart.add(new Paragraph("Paragraph 3"));

        // Add a list
        createList(subCatPart);
        Paragraph paragraph = new Paragraph();
        addEmptyLine(paragraph, 5);
        subCatPart.add(paragraph);

        // Add a table
        createTable(subCatPart);

        // Now add all this to the document
        document.add(catPart);

        // Next section
        anchor = new Anchor("Second Chapter", catFont);
        anchor.setName("Second Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 1);

        subPara = new Paragraph("Subcategory", subFont);
        subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("This is a very important message"));

        // Now add all this to the document
        document.add(catPart);

    }

    private static void createTable(Section subCatPart)
            throws BadElementException {
        PdfPTable table = new PdfPTable(3);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Job Name:"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Test 001"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase(""));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        table.addCell("Date:");
        table.addCell("1.1");
        table.addCell("");
        table.addCell("Labor Rate:");
        table.addCell("2.2");
        table.addCell("");
        table.addCell("Labor Cost:");
        table.addCell("3.2");
        table.addCell("3.3");

        subCatPart.add(table);

    }

    private static void createList(Section subCatPart) {
        List list = new List(true, false, 10);
        list.add(new ListItem("First point"));
        list.add(new ListItem("Second point"));
        list.add(new ListItem("Third point"));
        subCatPart.add(list);
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }