How to import XFDF Annotations into PDF with PDFBox - java
I have an XFDF files contain some Annotations but not Fields, I would like to know is it able to import those Annotations into PDF file with PDFBox
I have tried to import the XFDF into PDF in Acrobat Reader DC and it's successful to show the imported annotations. However it is not success to show the annotations via used the function importFDF in PDAcroForm. It only import the fields but not annotations.
below is my sample XFDF
<?xml version="1.0" encoding="UTF-8" ?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields />
<annots>
<ink page="0" rect="414.755559,308.987191,564.755559,365.356617" color="#000000" flags="print" name="a74a6f8a-4149-d913-6ee6-34ba434bc254" title="annotationTest" subject="Signature" date="D:20190808175939+08'00'" creationdate="D:20190808175827+08'00'">
<inklist>
<gesture>415.23,342.9;415.23,342.9;417.62,345.77;422.4,349.11;441.98,357.24;456.79,361.53;470.17,364.88;471.12,364.88;471.6,362.97;468.74,355.8;464.44,346.25;456.79,333.35;440.07,314.72;432.91,310.9;429.56,309.46;429.09,309.46;427.65,312.81;427.65,316.15;429.09,319.02;432.91,322.36;445.81,324.27;454.41,323.8;463,320.45;472.56,317.11;488.32,315.2;499.31,315.68;505.52,318.06;509.82,320.45;514.12,322.36;514.6,322.36;515.55,322.36;517.94,322.36;526.06,322.36;530.36,322.36;535.14,322.36;539.44,322.36;541.83,322.36;542.78,322.36;543.26,322.36;544.69,322.36;546.6,322.84;548.51,322.84;549.47,322.84;550.42,323.8;552.34,325.23;553.29,325.71;554.25,326.66;554.72,327.14;556.63,328.1;559.98,329.05;564.28,330.48</gesture>
</inklist>
</ink>
<circle page="0" rect="464.76803,505.088095,546.727124,576.052676" color="#FF8D00" flags="print" name="96f7f067-0811-a87f-3d94-0b654a720d7d" title="annotationTest" subject="Ellipse" date="D:20190808175825+08'00'" width="6" creationdate="D:20190808175813+08'00'"/>
<polygon page="0" rect="184.107608,217.431165,445.578111,277.002438" color="#E44234" flags="print" name="8523798b-3d79-d612-40bb-bcdec2789dcd" title="annotationTest" subject="Polygon" date="D:20190808175810+08'00'" style="cloudy" interior-color="#92E8E8" width="9" opacity="0.58" creationdate="D:20190808175742+08'00'" intensity="2">
<vertices>191.9,269.21;438.78,270.21;433.78,225.23;191.9,224.23;190.9,224.23;191.9,269.21</vertices>
</polygon>
<freetext page="0" rect="376.81193,778.184946,500.283994,807.936941" flags="print" name="373b558a-4892-5fb4-a9aa-b4d7175d3966" title="annotationTest" subject="Free text" date="D:20190808175721+08'00'" width="0" creationdate="D:20190808175700+08'00'" TextColor="#00CC63" FontSize="17">
<contents>Some words...</contents>
<defaultappearance>0 0 0 rg /Arial 17 Tf</defaultappearance>
<defaultstyle>font: Arial 17pt; text-align: left; color: #00CC63</defaultstyle>
</freetext>
<square page="0" rect="196.901725,719.980841,436.781998,752.964378" color="#E44234" flags="print" name="31250d16-7ee0-c34e-027c-5e11e66cf2b1" title="annotationTest" subject="Rectangle" date="D:20190808175655+08'00'" interior-color="#FFC67B" opacity="0.23" creationdate="D:20190808175653+08'00'"/>
<link page="0" rect="36.507,555.18,136.6,581.705" color="#000000" flags="print" name="null" title="null" subject="null" date="D:20190808175651+08'00'" width="0" style="solid"/>
</annots>
<pages>
<defmtx matrix="1.3333333333333333,0,0,-1.3333333333333333,0,1122.559973" />
</pages>
</xfdf>
Or any other library recommended? Many thanks.
After loaded xfdf file, get the COSObject from FDAnnotation and then use it to create a PDAnnotation
PDAnnotation pdfannot = PDAnnotation.createAnnotation(fdfannot.getCOSObject());
And then add to page
List<PDAnnotation> pdfannots = new ArrayList<PDAnnotation>;
pdfannots.add(pdfannot);
page.setAnnotations(pdfannots);
Save it, and you will see the annotations on the PDF
Related
apache Fop separated arabic charcters
I am trying to generate PDF from xml using apache Fop, It works fine with english but while i try to disply arabic text the characters are separated as shown in the Image 1- I tried with 2.1, 2.5 and latest stable apache Fop version 2.6 2- I embded the font in Fop config file 3- I enabled complex-scripts 4- I tried with different fonts but it didn't solve the issue I attached Apache Fop configuration file below <fop version="1.0"> <complex-scripts disabled="false" /> <renderers lang="ar"> <renderer mime="application/pdf"> <fonts> <font kerning="yes" embed-url="src/main/resources/fonts/trado.ttf" embedding-mode="auto" encoding="UTF-8"> <font-triplet name="trado-ar-font" style="normal" weight="normal" /> </font> </fonts> </renderer> </renderers> </fop> I also checked: XSL-FO: displaying Arabic characters and FOP Arabic letter spacing but there is nothing worked with me appriciate any support please
refrence to https://xmlgraphics.apache.org/fop/2.0/complexscripts.html It was solved by adding script="arab" to the root tag like: <fo:root script="arab">
Configure Batik to utilize custom fonts
I have an issue configuring Batik PDFTranscoder for Svg to Pdf conversion. I want to embed custom truetype fonts to PDF output, therefore I utilize Batik transcoder. I supply font configuration in fop config file, as described here: https://xmlgraphics.apache.org/fop/2.2/fonts.html I tried conversion with org.apache.xmlgraphics.Fop versions 2.1 and 2.2. Without any success. My pdf output, comes with 'Times New Roman' font instead of coming with embedded font 'Arial', as it should, based on example below: My config file: <?xml version="1.0" encoding="UTF-8"?> <fop version="1.0"> <renderers> <renderer mime="application/pdf"> <fonts> <directory>C:/path/to/fontsfolder</directory> <auto-detect/> </fonts> </renderer> </renderers> </fop> Here's my transcoder code: import org.apache.batik.transcoder.*; import org.apache.fop.svg.PDFTranscoder; import org.w3c.dom.Document; PDFTranscoder transcoder = new PDFTranscoder(); try { DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); Configuration cfg = cfgBuilder.buildFromFile(new File("path-to-xml-config-file")); ContainerUtil.configure(transcoder, cfg); } catch (Exception e) { throw new TranscoderException(e); } transcoder.transcode(new TranscoderInput(doc), new TranscoderOutput(os)); Sample svg file: <defs> <style type="text/css"> #font-face{font-family:'Arial';font-style: normal;font-weight: 400;src:url('C:/path/to/fontsfolder/arial.ttf') format('truetype');} </style> </defs> <text x="55" y="245" style="font-family: 'Arial'; font-weight:normal; font-style: normal" >Sample text</text> Can anyone tell me what I am missing here with Transcoder configuration? Embedding svg fonts into svg file is not an option in this case.
Update the config file and move fonts section outside the renderers section, like this: My config file: <?xml version="1.0" encoding="UTF-8"?> <fop version="1.0"> <fonts> <directory>C:/path/to/fontsfolder</directory> <auto-detect/> </fonts> </fop> Bulk font configuration from Apache website is simply wrong: https://xmlgraphics.apache.org/fop/2.2/fonts.html This answer was posted as an edit to the question Configure Batik to utilize custom fonts by the OP odoko under CC BY-SA 3.0.
XPath command with declaring multiple namespaces
I am currently trying to reference a node in an XML document using the java library org.apache.xmlbeans.XmlCursor with the command XMLCursor.selectPath(“XPath Call goes here”). I am able to get this command working for finding an element using: picCur.selectPath("declare namespace p='http://schemas.openxmlformats.org/drawingml/2006/main'; .//p:blip"); However now I wish to select an element with a specific attribute. This would usually be done using the same command with .//p:blip[#embed=”attribute value” however the attribute has a different namespace to the element and I am struggling to find a way to declare both namespaces before executing the Xpath command. This is the command that I have been trying to use: picCur.selectPath("declare namespace p='http://schemas.openxmlformats.org/drawingml/2006/main' ” + "declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships' " + ".//p:blip[#r:embed=' attribute value ']"); Below is the xml code, thank you in advanced for your help. <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"> <p:cSld> <p:spTree> <p:nvGrpSpPr> <p:cNvPr id="1" name="" /> <p:cNvGrpSpPr/> <p:nvPr/></p:nvGrpSpPr> <p:grpSpPr> <a:xfrm><a:off x="0" y="0" /><a:ext cx="0" cy="0" /><a:chOff x="0" y="0" /><a:chExt cx="0" cy="0" /></a:xfrm> </p:grpSpPr> <p:sp> <p:nvSpPr> <p:cNvPr id="2" name="Title 1" /> <p:cNvSpPr><a:spLocks noGrp="1" /> </p:cNvSpPr> <p:nvPr> <p:ph type="title" /></p:nvPr> </p:nvSpPr> <p:spPr/> <p:txBody><a:bodyPr/><a:lstStyle/> <a:p> <a:r><a:rPr lang="en-GB" dirty="0" smtClean="0" /> <a:t>Worlds Cutest Dragon</a:t> </a:r><a:endParaRPr lang="en-GB" dirty="0" /></a:p> </p:txBody> </p:sp> <p:pic> <p:nvPicPr> <p:cNvPr id="4" name="myDragon" /> <p:cNvPicPr><a:picLocks noGrp="1" noChangeAspect="1" /> </p:cNvPicPr> <p:nvPr> <p:ph idx="1" /></p:nvPr> </p:nvPicPr> <p:blipFill> <a:blip r:embed="rId2"> <a:extLst> <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /> </a:ext> </a:extLst> </a:blip> <a:stretch><a:fillRect/></a:stretch> </p:blipFill> <p:spPr> <a:xfrm><a:off x="3643312" y="2634456" /><a:ext cx="1857375" cy="2457450" /></a:xfrm> </p:spPr> </p:pic> </p:spTree> <p:extLst> <p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}"> <p14:creationId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="2185825445" /> </p:ext> </p:extLst> </p:cSld> <p:clrMapOvr><a:masterClrMapping/> </p:clrMapOvr> </p:sld>
Have you tried to declare the first namespace and then the other? picCur.selectPath(" declare namespace p='http://schemas.openxmlformats.org/drawingml/2006/main'; declare namespace r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'; .//p:blip[#r:embed='value'] ");
Please try it by adding saxonb ,saxon-dom, xbean jars to server..
Parse an XML into Java - MetaData Format
I saw some xml parsing in java but I really don't know how I can apply it to my code. Here is my xml file: <?xml version="1.0" encoding="UTF-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="https://adomain.com"> <id>https://sharepoint.mydomain/aFile)</id> <category term="SP.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> <title /> <updated>2015-05-18T07:13:18Z</updated> <author> Bla Bla<name /> </author> <content type="application/xml"> <m:properties> <d:CheckInComment /> <d:CheckOutType m:type="Edm.Int32">2</d:CheckOutType> <d:ContentTag>{63FD2CFA-D223-405B-86B3-D59B34ECEBBE},3,1</d:ContentTag> <d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus> <d:ETag>"{63FD2CFA-D223-405B-86B3-D59B34ECEBBE},3"</d:ETag> <d:Exists m:type="Edm.Boolean">true</d:Exists> <d:Length m:type="Edm.Int64">638367</d:Length> <d:Level m:type="Edm.Byte">2</d:Level> <d:MajorVersion m:type="Edm.Int32">0</d:MajorVersion> <d:MinorVersion m:type="Edm.Int32">1</d:MinorVersion> <d:Name>aName.pdf</d:Name> <d:ServerRelativeUrl>/mydomain.com/afile</d:ServerRelativeUrl> <d:TimeCreated m:type="Edm.DateTime">2014-09-03T15:30:22Z</d:TimeCreated> <d:TimeLastModified m:type="Edm.DateTime">2014-09-03T15:30:25Z</d:TimeLastModified> <d:Title /> <d:UIVersion m:type="Edm.Int32">1</d:UIVersion> <d:UIVersionLabel>0.1</d:UIVersionLabel> </m:properties> </content> </entry> I am trying to get the metadata of a file from SharePoint which is displayed in xml format. How can I get the data which is inside the content and also the title and the author like this: Author BlaBla Title Bla Type application/xml TimeLastModified xx/xx/xxxx
The easiest way to parse XML files is the DOM parser. You can find the documentation here and a few tutorials here and here. Also, a related question in stackoverflow here.
You can use Jaxb, now a days it is used for parsing purpose very effectively, Converting XML to JAVA called UnMarshalling http://www.javatpoint.com/jaxb-tutorial
Can I format data that is to be written in CSV file using java
I have some code to write data into a CSV file, but it writes data into a CSV without formatting it properly. I want to bold some specific text. Is that possible?
CSV is just a plain text format, so you can't do any formatting. If you want formatting, consider using an Excel library such as Apache POI or Jasper Reports. (Of course, then you end up with an excel file rather than a CSV, so depending on your situation that may or may not be appropriate) As a side note, there are some strange nuances to writing CSV (such as making sure quotes, commas etc are properly escaped). There's a nice lightweight library I've used called Open CSV that might make your life easier if you choose to just stick with plain old CSV.
CSV is a plain text file format, you can not use any text effect.
Not that I am aware of, CSV is a plain text format. If you are creating a csv, so that you can open it up in Excel, then I would suggest taking a look at the MS Excel XML format. http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example An example would be as follows (taken from the wikipedia link, and this makes some text BOLD) <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author>Darl McBride</Author> <LastAuthor>Bill Gates</LastAuthor> <Created>2007-03-15T23:04:04Z</Created> <Company>SCO Group, Inc.</Company> <Version>11.8036</Version> </DocumentProperties> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>6795</WindowHeight> <WindowWidth>8460</WindowWidth> <WindowTopX>120</WindowTopX> <WindowTopY>15</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom" /> <Borders /> <Font /> <Interior /> <NumberFormat /> <Protection /> </Style> <Style ss:ID="s21"> <Font x:Family="Swiss" ss:Bold="1" /> </Style> </Styles> <Worksheet ss:Name="Sheet1"> <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="5" x:FullColumns="1" x:FullRows="1"> <Row> <Cell> <Data ss:Type="String">Text in cell A1</Data> </Cell> </Row> <Row> <Cell ss:StyleID="s21"> <Data ss:Type="String">Bold text in A2</Data> </Cell> </Row> <Row ss:Index="4"> <Cell ss:Index="2"> <Data ss:Type="Number">43</Data> </Cell> </Row> <Row> <Cell ss:Index="2" ss:Formula="=R[-1]C/2"> <Data ss:Type="Number">21.5</Data> </Cell> </Row> </Table> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <Print> <ValidPrinterInfo /> <HorizontalResolution>600</HorizontalResolution> <VerticalResolution>600</VerticalResolution> </Print> <Selected /> <Panes> <Pane> <Number>3</Number> <ActiveRow>5</ActiveRow> <ActiveCol>1</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </Worksheet> </Workbook
I plain old regular CSV no. But there is no reason why you could not encode your data before writing it out... for example the HTML tags for bold is <b></b>. This would signal to you exactly which portions of the text are bold and being a from a well know standard is still human readable too. The main drawback is you have to parse your data after you read it :( Something else to consider, since you are writing the data out why not write it out as comma separated values in RTF or some other format that does support bold etc? Normally CSV is plain text but there is no reason why you couldn't write it out another way. Just remember to read it back in the same format...