2个回答
展开全部
需要用到一个pdf的jar包,去网上下载一个itextpdf.jar。导出pdf的格式都是需要自己用代码实现的,每一行 ,每一个列是什么格式,都需要自己写出来,是不能够自动生成的,反正很麻烦。我做的一个导出成pdf特定格式的东西,写了好几千行代码。相当头痛。
package com.dw.mqs.export;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dw.file.WebHelper;
import com.dw.mqs.MqsManager;
import com.dw.mqs.MqsUtilNew;
import com.dw.mqs.ProductItem;
import com.dw.mqs.ProjectBasicItem;
import com.dw.mqs.ProjectConfDetailItem;
import com.dw.mqs.ProjectConfVerItem;
import com.dw.mqs.ProjectConfig;
import com.dw.mqs.ProjectService;
import com.dw.mqs.Util;
import com.dw.system.Convert;
import com.dw.system.gdb.DBResult;
import com.dw.system.gdb.DataRow;
import com.dw.system.gdb.GDB;
import com.dw.user.User;
import com.dw.user.UserManager;
import com.dw.user.UserProfile;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PdfService
{
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter pdf = PdfWriter.getInstance(document, os);
Rectangle rect = new Rectangle(36, 54, 559, 788);
pdf.setBoxSize("art", rect);
pdf.setPageEvent(new TableHeader());
document.open();
BaseFont baseArialuni = BaseFont.createFont("res/ARIALUNI.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // Arial
// unicode字体
Font fontCN = new Font(baseArialuni, 9, Font.NORMAL, Color.BLACK);
Font fontCN8b = new Font(baseArialuni, 9, Font.BOLD, Color.BLACK);
Font fontCN12b = new Font(baseArialuni, 12, Font.BOLD, Color.BLACK);
Font fontCN9b = new Font(baseArialuni, 10, Font.BOLD, Color.BLACK);
Font fontCN9 = new Font(baseArialuni, 10, Font.NORMAL, Color.BLACK);
Font fontCN9b_blue = new Font(baseArialuni, 10, Font.BOLD, Color.BLUE);
PdfPTable table = null;
PdfPCell cell = null;
table = new PdfPTable(relativeWidths);
table.setWidthPercentage(100);
cell = new PdfPCell(new Paragraph(tit + "标题", fontCN12b));
cell.setColspan(root ? 12 : 10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 垂直居中
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
}
package com.dw.mqs.export;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dw.file.WebHelper;
import com.dw.mqs.MqsManager;
import com.dw.mqs.MqsUtilNew;
import com.dw.mqs.ProductItem;
import com.dw.mqs.ProjectBasicItem;
import com.dw.mqs.ProjectConfDetailItem;
import com.dw.mqs.ProjectConfVerItem;
import com.dw.mqs.ProjectConfig;
import com.dw.mqs.ProjectService;
import com.dw.mqs.Util;
import com.dw.system.Convert;
import com.dw.system.gdb.DBResult;
import com.dw.system.gdb.DataRow;
import com.dw.system.gdb.GDB;
import com.dw.user.User;
import com.dw.user.UserManager;
import com.dw.user.UserProfile;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PdfService
{
Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter pdf = PdfWriter.getInstance(document, os);
Rectangle rect = new Rectangle(36, 54, 559, 788);
pdf.setBoxSize("art", rect);
pdf.setPageEvent(new TableHeader());
document.open();
BaseFont baseArialuni = BaseFont.createFont("res/ARIALUNI.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // Arial
// unicode字体
Font fontCN = new Font(baseArialuni, 9, Font.NORMAL, Color.BLACK);
Font fontCN8b = new Font(baseArialuni, 9, Font.BOLD, Color.BLACK);
Font fontCN12b = new Font(baseArialuni, 12, Font.BOLD, Color.BLACK);
Font fontCN9b = new Font(baseArialuni, 10, Font.BOLD, Color.BLACK);
Font fontCN9 = new Font(baseArialuni, 10, Font.NORMAL, Color.BLACK);
Font fontCN9b_blue = new Font(baseArialuni, 10, Font.BOLD, Color.BLUE);
PdfPTable table = null;
PdfPCell cell = null;
table = new PdfPTable(relativeWidths);
table.setWidthPercentage(100);
cell = new PdfPCell(new Paragraph(tit + "标题", fontCN12b));
cell.setColspan(root ? 12 : 10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 垂直居中
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
}
追问
那每个页面都要自己手写导出pdf代码,是不是太烦了啊!还有使用itextpdf加页眉页脚怎么搞啊!求大师帮忙啊
追答
页眉页脚没有弄过。我觉得主要就是布局的问题吧,pdf其实就是一个没有任何格式的空白界面。里面的东西都得你自己实现,你不需要的页面和页脚都抽出来,写成两个方法,然后在插入到每页里面
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java操作pdf咯,有外部插件支持的咯,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询