我正在尝试使用itext生成pdf文件。我需要在最后一页的页脚上添加一个元素(段落)。我不知道如何将元素放在那个位置。
答案 0 :(得分:2)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
void tokenize(string& str, vector<string>& tokens)
{
int pos;
string token;
while ((pos = str.find(" ")) != std::string::npos )
{
token = str.substr(0,pos);
tokens.push_back(token);
str.erase(0, pos + 1);
}
tokens.push_back(str.c_str());
}
bool isOperator(string str)
{
if((str == "+") || (str == "*") || (str == "/") )
return true;
else
return false;
}
float compute(string oper, float val1, float val2)
{
if(oper == "+")
return (val1 + val2);
else if(oper == "*")
return (val1 * val2);
else if(oper == "/")
return (val1 / val2);
else
return 0;
}
void evalPrefix(vector<string>& expression)
{
vector<float> numStack;
float num1;
float num2;
for (int i = (expression.size() - 1); i >=0; i--)
{
if(isOperator(expression[i]))
{
num1 = numStack.back();
numStack.pop_back();
num2 = numStack.back();
numStack.pop_back();
numStack.push_back(compute(expression[i], num1, num2));
}
else
{
numStack.push_back(atoi(expression[i].c_str()));
}
}
int i = int (numStack[0] + 0.5);
cout << i << endl;
}
int main(int argc, char *argv[])
{
ifstream file(argv[1]);
string line;
string token;
vector<string> tokens;
while (getline(file, line)) //processing the file
{
if(line.length() == 0)
continue;
else
{
tokens.clear();
tokenize(line, tokens); //tokenizing the file
if(tokens.size())
evalPrefix(tokens);
}
}
return 0;
}
并将标题装饰器添加到页面事件
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class PdfHeaderDecorator extends PdfPageEventHelper
{
public PdfHeaderDecorator()
{
super();
}
public void onEndPage(PdfWriter writer, Document document)
{
PdfPTable tableF = new PdfPTable(3);
try
{
tableF.setWidths(new int[]
{ 24, 24, 2 });
tableF.setTotalWidth(527);
tableF.setLockedWidth(true);
tableF.getDefaultCell().setFixedHeight(9);
tableF.getDefaultCell().setBorder(Rectangle.BOTTOM);
tableF.addCell(new Phrase("SOME Text"));
tableF.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
tableF.addCell(new Phrase(String.format("page %d ", writer.getPageNumber())));
PdfPCell cell = new PdfPCell(new Phrase("bla bla bla"));
cell.setBorder(Rectangle.BOTTOM);
tableF.addCell(cell);
tableF.writeSelectedRows(0, -1, 34, 30, writer.getDirectContent());
}
catch (DocumentException de)
{
throw new ExceptionConverter(de);
}
}
}