Merging Tagged PDF without ruining the tags

后端 未结 2 1320
慢半拍i
慢半拍i 2021-01-25 02:20

I am trying to merge two Tagged PDF\'s with the iTextPDF 5.4.4 version jar. After doing all the operations while closing the document on the line: document.close();): . It thro

2条回答
  •  伪装坚强ぢ
    2021-01-25 02:47

    The Code was written in C#

      public static byte[] mergeTest(byte[] pdf) {
            PdfReader reader = null;
            Document doc = null;
            PdfCopy copy = null;
            MemoryStream stream = new MemoryStream();
            byte[] output = null;
    
            try {
                reader = new PdfReader(pdf);
                doc = new Document();
    
                copy = new PdfCopy(doc, stream);
                bool tagged = reader.IsTagged();
    
                if (tagged)
                    copy.SetTagged();
    
    
                doc.Open();
    
                for (int x = 1; x <= reader.NumberOfPages; x++) {
                    copy.AddPage(copy.GetImportedPage(reader, x, tagged));
                }
    
                copy.FreeReader(reader);
                doc.Close();
                copy.Close();
    
                output = stream.ToArray();
    
                stream.Flush();
                stream.Dispose();
    
            } catch (Exception ex) {
    
            } finally {
                try {
                    if (reader != null)
                        reader.Close();
                } catch (Exception) { }
            }
            return output;
        }
    

提交回复
热议问题