第一个pdf页面的大小并不像在liferay中使用itext的其他pdf页面那个样大
我的需求是开发一个应用程序,从用户表中读取数据,该表是liferay默认数据库lportal中的默认表之一,并以pdf格式发布该数据。 每件事都很好,但当我试图增加页面大小时,除了第一页,所有其他页面的大小都会根据我的需要增加。我不知道为什么会发生这种情况,为什么只有第一页的大小没有增加。 我的动作类代码是第一个pdf页面的大小并不像在liferay中使用itext的其他pdf页面那个样大,pdf,liferay,itext,liferay-6,Pdf,Liferay,Itext,Liferay 6,我的需求是开发一个应用程序,从用户表中读取数据,该表是liferay默认数据库lportal中的默认表之一,并以pdf格式发布该数据。 每件事都很好,但当我试图增加页面大小时,除了第一页,所有其他页面的大小都会根据我的需要增加。我不知道为什么会发生这种情况,为什么只有第一页的大小没有增加。 我的动作类代码是 public class pdfPortlet extends MVCPortlet { public void serveResource(ResourceRequest r
public class pdfPortlet extends MVCPortlet
{
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException
{
if(req.isUserInRole("administrator"))
{
try
{
adminMethod(req,res);
}
catch (PortalException e)
{
e.printStackTrace();
}
catch (SystemException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
else
{
userMethod(req,res);
}
}
/********** adminMethod which can fetch the whole user data from user_ table
* @throws SystemException
* @throws PortalException
* @throws DocumentException
* @throws IOException **********/
public void adminMethod(ResourceRequest req, ResourceResponse res) throws PortalException, SystemException, DocumentException, IOException
{
int numOfUser=UserLocalServiceUtil.getUsersCount(); //get the number of users
String remoteUserId = req.getRemoteUser(); //getting current user id
Integer io=Integer.parseInt(remoteUserId); //getting current userid
int currentUserId=io.intValue(); //typecasting from Integer to primitive int
User tblUser = UserLocalServiceUtil.getUser(currentUserId); //will return an object containing element of each row and hence by iterating this element we can get number of columns
System.out.println("@@@@@@ tbluser: "+tblUser);
req.setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document document = new com.lowagie.text.Document(); //blank pdf created
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos); //predefine class PdfWriter calls static method getInstance
//document.setPageSize(new Rectangle(2000, 2600));
document.open(); //open pdf in write method
PdfPTable table= new PdfPTable(39); //creating a pdf table having 39 columns
document.setPageSize(new Rectangle(2000, 2600));
table.setWidthPercentage(100);
/* float[] columnWidths = new float[39];
for(int i=0;i<39;i++)
columnWidths[i]=10f;
table.setWidths(columnWidths);*/
table.addCell("uuid_");
table.addCell("userId");
table.addCell("companyId");
table.addCell("createDate");
table.addCell("modifiedDate");
table.addCell("defaultUser");
table.addCell("contactId");
table.addCell("password_");
table.addCell("passwordEncrypted");
table.addCell("passwordReset");
table.addCell("passwordModifiedDate");
table.addCell("digest");
table.addCell("reminderQueryQuestion");
table.addCell("reminderQueryAnswer");
table.addCell("graceLoginCount");
table.addCell("screenName");
table.addCell("emailAddress");
table.addCell("facebookId");
table.addCell("openId");
table.addCell("portraitId");
table.addCell("languageId");
table.addCell("timeZoneId");
table.addCell("greeting");
table.addCell("comments");
table.addCell("firstName");
table.addCell("middleName");
table.addCell("lastName");
table.addCell("jobTitle");
table.addCell("loginDate");
table.addCell("loginIP");
table.addCell("lastLoginDate");
table.addCell("lastLoginIP");
table.addCell("lastFailedLoginDate");
table.addCell("failedLoginAttempts");
table.addCell("lockout");
table.addCell("lockoutDate");
table.addCell("agreedToTermsOfUse");
table.addCell("emailAddressVerified");
table.addCell("status");
List<User> totalUsers=UserLocalServiceUtil.getUsers(0,numOfUser); //fetch whole table in our object
Iterator<User> it=totalUsers.iterator();
while(it.hasNext())
{
User eachRow=(User) it.next();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String str=null;
java.util.Date jDate=null;
table.addCell(eachRow.getUuid());
table.addCell(eachRow.getUserId()+"");
table.addCell(eachRow.getCompanyId()+"");
jDate=eachRow.getCreateDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
jDate=eachRow.getModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDefaultUser()+"");
table.addCell(eachRow.getContactId()+"");
table.addCell(eachRow.getPassword());
table.addCell(eachRow.getPasswordEncrypted()+"");
table.addCell(eachRow.getPasswordReset()+"");
jDate=eachRow.getPasswordModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDigest());
Iterator itr=(eachRow.getReminderQueryQuestions()).iterator();
Object obj=itr.next();
str=(String)obj;
table.addCell(str);
table.addCell(eachRow.getReminderQueryAnswer());
table.addCell(eachRow.getGraceLoginCount()+"");
table.addCell(eachRow.getScreenName());
table.addCell(eachRow.getEmailAddress());
table.addCell(eachRow.getFacebookId()+"");
table.addCell(eachRow.getOpenId());
table.addCell(eachRow.getPortraitId()+"");
table.addCell(eachRow.getLanguageId());
table.addCell(eachRow.getTimeZoneId());
table.addCell(eachRow.getGreeting());
table.addCell(eachRow.getComments());
table.addCell(eachRow.getFirstName());
table.addCell(eachRow.getMiddleName());
table.addCell(eachRow.getLastName());
table.addCell(eachRow.getJobTitle());
jDate=eachRow.getLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLoginIP());
jDate=eachRow.getLastLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLastLoginIP());
jDate=eachRow.getLastFailedLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getFailedLoginAttempts()+"");
table.addCell(eachRow.getLockout()+"");
jDate=eachRow.getLockoutDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getAgreedToTermsOfUse()+"");
table.addCell(eachRow.getEmailAddressVerified()+"");
table.addCell(eachRow.getStatus()+"");
}//end of wile
document.add(table); //adding table to created pdf document
document.close(); //we have to first close the document
String fileName="attachment;filename=ashraf.pdf"; //filename
res.setContentType("application/pdf"); //setting the content type either application or pdf(Portable Document Format)
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName); //
OutputStream out = res.getPortletOutputStream();
byte[] downloadBytes = Base64.decode((String) req.getAttribute("fileToDownloadBase64"));
out.write(downloadBytes);
baos.writeTo(out);
out.flush();
out.close();
} //adminMethod
公共类pdfPortlet扩展了MVCPortlet
{
public void服务器资源(ResourceRequest-req、ResourceResponse-res)
抛出PortletException,IOException
{
如果(请求isUserInRole(“管理员”))
{
尝试
{
管理方法(req、res);
}
捕获(PortalException e)
{
e、 printStackTrace();
}
捕获(系统异常e)
{
e、 printStackTrace();
}
捕获(文档异常)
{
e、 printStackTrace();
}
}
其他的
{
用户方法(req、res);
}
}
/**********adminMethod,它可以从用户表中获取整个用户数据
*@SystemException
*@PortalException
*@DocumentException
*@抛出异常**********/
public void adminMethod(ResourceRequest req、ResourceResponse res)抛出PortalException、SystemException、DocumentException、IOException
{
int numOfUser=UserLocalServiceUtil.getuserscont();//获取用户数
字符串remoteUserId=req.getRemoteUser();//获取当前用户id
Integer io=Integer.parseInt(remoteUserId);//获取当前用户ID
int currentUserId=io.intValue();//从整数到基元int的类型转换
User tblUser=UserLocalServiceUtil.getUser(currentUserId);//将返回一个包含每行元素的对象,因此通过迭代该元素,我们可以得到列数
System.out.println(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;
请求setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document Document=新建com.lowagie.text.Document();//已创建空白pdf
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
getInstance(document,baos);//预定义类PdfWriter调用静态方法getInstance
//setPageSize(新矩形(20002600));
document.open();//用write方法打开pdf
PdfPTable=newpdfptable(39);//创建一个包含39列的pdf表
setPageSize(新矩形(20002600));
表1.设定宽度百分比(100);
/*float[]列宽=新的float[39];
对于(int i=0;i首先我问一个问题,然后我给出答案。在这篇文章中,我的目的是分享我的经验,我面临的问题是什么,以及我如何摆脱这个问题。
在我的问题中,我说我的应用程序中唯一的问题是第一页的大小并不像我提到的那个样大,其他页面都是根据我的命令。
现在的解决方案就是在document.open()之前设置页面大小并解决问题
public class pdfPortlet extends MVCPortlet
{
public void serveResource(ResourceRequest req, ResourceResponse res)
throws PortletException, IOException
{
if(req.isUserInRole("administrator"))
{
try
{
adminMethod(req,res);
}
catch (PortalException e)
{
e.printStackTrace();
}
catch (SystemException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
else
{
userMethod(req,res);
}
}
/********** adminMethod which can fetch the whole user data from user_ table
* @throws SystemException
* @throws PortalException
* @throws DocumentException
* @throws IOException **********/
public void adminMethod(ResourceRequest req, ResourceResponse res) throws PortalException, SystemException, DocumentException, IOException
{
int numOfUser=UserLocalServiceUtil.getUsersCount(); //get the number of users
String remoteUserId = req.getRemoteUser(); //getting current user id
Integer io=Integer.parseInt(remoteUserId); //getting current userid
int currentUserId=io.intValue(); //typecasting from Integer to primitive int
User tblUser = UserLocalServiceUtil.getUser(currentUserId); //will return an object containing element of each row and hence by iterating this element we can get number of columns
System.out.println("@@@@@@ tbluser: "+tblUser);
req.setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document document = new com.lowagie.text.Document(); //blank pdf created
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos); //predefine class PdfWriter calls static method getInstance
document.setPageSize(new Rectangle(2000, 2600));//"this is the difference form my previous code here I use this function before the document.open();
document.open(); //open pdf in write method
PdfPTable table= new PdfPTable(39); //creating a pdf table having 39 columns
//document.setPageSize(new Rectangle(2000, 2600));
table.setWidthPercentage(100);
/* float[] columnWidths = new float[39];
for(int i=0;i<39;i++)
columnWidths[i]=10f;
table.setWidths(columnWidths);*/
table.addCell("uuid_");
table.addCell("userId");
table.addCell("companyId");
table.addCell("createDate");
table.addCell("modifiedDate");
table.addCell("defaultUser");
table.addCell("contactId");
table.addCell("password_");
table.addCell("passwordEncrypted");
table.addCell("passwordReset");
table.addCell("passwordModifiedDate");
table.addCell("digest");
table.addCell("reminderQueryQuestion");
table.addCell("reminderQueryAnswer");
table.addCell("graceLoginCount");
table.addCell("screenName");
table.addCell("emailAddress");
table.addCell("facebookId");
table.addCell("openId");
table.addCell("portraitId");
table.addCell("languageId");
table.addCell("timeZoneId");
table.addCell("greeting");
table.addCell("comments");
table.addCell("firstName");
table.addCell("middleName");
table.addCell("lastName");
table.addCell("jobTitle");
table.addCell("loginDate");
table.addCell("loginIP");
table.addCell("lastLoginDate");
table.addCell("lastLoginIP");
table.addCell("lastFailedLoginDate");
table.addCell("failedLoginAttempts");
table.addCell("lockout");
table.addCell("lockoutDate");
table.addCell("agreedToTermsOfUse");
table.addCell("emailAddressVerified");
table.addCell("status");
List<User> totalUsers=UserLocalServiceUtil.getUsers(0,numOfUser); //fetch whole table in our object
Iterator<User> it=totalUsers.iterator();
while(it.hasNext())
{
User eachRow=(User) it.next();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String str=null;
java.util.Date jDate=null;
table.addCell(eachRow.getUuid());
table.addCell(eachRow.getUserId()+"");
table.addCell(eachRow.getCompanyId()+"");
jDate=eachRow.getCreateDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
jDate=eachRow.getModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDefaultUser()+"");
table.addCell(eachRow.getContactId()+"");
table.addCell(eachRow.getPassword());
table.addCell(eachRow.getPasswordEncrypted()+"");
table.addCell(eachRow.getPasswordReset()+"");
jDate=eachRow.getPasswordModifiedDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getDigest());
Iterator itr=(eachRow.getReminderQueryQuestions()).iterator();
Object obj=itr.next();
str=(String)obj;
table.addCell(str);
table.addCell(eachRow.getReminderQueryAnswer());
table.addCell(eachRow.getGraceLoginCount()+"");
table.addCell(eachRow.getScreenName());
table.addCell(eachRow.getEmailAddress());
table.addCell(eachRow.getFacebookId()+"");
table.addCell(eachRow.getOpenId());
table.addCell(eachRow.getPortraitId()+"");
table.addCell(eachRow.getLanguageId());
table.addCell(eachRow.getTimeZoneId());
table.addCell(eachRow.getGreeting());
table.addCell(eachRow.getComments());
table.addCell(eachRow.getFirstName());
table.addCell(eachRow.getMiddleName());
table.addCell(eachRow.getLastName());
table.addCell(eachRow.getJobTitle());
jDate=eachRow.getLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLoginIP());
jDate=eachRow.getLastLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getLastLoginIP());
jDate=eachRow.getLastFailedLoginDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getFailedLoginAttempts()+"");
table.addCell(eachRow.getLockout()+"");
jDate=eachRow.getLockoutDate();
if(jDate!=null)
{
str=df.format(jDate);
System.out.println("str password modified date: "+str);
str=df.format(jDate);
}
else
str="NA";
table.addCell(str);
table.addCell(eachRow.getAgreedToTermsOfUse()+"");
table.addCell(eachRow.getEmailAddressVerified()+"");
table.addCell(eachRow.getStatus()+"");
}//end of wile
document.add(table); //adding table to created pdf document
document.close(); //we have to first close the document
String fileName="attachment;filename=ashraf.pdf"; //filename
res.setContentType("application/pdf"); //setting the content type either application or pdf(Portable Document Format)
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName); //
OutputStream out = res.getPortletOutputStream();
byte[] downloadBytes = Base64.decode((String) req.getAttribute("fileToDownloadBase64"));
out.write(downloadBytes);
baos.writeTo(out);
out.flush();
out.close();
} //adminMethod
公共类pdfPortlet扩展了MVCPortlet
{
public void服务器资源(ResourceRequest-req、ResourceResponse-res)
抛出PortletException,IOException
{
如果(请求isUserInRole(“管理员”))
{
尝试
{
管理方法(req、res);
}
捕获(PortalException e)
{
e、 printStackTrace();
}
捕获(系统异常e)
{
e、 printStackTrace();
}
捕获(文档异常)
{
e、 printStackTrace();
}
}
其他的
{
用户方法(req、res);
}
}
/**********adminMethod,它可以从用户表中获取整个用户数据
*@SystemException
*@PortalException
*@DocumentException
*@抛出异常**********/
public void adminMethod(ResourceRequest req、ResourceResponse res)抛出PortalException、SystemException、DocumentException、IOException
{
int numOfUser=UserLocalServiceUtil.getuserscont();//获取用户数
字符串remoteUserId=req.getRemoteUser();//获取当前用户id
Integer io=Integer.parseInt(remoteUserId);//获取当前用户ID
int currentUserId=io.intValue();//从整数到基元int的类型转换
User tblUser=UserLocalServiceUtil.getUser(currentUserId);//将返回一个包含每行元素的对象,因此通过迭代该元素,我们可以得到列数
System.out.println(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@;
请求setCharacterEncoding(StringPool.UTF8);
com.lowagie.text.Document Document=新建com.lowagie.text.Document();//已创建空白pdf
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
getInstance(document,baos);//预定义类PdfWriter调用静态方法getInstance
document.setP