当前位置: 首页 > news >正文

深圳网站建设开发/电商平台怎么运营的

深圳网站建设开发,电商平台怎么运营的,做网站怎么添加图片,免费网站域名注册申请pdf(Portable Document Format的简称,意为“便携式文档格式”),是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。iText 是java和C#中的一个处理PDF的开源类库,国外的大牛已经把它移植到Android上…

pdf(Portable Document Format的简称,意为“便携式文档格式”),是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。

iText 是java和C#中的一个处理PDF的开源类库,国外的大牛已经把它移植到Android上了,但是直接拿来用还是需要花费一点功夫,下面就用一个简单的demo来测试一下。

本文主要介绍在Android开发中如何利用iText生成PDF。

iText项目地址:

4c2a8575cb378e3f11b1352c6086f7d8.png

首先用过svn把代码check下来。

c2e561bcbf7e1cbb25d4b080bf4a26b7.png

得到三个文件夹,droidText是一个android的库工程,droidTextTest是测试工程。

在eclipse中导入droidText项目。这是个library project,后面创建的项目需要引用到。

然后创建一个Android工程-iTextTest

在工程中引用droidText:

Project->properties->Android->LIbrary:ADD

09dee6d2ff4af5b9a8099f118c7d75f6.png

链接好之后就像上图。

主界面就一个Button,按下之后就开始生产PDF。

package com.example.itexttest;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.PrintStream;

import java.lang.reflect.Method;

import android.os.Bundle;

import android.os.Environment;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class ITextActivity extends Activity {

private Button mButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_itext);

mButton = (Button)findViewById(R.id.button1);

mButton.setOnClickListener(new OnClickListenerImpl());

}

private class OnClickListenerImpl implements View.OnClickListener

{

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

//Toast.makeText(getApplicationContext(), "Run", Toast.LENGTH_SHORT).show();

// Create droidtext directory for storing results

File file = new File(

android.os.Environment.getExternalStorageDirectory()

+ File.separator + "iTextTest");

if (!file.exists()) {

file.mkdir();

}

System.out.println("Click!");

Thread t = new Thread() {

public void run() {

int success = 0;

int count = 1;

String className = "com.example.itexttest.HelloWprld";

String result = null;

try {

// Set output streams to bytearray streams so we can

// display the output of examples

ByteArrayOutputStream bos = new ByteArrayOutputStream();

PrintStream errorStream = new PrintStream(bos, true);

System.setErr(errorStream);

ByteArrayOutputStream bos2 = new ByteArrayOutputStream();

PrintStream outStream = new PrintStream(bos2, true);

System.setOut(outStream);

// Find the main method

Class c = Class.forName(className);

Method main = c.getDeclaredMethod("main",String[].class);

System.out.println("GetMain"+main.getName());

// Emulate CLI parameters if necessary

String[] params = null;

if (className

.equals("com.lowagie.examples.objects.tables.pdfptable.FragmentTable")) {

params = new String[] { "3" };

} else if (className

.equals("com.lowagie.examples.objects.images.tiff.OddEven")) {

params = new String[] { "odd.tif", "even.tif",

"odd_even.tiff" };

} else if (className

.equals("com.lowagie.examples.objects.images.tiff.Tiff2Pdf")) {

params = new String[] { "tif_12.tif" };

} else if (className

.equals("com.lowagie.examples.objects.images.DvdCover")) {

params = new String[] { "dvdcover.pdf", "Title",

"0xff0000", "hitchcock.png" };

} else if (className

.equals("com.lowagie.examples.forms.ListFields")) {

params = new String[] {};

} else if (className

.equals("com.lowagie.examples.general.read.Info")) {

params = new String[] { "RomeoJuliet.pdf" };

} else if (className

.equals("com.lowagie.examples.objects.anchors.OpenApplication")) {

params = new String[] { "" };

}

main.invoke(null, (Object) params);

// Parse results

String string = new String(bos.toByteArray());

String string2 = new String(bos2.toByteArray());

if (string.length() > 0) {

result = "Failed: " + string;

} else if (string2.contains("Exception")) {

result = "Failed: " + string2;

} else if ("Images.pdf" != null) {

File pdf = new File(

Environment.getExternalStorageDirectory()

+ File.separator + "iTextTest"

+ File.separator

+ "Images.pdf");

System.out.println("Create Pdf@");

if (!pdf.exists()) {

result = "Failed: Resulting pdf didn't get created";

} else if (pdf.length() <= 0) {

result = "Failed: Resulting pdf is empty";

} else {

success++;

result = "Successful";

}

} else {

success++;

result = "Successful";

}

} catch (Exception e) {

result = "Failed with exception: "

+ e.getClass().getName() + ": "

+ e.getMessage();

System.out.println(result);

}

if (result.startsWith("Failed")) {

System.out.println("Failed!");

} else {

System.out.println("Success!");

}

System.out.println(result);

}

};

t.start();

}

}

}

OnClick里面的代码有点小复杂,要用的的话直接粘就可以了,注意修改相应的变量名,classname对应对就是操作itext生产pdf的类。

在包里面再创建两个测试类:

HelloWorld.java

package com.example.itexttest;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

/**

* Generates a simple 'Hello World' PDF file.

*

* @author blowagie

*/

public class HelloWorld {

/**

* Generates a PDF file with the text 'Hello World'

*

* @param args

* no arguments needed here

*/

public static void main(String[] args) {

System.out.println("Hello World");

// step 1: creation of a document-object

Document document = new Document();

try {

// step 2:

// we create a writer that listens to the document

// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "iTextTest" + java.io.File.separator + "HelloWorld.pdf"));

// step 3: we open the document

document.open();

// step 4: we add a paragraph to the document

document.add(new Paragraph("Hello World"));

} catch (DocumentException de) {

System.err.println(de.getMessage());

} catch (IOException ioe) {

System.err.println(ioe.getMessage());

}

// step 5: we close the document

document.close();

}

}

生产Pdf如下:

04105957c4c2d24a86047e7eab83e9df.png

Rotating.java(创建图片,并旋转)

注意再sdcard的根目录里面放一张图片,改名jxk_run.png。

/*

* $Id: Rotating.java 3373 2008-05-12 16:21:24Z xlv $

*

* This code is part of the 'iText Tutorial'.

* You can find the complete tutorial at the following address:

* http://itextdocs.lowagie.com/tutorial/

*

* This code is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*

* itext-questions@lists.sourceforge.NET

*/

package com.example.itexttest;

import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import com.example.itexttest.R;

import com.example.itexttest.ITextActivity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Image;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

/**

* Rotating images.

*/

public class Rotating {

/**

* Rotating images.

*

* @param args

* No arguments needed

*/

public static void main(String[] args) {

System.out.println("Rotating an Image");

// step 1: creation of a document-object

Document document = new Document();

try {

// step 2:

// we create a writer that listens to the document

// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "iTextTest" + java.io.File.separator + "rotating.pdf"));

// step 3: we open the document

document.open();

// step 4: we add content

//Can't use filename => use byte[] instead

// Image jpg4 = Image.getInstance("otsoe.jpg");

ByteArrayOutputStream stream = new ByteArrayOutputStream();

//Bitmap bitmap = BitmapFactory.decodeResource(ITextActivity.getActivity().getResources(), R.drawable.otsoe);

Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/jxk_run.png");

bitmap.compress(Bitmap.CompressFormat.JPEG /* FileType */,100 /* Ratio */, stream);

Image jpg = Image.getInstance(stream.toByteArray());

jpg.setAlignment(Image.MIDDLE);

jpg.setRotation((float) Math.PI / 6);

document.add(new Paragraph("rotate 30 degrees"));

document.add(jpg);

document.newPage();

jpg.setRotation((float) Math.PI / 4);

document.add(new Paragraph("rotate 45 degrees"));

document.add(jpg);

document.newPage();

jpg.setRotation((float) Math.PI / 2);

document.add(new Paragraph("rotate pi/2 radians"));

document.add(jpg);

document.newPage();

jpg.setRotation((float) (Math.PI * 0.75));

document.add(new Paragraph("rotate 135 degrees"));

document.add(jpg);

document.newPage();

jpg.setRotation((float) Math.PI);

document.add(new Paragraph("rotate pi radians"));

document.add(jpg);

document.newPage();

jpg.setRotation((float) (2.0 * Math.PI));

document.add(new Paragraph("rotate 2 x pi radians"));

document.add(jpg);

} catch (DocumentException de) {

System.err.println(de.getMessage());

} catch (IOException ioe) {

System.err.println(ioe.getMessage());

}

// step 5: we close the document

document.close();

}

}

生产PDF如下:

ebcec7e1165c0b0f7f18d59202ffe4f4.png

在Android开发中利用iText生成PDF整个过程步骤简单,但主要是代码解释,因此,有兴趣操作的同学要仔细看清代码,理解代码。

http://www.lbrq.cn/news/1283221.html

相关文章:

  • 网站网站开发的公司/百度快照在哪里
  • 宁波做网站nbyckj/安卓神级系统优化工具
  • wordpress更改首页代码/青山seo排名公司
  • 辽宁做网站找谁/百度客服人工
  • 网站可以做库存吗/信息流优化师培训机构
  • 彩票网站建设哪家公司好/今日新闻内容摘抄
  • 菜鸟教程网站/做教育培训应该注册什么公司
  • 临淄哪里做网站/怎样做品牌推广
  • 无锡网站维护/深圳营销策划公司十强
  • 网站上的小动画咋做/友情链接网自动收录
  • 网站验证码系统/注册一个公司网站需要多少钱
  • 全套做网站/抖音推广方案
  • 天津市武清区网站建设/一般的电脑培训班要多少钱
  • 做服务的网站/百度搜索量怎么查
  • 网站建设便宜的公司哪家好/网站推广引流最快方法
  • 谷歌做网站推广/微信营销
  • 代码高亮网站/seo网络推广培训班
  • 网站制作多少钱方案/推广普通话手抄报内容简短
  • 做小说网站做国外域名还是国内的好/网搜网
  • 有哪些网站建设工作/新网站怎么快速收录
  • 黄石做网站的公司/2023国内外重大新闻事件10条
  • 做雕塑网站/集团网站推广
  • 快速提高关键词排名的软件/seo是什么东西
  • wordpress登录页样式美化/windows7系统优化工具
  • 自贡市网站建设/国内搜索引擎
  • 网站开发的目的意义/kol营销
  • 怎么做网站卖产品/怎样搭建自己的网站
  • 网站权重6了该则么做优化方案/做竞价推广这个工作怎么样
  • 重庆双八自助建设网站/百度导航2023年最新版
  • 怎么做网站赌博/企业品牌策划
  • Rust赋能土木工程数字化
  • SpringBoot总结
  • 简单实现支付密码的页面及输入效果
  • 机器学习(一)KNN,K近邻算法(K-Nearest Neighbors)
  • 图书推荐-由浅入深的大模型构建《从零构建大模型》
  • Langchain学习——PromptTemplate