I convert Jpanel to PDF using iText PDF, but certain special (Turkish) characters are not being displayed in the resulting PDF document.
My code is shown below:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
document.open();
document.newPage();
Rectangle rec = new Rectangle(0,0,panel.getWidth(), panel.getHeight());
document.setPageSize(rec);
PdfContentByte contentByte = writer.getDirectContent();
PdfGraphics2D g2 = new PdfGraphics2D(contentByte,panel.getWidth(),panel.getHeight());
panel.print(g2);
g2.dispose();
Related
I am trying to create a PDF/UA compliant file that contains a text hyperlink with iText 7. Both the Acrobat Preflight test for PDF/UA and the PDF Accessibility Checker (PAC 3) complain that the PDF file say that the PDF is not compliant.
PAC 3 says ""Link" annotation is not nested inside a "Link" structure element" and the Acrobat Preflight test says the Link annotation does not have an alternate description in the Contents key.
The following is my attempt to create PDF/UA compliant output that contains a text hyperlink.
Any advice would be appreciated.
public void testHyperLink() throws IOException {
// Create PDF/UA with text hyperlink
String filename = "./results/HyperLink.pdf";
WriterProperties properties = new WriterProperties();
properties.addUAXmpMetadata().setPdfVersion(PdfVersion.PDF_1_7);
PdfWriter writer = new PdfWriter(filename, properties);
pdfDoc = new PdfDocument(writer);
//Make document tagged
pdfDoc.setTagged();
pdfDoc.getCatalog().setLang(new PdfString("en-US"));
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdfDoc.getDocumentInfo();
info.setTitle("Hello Hyperlinks!");
document = new Document(pdfDoc);
// Must embed font for PDF/UA
byte[] inputBytes = Files.readAllBytes(Paths.get("./resources/fonts/opensans-regular.ttf"));
boolean embedded = true;
boolean cached = false;
PdfFont font = PdfFontFactory.createFont(inputBytes, PdfEncodings.CP1252, embedded, cached);
Text text = new Text("This is a Text link");
text.setFont(font);
text.setFontSize(16F);
// Add alternate text for hyperlink
text.getAccessibilityProperties().setAlternateDescription("Click here to go to the iText website");
PdfAction act = PdfAction.createURI("https://itextpdf.com/");
text.setAction(act);
Paragraph para = new Paragraph();
para.add(text);
document.add(para);
document.close();
System.out.println("Created "+ filename);
}
A Link object might be what you want:
Link lnk = new Link("This is a Text link",
PdfAction.CreateURI("https://itextpdf.com/"));
lnk.SetFont(font);
lnk.GetLinkAnnotation().SetBorder(new PdfAnnotationBorder(0, 0, 0));//Remove the default border
lnk.GetAccessibilityProperties().SetAlternateDescription("Click here to go to the iText website");
Paragraph para = new Paragraph();
para.Add(lnk);
document.Add(para);
I am generating pdf report with few inputs like font name, font size. I tried to create a font using below code.
Font font = new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
Here, how pass font name that is TIMES_ROMAN as a string?
Here's a quick way on how you can achieve the desired behavior with iText 7:
final PdfDocument pdfDocument = new PdfDocument(new PdfWriter(DEST));
PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.TIMES_ROMAN));
Style myStyle = new Style()
.setFontSize(50)
.setUnderline()
.setFontColor(RED)
.setFont(font);
try (final Document document = new Document(pdfDocument)) {
document.add(new Paragraph("Hello World!").addStyle(myStyle));
document.add(new Paragraph("Hello World!").setFont(font)
.setFontSize(50)
.setUnderline()
.setFontColor(RED));
}
You can also define the font on a Document level (I'm showing Style and directly on the Paragraph).
I have multiple PDF documents that I am merging into a single document. The goal is to create a brand new outline. Each of these sub-documents could have there own outline, I need to remove them and have one entry in the outline per document. If I click on the outline in the merged document, the user has to be taken to the specific page where the sub-document was inserted.
I am merging and removing the existing outline as follows:
InputStream intputSteam = googleDriveService.executeMediaAsInputStream(fileId);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
PdfDocument pdfDocument1 = new PdfDocument(new PdfReader(bais));
PdfDocument pdfDocument2 = new PdfDocument(new PdfReader(intputSteam));
PdfDocument mergedDocument = new PdfDocument(new PdfWriter(baos));
PdfMerger merger = new PdfMerger(mergedDocument);
merger.setCloseSourceDocuments(true)
.merge(pdfDocument1, 1, pdfDocument1.getNumberOfPages())
.merge(pdfDocument2, 1, pdfDocument2.getNumberOfPages());
mergedDocument.getCatalog().remove(PdfName.Outlines);
mergedDocument.close();
The merged document is uploaded to the Google Drive. Later in the execution, the merged document is loaded into memory from Google Drive and I am adding an outline as follows:
InputStream inputStream = driveService.files().get(fileId).executeMediaAsInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfReader pdfReader = new PdfReader(inputStream);
PdfWriter pdfWriter = new PdfWriter(baos);
PdfDocument doc = new PdfDocument(pdfReader, pdfWriter);
PdfOutline pdfOutine = doc.getOutlines(false);
pdfOutine.addOutline("test #1").addAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(5)));
doc.close();
// Upload the document to Google Drive
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
File fileMetadata = new File();
fileMetadata.setName("generated-doc.pdf");
InputStreamContent inputStreamContent = new InputStreamContent("application/pdf", bais);
driveService.files().create(fileMetadata, inputStreamContent)
.execute();
In the resulting document, the outline from the sub-documents has been removed, but the new outline has not been added. Is there something wrong with the way I am adding the outline?
I try to fill data to PDF form using iTextSharp. When I use Adobe Reader to open that PDF, i can see the data has been filled into the field. But when I click on the field, the font size will be changed and if i modify the content, the font size will be fixed. Here is my PDF file
Additional Information:
If i set the field font to "Times Roman", it won't have this issue.
BaseFont bfChinese = BaseFont.CreateFont("c:\\windows\\fonts\\mingliu.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
using (MemoryStream ms = new MemoryStream())
{
PdfReader pdfReader = new PdfReader(file);
PdfStamper pdfStamper = new PdfStamper(pdfReader, ms, '\0', true);
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.AddSubstitutionFont(bfChinese);
pdfFormFields.SetField("Text1", "Testing Message");
pdfFormFields.SetField("Text2", "Testing Message");
pdfStamper.Close();
pdfReader.Close();
return ms.ToArray();
}
I'd like to stamp text to a certain pdf with iText. As this text can be cyrillic I use a unicode font and encoding.
The following sample code represents how I do it:
string inputFile = #"sampleStamped.pdf";
PdfReader reader;
PdfStamper stamper;
FileStream fs;
byte[] binaryPdf = File.ReadAllBytes(inputFile);
reader = new PdfReader(binaryPdf);
fs = new FileStream(inputFile, FileMode.Create, FileAccess.Write);
stamper = new PdfStamper(reader, fs);
BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfContentByte cb = stamper.GetOverContent(1);
Phrase p = new Phrase();
p.Font = new Font(bf, 25, Font.NORMAL, BaseColor.BLUE);
p.Add("Sample Text");
ColumnText.ShowTextAligned(cb, PdfContentByte.ALIGN_LEFT, p, 200, 200, 0);
if (stamper != null)
stamper.Close();
if (fs != null)
fs.Close();
if (reader != null)
reader.Close();
The program works as expected and without errors. But if I want to open the stamped pdf in Acrobat Reader 11 or DC on Windows it says that there are proplems to display content and the stamped text is not there.
I use itextsharp 5.5.8.
Any idea how to fix this problem?
Thanks
This is not an issue of iText(Sharp) but a quirk (a feature?) of Adobe Reader.
Your sample file claims to be a PDF 1.2 file. Adobe Reader seems to behave differently when confronted with composite fonts in PDF 1.2 files.
You can check this by patching your sampleStamped.pdf, simply replace the first bytes %PDF-1.2 by %PDF-1.3 and open the file in Adobe Reader... no problem anymore.
Thus, you should make sure that your stamped PDF claims to be at least PDF 1.3. If you stamp your PDF 1.2 file, you can do so by creating the PdfStamper like this:
stamper = new PdfStamper(reader, fs, (char)3);
The result: