使用获取/设置变量生成PDF [英] Generating PDF with get/set variables
本文介绍了使用获取/设置变量生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要通过iText PDF生成器将变量value1到value4显示在PDF中.当我使用该值时,它显示无法对非静态字段value1进行静态引用".我该如何解决? 这是代码:
The variables value1 to value4 are needed to display in the PDF through the iText PDF generator. When I use the value, it shows "Cannot make a static reference to the non-static field value1". How could I fix that? Here is the code:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class GenerateSummonPDF
{
private String value1;
private String value2;
private String value3;
private String value4; //this variables with constant updated string data
public String getValue1()
{
return this.value1;
}
public void userdata(String p1, String p2, String p3, String p4)
{
this.value1 = p1;
this.value2 = p2;
this.value3 = p3;
this.value4 = p4;
}
public static void main(String[] args)
{
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
document.open();
document.add(new Paragraph(getValue1()); //i need to print all the data here from the userdata
document.close();
writer.close();
} catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
推荐答案
您需要GenerateSummonPDF
的实例才能调用非静态方法:
You need an instance of GenerateSummonPDF
to call non-static methods:
public static void main(String[] args)
{
GenerateSummonPDF generateSummonPDF = new GenerateSummonPDF(); //create an instance
generateSummonPDF.userdata("TestString1", "TestString2", "TestString3", "TestString4"); //some content
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
document.open();
document.add(new Paragraph(generateSummonPDF.getValue1()); //get value1
document.close();
writer.close();
} catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
这篇关于使用获取/设置变量生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文