java - 使用获取/设置变量生成 PDF

标签 java eclipse pdf show

需要通过 iText PDF 生成器在 PDF 中显示变量 value1 至 value4。当我使用该值时,它显示“无法对非静态字段 value1 进行静态引用”。我该如何解决这个问题? 这是代码:

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实例来调用非静态方法:

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();
    }
}

关于java - 使用获取/设置变量生成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32316788/

相关文章:

java - maven 构建失败 - 无法解决 maven 依赖关系

java - JAXRS CXF - 获取 http header 而不指定为方法参数

java - 无法从 eclipse java 连接到 mongo db 并且我没有使用 Maven/Gradle

pdf - 使用utf8编码将文本转换为pdf(替代a2ps)

php - 使用 FPDF 通过 PHP 发送电子邮件 PDF 附件

java - 通过 wget 在 Linux 上下载 Java JDK 会显示许可证页面

java - 将 BIRT 集成到现有的 Web 应用程序中

java - java.lang.String 值的布局无效

java - Java 7 Diamond Operator 编译错误 : ArrayList<>();

python - 在python中使用tabulizer循环遍历pdf文件