Add toc on 3rd page using apache XWPF [duplicate] - java

This question already has answers here:
Creating a Table of Contents for a XWPFDocument with page numbers' indication
(5 answers)
Closed 5 years ago.
I want to add table of contents(toc) on 3rd page of Doc using apache XWPF.
Here is the code below :
XWPFDocument doc = new XWPFDocument();
CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
TOC toc = new TOC(block);
toc.addRow(3, "toc", 3, "16283778");
doc.write(new FileOutputStream(new File("toc.docx")));

I think you should create a paragraph in your document with a run. That way you can add BreakType.PAGE to your paragraphs.
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
BreakType.PAGE skips a page

Related

PDFBox - Setting Cover Sheet for PDF Portfolio

I am creating a PDF portfolio from scratch using PDFBox 2.0.16. I create the PDF and add attachments. Once the attachments are added, I then create the PDF Portfolio. The following is the code to create the PDF Portfolio/Package.
COSDictionary collectionDictionary = new COSDictionary();
collectionDictionary.setName( COSName.TYPE, "Collection" );
collectionDictionary.setName( "PDF Filename", "T" );
PDDocumentCatalog catalog = pdfDoc.getDocumentCatalog();
catalog.getCOSObject().setItem( "Collection", collectionDictionary );
pdfDoc.save( pdfFile );
When I open the PDF, the Cover Sheet is a blank page. I would like the Cover Sheet to be the first attachment in the PDF Portfolio/Package so that when a user opens the PDF, the 1st attachment in the portfolio is viewed.
What somewhat worked for me (it positioned to this file, as if I had clicked on it in the list) is this:
collectionDictionary.setString(COSName.D, "Attachment 1");
where "Attachment 1" is the name used in the name tree in this example.

How to create new paragraph in determinate position with Apache poi?

I need to insert new paragraphs in diferents positiins in docx.
My code insert new paragraph but not in determinate position
XWFParagraph.
para=docx.createParagraph();
XWPFRun run =para.createRun();
run.setText("example
paragraph");

PDFBox create oversized pages

when I open my PDF created by PDFBOX with PAGE_SIZE_A4, in Adobe to print. It says that it will be "Shrink oversized pages" to 96%. Even if decrease the page size by myself it shows "Shrink oversized pages" to 100%.
I know it is may a duplicate to: How to set Page Scaling option in Apache PDfBox. But this is already 2 years ago.
Usign: latest pdfbox 1.8.9
My example code:
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); // new PDPage(595.27563f, 841.8898);
document.addPage(page);
PDPageContentStream cs = new PDPageContentStream(document, page);
/* With or without content */
cs.close();
document.save(pdfFile);
document.close();
The workaround with images is not an option.
iText is not an option.
Thank you.

Content invisible using Itext in Java

I'm using the iText library with Java and I have an issue, let me explain:
I have a Word file converted into a PDF and i want to add a table at the end of the document, this works perfectly except when the Word file hasn’t active the feature "No Color" at the tab "Page Layout", the table adds it as "invisible".
I've already tried to change the property Font from the Chunk object that created the content, but it does not work, the table continues showing as "invisible".
How I can solve this
The following code is the one that I use to create the table:
Font font = new Font(1,fontSize, Font.BOLD);
font.setColor(0,0,0);
Chunk name = new Chunk(v_emp.getNombre(),font);
Chunk tipo = new Chunk(v_emp.getDireccion(),font);
Paragraph paragraph = new Paragraph();
paragraph.add(tipo);
paragraph.add(new Chunk("\n"));
paragraph.add(name);
PdfPCell cell1 = new PdfPCell(paragraph);
cell1.setPadding(2);
table.addCell(cell1);
Try selecting the table in PDF with your mouse and see if the table is there and still it is invisible.
If it is there, go to the file menu and do print as pdf.

read text from a particular page using PDFBox [duplicate]

This question already has answers here:
Reading a particular page from a PDF document using PDFBox
(6 answers)
Closed 9 years ago.
I know how to read text of an entire pdf file usinf PDFBox using PDFTextStripper.getText(PDDocument).
I also have a sample on how to get an object reference to a particular page using PDDocumentCatalog.getAllPages().get(i).
How do I get the text of just one page using PDFBox as I dont see any such method on PDPage class?
You can set parameters on the PDFTextStripper to read particular pages:
PDDocument doc; // document
int i; // page no.
PDFTextStripper reader = new PDFTextStripper();
reader.setStartPage(i);
reader.setEndPage(i);
String pageText = reader.getText(doc);
As far as I'm aware, PDPage is more used with representing a page onscreen, rather than extracting text. As such, I wouldn't recommend using this to extract text.

Categories