2017-07-18 93 views
0

我正在使用iText重新創建Acrobat的標記樹功能。使用MCID內容獲取標記的內容

到目前爲止,我已經設法得到標籤結構。

我想弄清楚的最後一件事是如何讓&解碼內容流中標記的「標記內容」。

enter image description here

編輯:添加目的

這個問題的目的是要弄清楚如何訪問內容流,具有MCID和解碼的內容。

編輯2:添加iText的RUPS參考

下面的圖像顯示了我的樹已經達到,紅線指向MCID,我想獲得它的內容。

enter image description here

編輯3:添加構建樹

private void manipulate(PdfDictionary element, ItemCollection items) 
    { 
     if (element == null) 
     { 
      return; 
     } 

     ICollection<PdfName> val = element.KeySet(); 
     PdfObject tagName = element.Get(PdfName.S); 
     PdfObject elementType = element.Get(PdfName.Type); 

     string tn = ""; 

     if (tagName != null) 
     { 
      tn = ((PdfName)tagName).GetValue(); 
     } 
     else 
     { 
      tn = ((PdfName)elementType).GetValue(); 
     } 

     TreeViewItem tvI = new TreeViewItem() { Header = tn, IsExpanded = true }; 
     items.Add(tvI); 

     PdfArray kids = element.GetAsArray(PdfName.K); 
     if (kids == null) 
     { 
      return; 
     } 
     for (int i = 0; i < kids.Size(); i++) 
     { 
      PdfDictionary child = kids.GetAsDictionary(i); //Code change required here to detect MCID & get content, this line returns null when child is a MCID 
      manipulate(child, tvI.Items); 
     } 
    } 
} 

編輯4當前的代碼:這樣做的原因是重新創建的Acrobat的 「變量樹」 的功能。

回答

2

根據您添加到問題中的標籤,我看到您正在添加iText 7. iText 7有一個名爲TaggedPdfReaderTool的類。這個類可以用來標記PDF文件轉換成XML:

FileOutputStream outXml = new FileOutputStream("pdf_content.xml"); 
TaggedPdfReaderTool tool = new TaggedPdfReaderTool(document); 
tool.setRootTag("root"); 
tool.convertToXml(outXml); 
outXml.close(); 

XML將具有相同的結構是「標記結構」你已經能夠提取。 XML標籤內的內容將與PDF內容流中標記爲「標籤的一部分」的內容相對應。

給其他讀者的重要信息:問題中的屏幕截圖清楚地顯示PDF已被標記。如果您在未加標籤的PDF上嘗試使用此代碼段,則無法將內容轉換爲PDF。

更新:較低層次的方法

您還可以檢查結構樹這樣的所有部分:process(document.getStructTreeRoot());

process()方法是這樣的:

public static void process(IPdfStructElem elem) { 
    if (elem == null) return; 
    System.out.println(elem.getRole()); 
    System.out.println(elem.getClass().getName()); 
    if (elem instanceof PdfStructElem) { 
     processStructElem((PdfStructElem) elem); 
    } 
    if (elem.getKids() == null) return; 
    for (IPdfStructElem structElem : elem.getKids()) { 
     process(structElem); 
    } 
} 

public static void processStructElem(PdfStructElem elem) { 
    PdfDictionary page = elem.getPdfObject().getAsDictionary(PdfName.Pg); 
    if (page == null) return; 
    PdfStream contents = page.getAsStream(PdfName.Contents); 
    if (contents != null) { 
     System.out.println(new String(contents.getBytes())); 
    } 
    PdfArray array = page.getAsArray(PdfName.Contents); 
    System.out.println(array); 
} 

注頁面的/Contents可以引用單個流或流的數組。在這個簡短的片段中,我忽略了存儲在流數組中的所有/Contents

這是我們使用的測試帶標記的PDF執行這個時所透露的內容的一個例子:

EMC 
/Artifact BMC 
q 
0.01961 0.33333 0.52941 rg 
36 432.34 184.23 27.98 re 
f 
Q 
EMC 
/Span <</MCID 13>> BDC 
q 
BT 
/F2 12 Tf 
42 442.65 Td 
1 1 1 rg 
(The Library)Tj 
ET 
Q 
EMC 
/Artifact BMC 
q 
0.01961 0.33333 0.52941 rg 
36 399.11 184.23 27.98 re 
f 
Q 
EMC 
/Span <</MCID 14>> BDC 
q 
BT 
/F2 12 Tf 
42 409.42 Td 
1 1 1 rg 
(The Company)Tj 
ET 
Q 
EMC 
/Span <</MCID 15>> BDC 
q 
BT 
/F1 20 Tf 
227.73 472.71 Td 
(The Library)Tj 
ET 
Q 
EMC 
/Span <</MCID 16>> BDC 
q 
BT 
/F2 12 Tf 
229.23 440.45 Td 
(iText is a software developer toolkit that allows users to integrate PDF)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 17>> BDC 
q 
BT 
/F2 12 Tf 
229.23 424.46 Td 
(functionalities within their applications, processes or products.)Tj 
ET 
Q 
EMC 
/Artifact BMC 
q 
0.01961 0.33333 0.52941 rg 
605.03 262.75 191.73 235.31 re 
f 
Q 
EMC 
/Span <</MCID 18>> BDC 
q 
BT 
/F1 16 Tf 
676.45 482.5 Td 
0.97647 0.76078 0.15294 rg 
(What?)Tj 
ET 
Q 
EMC 
/Span <</MCID 19>> BDC 
q 
BT 
/F2 12 Tf 
607.94 453.08 Td 
1 1 1 rg 
(iText is a software developer toolkit)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 20>> BDC 
q 
BT 
/F2 12 Tf 
611.61 437.09 Td 
1 1 1 rg 
(that allows users to integrate PDF)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 21>> BDC 
q 
BT 
/F2 12 Tf 
634.95 421.11 Td 
1 1 1 rg 
(functionalities within their)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 22>> BDC 
q 
BT 
/F2 12 Tf 
669.96 405.12 Td 
1 1 1 rg 
(applications)Tj 
ET 
Q 
EMC 
/Span <</MCID 23>> BDC 
q 
BT 
/F1 16 Tf 
679.12 381.5 Td 
0.97647 0.76078 0.15294 rg 
(How?)Tj 
ET 
Q 
EMC 
/Span <</MCID 24>> BDC 
q 
BT 
/F2 12 Tf 
613.94 352.08 Td 
1 1 1 rg 
(By providing you with the tools to)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 25>> BDC 
q 
BT 
/F2 12 Tf 
607.59 336.09 Td 
1 1 1 rg 
(create and manipulate a pdf in your)Tj 
()Tj 
ET 
Q 
EMC 
/Span <</MCID 26>> BDC 
q 
BT 
/F2 12 Tf 
668.96 320.11 Td 
1 1 1 rg 
(source code)Tj 
ET 
Q 
EMC 
/Span <</MCID 27>> BDC 
q 
BT 
/F1 16 Tf 
672.44 296.49 Td 
0.97647 0.76078 0.15294 rg 
(Really?)Tj 
ET 
Q 
EMC 
/Span <</MCID 28>> BDC 
q 
BT 
/F2 12 Tf 
673.64 267.06 Td 
1 1 1 rg 
(Yes really!)Tj 
ET 
Q 
EMC 

的一切,是不是BMC/EDCBDC/EDC運營商之間沒有標記。您正在尋找標有MCID的內容。

在評論中,我解釋說最好使用不同的方法。最好解析每個頁面的內容流(只有一次),並將所有遇到的對象映射到結構樹中的元素。

用你的方法,你必須一遍又一遍地爲每個結構元素解析頁面的內容流。這需要更多的處理。

+0

感謝您的建議。但我仍然想知道如何訪問內容流並對其內容進行解碼。 – PrivatMamtora

+0

你有沒有聽說過iText RUPS? –

+0

是的,我一直在使用它作爲一個調試工具。請參閱編輯。 – PrivatMamtora