1

我正在尝试从 url 读取 pdf 而无需下载,然后我试图将其展平。

这是代码:

import java.io.FileOutputStream;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class FormFillAndMakeItFlattenPDF {
   public static void main(String[] args) {
      try {
         PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
         PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Flattened.pdf"));
         AcroFields form2 = stamp2.getAcroFields();
         stamp2.setFormFlattening(true);
         stamp2.close();
      }
      catch (Exception de) {
         de.printStackTrace();
      }
   }
}

但是,这会引发错误/异常。这是堆栈跟踪:

    java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:352)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:214)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:201)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:530)
    at java.net.Socket.connect(Socket.java:480)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:406)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:541)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:245)
    at sun.net.www.http.HttpClient.New(HttpClient.java:318)
    at sun.net.www.http.HttpClient.New(HttpClient.java:335)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:832)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:773)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:698)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1021)
    at java.net.URL.openStream(URL.java:1009)
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source)
    at com.lowagie.text.pdf.PRTokeniser.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
    at FormFillAndMakeItFlattenPDF.main(FormFillAndMakeItFlattenPDF.java:18)

谁能告诉我,我在这里做错了什么?

4

2 回答 2

2

如果问题是由于代理而发生的,请在应用程序启动时调用它。

System.setProperty("http.proxyHost", Config.PROXY_HOST);
System.setProperty("http.proxyPort", Config.PROXY_PORT);

您将 Config.X 更改为系统值的位置。

注意:对于 https,您需要使用 https.proxyHost 和 https.proxyPort 添加类似的行

于 2012-11-12T08:56:34.373 回答
1

您的确切代码对我有用。我在带有 Java 7 的 Windows 上使用 iText-4.2.0 (*)。

这是我运行的代码,测试是绿色的并且“Flattened.pdf”被正确创建:

public class PdfTest {
    @Test
    public void testFlatten() throws Exception {
        PdfReader reader = new PdfReader("http://www.irs.gov/pub/irs-pdf/fw4.pdf");
        PdfStamper stamp2 = new PdfStamper(reader, new FileOutputStream("C:\\Users\\david\\Flattened.pdf"));
        AcroFields form2 = stamp2.getAcroFields();
        stamp2.setFormFlattening(true);
        stamp2.close();
    }
}

(*) https://github.com/ymasory/iText-4.2.0

于 2012-11-12T08:54:53.413 回答