This question already has an answer here:
Acrofields created with libre office are not code fillable unless I edit the pdf once
(1 answer)
Closed 1 year ago.
all
I created a pdf template using libreoffice. I tried to fill chinese string into these field but only garbage in those fields. code like below:
PdfFont kaiuFont = PdfFontFactory.createFont("c:/windows/fonts/kaiu.ttf", true);
FileOutputStream output = new FileOutputStream("d:/download/202106.pdf");
PdfDocument pdf = new PdfDocument(new PdfReader(template), new PdfWriter(output));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("COMPANY_TITLE").setValue(<Chinese words>).setFontAndSize(kaiuFont, 18f);
pdf.close();
What's wrong with those code?
Please ignore this topic if your pdf is not generated by LibreOffice. I google d for a day and found this solution link and it works for me
libreoffice pdf issue
Related
I am in the process of updating some PDF editing tools from iTextSharp to iText7.
In iTextSharp there was a method in the pdfStamper class: pdfStamper.ReplacePage().
However, the pdfStamper class has been removed in iText7 and I am having trouble replicating the same functionality.
Say I have document X that needs page 4 replaced with page 1 of document Y but I want the result to be save to a new document Z.
So far I have 3 lines that use the CopyPageTo method. However any PDF document that gets created is only 1kb in size and corrupted.
Anyone have experience with the newer iText7 and the CopyPageTo method?
NewPagesReader = New PdfReader(strNewPageDocPath)
docNewPages = New PdfDocument(NewPagesReader)
OriginalDocReader = New PdfReader(strOrigPageDocPath)
docOringal = New PdfDocument(OriginalDocReader)
Dim docNew As PdfDocument
Dim NewPDFWriter As New PdfWriter(saver.FileName)
docNew = New PdfDocument(NewPDFWriter)
docOringal.CopyPagesTo(1, 3, docNew)
docNewPages.CopyPagesTo(1, 1, docNew)
docOringal.CopyPagesTo(5, 6, docNew)
ToWriter.Close()
docNew.Close()
Your code looks fine. Instead of copying all the pages to a new document, you can also delete and insert a page:
NewPagesReader = New PdfReader(strNewPageDocPath)
docNewPages = New PdfDocument(NewPagesReader)
OriginalDocReader = New PdfReader(strOrigPageDocPath)
Dim NewPDFWriter As New PdfWriter(saver.FileName)
docOringal = New PdfDocument(OriginalDocReader, NewPDFWriter)
docOringal.RemovePage(4)
docNewPages.CopyPagesTo(1, 1, docOringal, 4)
docOringal.Close()
I am using grails 2.5.2.
I have created a table which shows all the data from database to gsp page and now i need to save that shown data in a pdf format with a button click.What will be the best way to show them into a PDF and save it to my directory. please Help
You can use itext for converting HTML into pdf using the code below:
public void createPdf(HttpServletResponse response, String args, String css, String pdfTitle) {
response.setContentType("application/force-download")
response.setHeader("Content-Disposition", "attachment;filename=${pdfTitle}.pdf")
Document document = new Document()
Rectangle one = new Rectangle(900, 600)
document.setPageSize(one)
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream())
document.open()
ByteArrayInputStream bis = new ByteArrayInputStream(args.toString().getBytes())
ByteArrayInputStream cis = new ByteArrayInputStream(css.toString().getBytes())
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bis, cis)
document.close()
}
Though answering this question late,take a look at grails export plugin.It will be useful if you want to export your data to excel and pdf( useful only if there is no in pre-defined template to export).
Got idea from itext. Used itext 2.1.7 and posted all the values to pdf from a controller method. Used images as background and paragraph and phrase to show values from database.
This question already has an answer here:
PDFBox : PDPageContentStream's append mode misbehaving
(1 answer)
Closed 6 years ago.
I’m using PDFBox 1.8.10 to add text to PDF documents.
It works fine except on some documents where text is added to document (checked with pdf structure inspector) but not displayed in PDF.
Sample document is here: https://kali-docs.ks2.fr/share/s/Ut_LdO8LR4WEeEd1y2k58Q
Because I want to set some custom AlphaConstant to text (and rectangles), I’m using Graphics State Parameter Dictionaries to add text.
Code used:
PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, pdfPage, true, true);
this.textGraphicState = new PDExtendedGraphicsState();
textGraphicState.setNonStrokingAlphaConstant(1f);
Map<String, PDExtendedGraphicsState> graphicsStatesMap = pdfPage.getResources().getGraphicsStates();
if (graphicsStatesMap == null)
{
graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
}
graphicsStatesMap.put("textGraphicState", textGraphicState);
pdfPage.getResources().setGraphicsStates(graphicsStatesMap);
contentStream.appendRawCommands("/textGraphicState gs\n");
contentStream.setNonStrokingColor(fontColor);
contentStream.beginText();
contentStream.setFont( font, fontSize );
contentStream.moveTextPositionByAmount( pagePosX, pagePosY );
contentStream.drawString(text);
contentStream.endText();
contentStream.close();
Any idea ?
Thanks,
Vincent
Resetting graphic state solved my problem (fifth parameter of PDPageContentStream constructor).
PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, pdfPage, true, true, true);
I have a requirement to open PDF on JXBrowser and let the user modify values on PDF and upon saving, I should able to read the modified values and save to database.
My issue was, I am unable to fetch modified values from pdf, its always sending back original values from pdf (acroForm.getField(field name);). Could you help me if there is any other way to solve this problem.
I am using pdfbox 2.0.1
Appreciate your help.
Thanks,
Prasad
Update1:
Adding sample code that I have used in my application
PDDocument PDFDoc = PDDocument.load(complaintform.pdf);
LoggerProvider.setLevel(Level.OFF);
Base64Encoder b64 = new Base64Encoder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDFDoc.save(baos);
String pdfHTML = "<HTML><BODY style=\"width:100%; height:100%\" > <embed style=\"width:100%; height:100%\" src=\"data:application/pdf;base64,"+b64.encode(baos.toByteArray())+"\"type=\"application/pdf\"></BODY></HTML>";
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
this.add(browserView, BorderLayout.CENTER);
browser.loadHTML(pdfHTML);
save()
{
PDDocumentCatalog docCatalog = PDFDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("last");
String modifiedValue = field.getValueAsString();
}
Has anyone encountered this error? Did a quick Google and almost nothing came up.
I used MS Excel to print to PDF and I'm getting this error in PDFBox when I'm trying to extract the first page from the PDF file. I can view the PDF file just fine on my Mac using the default Preview.
This is the PDF that is throwing the error: https://www.dropbox.com/s/owoxicr137iaduz/malformed.pdf
You did not provide code, but I think you probably closed your document before saving the new one.
Example (not working):
PDDocument document = PDDocument.load(new File("..."));
PDDocument newDocument = new PDDocument();
// insert page from document into newDocument
document.close();
newDocument.save(new File("...")); // Causes the error
Example (working):
PDDocument document = PDDocument.load(new File("..."));
PDDocument newDocument = new PDDocument();
// insert page from document into newDocument
newDocument.save(new File("...")); // No error
document.close();