C# PDDocument addPage/importPage工作不正确

C# PDDocument addPage/importPage工作不正确,c#,pdfbox,C#,Pdfbox,我正在使用PDFBOX1.8.3,我需要的是采取一套ZPL和转换成一个PDF格式。对于转换部分(ZPL到PDF),我们使用labelary,这是正确的。我基本上是使用PDFBox来“缝合”所有这些单独的PDF。我的源代码非常简单,如下所示: var outputByteStream = new ByteArrayOutputStream(); var destinationDoc = null; var doc = null; for each(var img

我正在使用PDFBOX1.8.3,我需要的是采取一套ZPL和转换成一个PDF格式。对于转换部分(ZPL到PDF),我们使用labelary,这是正确的。我基本上是使用PDFBox来“缝合”所有这些单独的PDF。我的源代码非常简单,如下所示:

    var outputByteStream = new ByteArrayOutputStream();
    var destinationDoc = null;
    var doc = null;

    for each(var img in images.toArray()) {
      log.debug("Working with image : " + img);

      if("ZPL".equalsIgnoreCase(img.getFormat()) || "ZPL203".equalsIgnoreCase(img.getFormat())) {

        try {
          var convertorServiceUrl = "http://labelary ...." + img.getId()+"?labelSize=4x6&density=8dpmm";
          var urlObject = new URL(convertorServiceUrl);    
          var conn = urlObject.openConnection();   
          conn.connect();          

          if(destinationDoc == null ) { 
             var tmpfile = java.io.File.createTempFile(pw +"-"+uniqKey, ".pdf");
             var raf = new org.apache.pdfbox.io.RandomAccessFile(tmpfile, "rw");          
            destinationDoc = PDDocument.load(conn.getInputStream(), raf); 
          }
          else {           
            doc = PDDocument.load(conn.getInputStream());            
            if (doc != null && doc.getNumberOfPages() > 0) {            
                var page = doc.getDocumentCatalog().getAllPages().get(0);
                destinationDoc.importPage(page); 
            }  
          }           

        } catch (res) {
          log.error("Error message retrieved is " + exceptionMsg);
          throw new BaseRuntimeException("Unable to convert the PDF for image with id " + img.getId(), res);
        }
      }
    }

    try {
      if(destinationDoc != null) {
          destinationDoc.save(outputByteStream);
          destinationDoc.close();
      }
    } catch (e1) {
      log.error("Error in writing the document to the output stream " + pw + "." , e1);
      throw e1;
    }

    return outputByteStream.toByteArray();
源代码运行并生成PDF,但PDF的所有页面都指向第一页。所以,若我的for循环运行4次,PDF的所有4页都是第一个标签

如果我像下面这样使用addPage

    var outputByteStream = new ByteArrayOutputStream();
    var destinationDoc = new PDDocument();
    var doc = null;

    for each(var img in images.toArray()) {
      log.debug("Working with image : " + img);

      if("ZPL".equalsIgnoreCase(img.getFormat()) || "ZPL203".equalsIgnoreCase(img.getFormat())) {

        try {
          var convertorServiceUrl = "http://labelary ...." + img.getId()+"?labelSize=4x6&density=8dpmm";
          var urlObject = new URL(convertorServiceUrl);    
          var conn = urlObject.openConnection();   
          conn.connect();          


            doc = PDDocument.load(conn.getInputStream());            
            if (doc != null && doc.getNumberOfPages() > 0) {            
                var page = doc.getDocumentCatalog().getAllPages().get(0);
                destinationDoc.addPage(page); 
            }  


        } catch (res) {
          log.error("Error message retrieved is " + exceptionMsg);
          throw new BaseRuntimeException("Unable to convert the PDF for image with id " + img.getId(), res);
        }
      }
    }

    try {
      if(destinationDoc != null) {
          destinationDoc.save(outputByteStream);
          destinationDoc.close();
      }
    } catch (e1) {
      log.error("Error in writing the document to the output stream " + pw + "." , e1);
      throw e1;
    }

    return outputByteStream.toByteArray();
然后结果是一个包含所有空页面的PDF页面

我已经确保了从labelary服务返回的内容是正确的,如果我只是简单地获取响应并将其保存到文件中,它就会正常工作。即使一次只保存一页PDDocument,也能正确生成PDF


我的问题是PDF的“缝合”。它应该按照文档工作,但我不确定我做错了什么

您似乎使用了来自c#的pdfbox(根据每个循环语法的
判断)。如果pdfbox可供您使用,是否没有更新版本?甚至Apache pdfbox维护的1.8.x分支也在1.8.17或1.8.18(我没有跟踪)中进行了大量修复。如果您只是想合并PDF,请使用pdfbox 2.0.18的命令行实用程序与java。我使用的是Rhino(JavaScript)的pdfbox,更新版本的pdfbox不是一个即时选项,因为这是一个生产环境。命令行不是我的选项。我也尝试过PDFMergerUtility,但成功率参差不齐```而且,我能够很容易地使用iText实现这一点。这是如此直接,我仍然在使用他们的旧版本。你仍然应该使用当前版本1.8.16/2.0.18进行测试,看看这是否是一个PDFBox错误。因为如果它能工作,那么它肯定是一个bug。