I'm using this code to print items to pdf file using:
gfx.DrawString(mstrPrintData(mnumCurrentItemNumber) & "", **fntPrintFont**, brushBrushes, New XRect((numLeftMargin + mnumIndentFromLeftMargin(mnumCurrentItemNumber) + numAdjuster + gnumLeftShift) * gnumWidthScale, (mnumThisYPosition(mnumCurrentItemNumber) + mnumLastHeaderYPosition - mnumYPositionReBase) * gnumHeightScale, gPage.Width.Point, 0), XStringFormats.TopLeft)
where fntPrintFont has been previously set as:
fntPrintFont = New XFont("Name of new font", 15, FontStyle.Regular)
The routine prints some of the fonts I've tried, but not the new 'Daniel' handwriting font the customer wants. I installed the Daniel font correctly. Daniel font installed
Related
When printing to dotmatrix printer, I noticed the texts in "Courier New" font are blurry/sponge when using GrapeCity's Document PDF.
To make sure it's not a printer driver issues, I used MS Word & set that text to "Courier New" with same font size as the one used in GrapeCity PDF. I then print it from MS Word, the text are crystal clear so I know the printer driver & Windows 10 printer setup is ok.
Does anyone know why it doesn't work too good in GrapeCity.Documents.PDF? How do we fix this script bug properly?
var pdfDocument = new GcPdfDocument(PdfEditor.GrapeCityLicense);
var pdfPage = pdfDocument.NewPage();
pdfPage.PaperKind = PaperKind.Legal;
pdfPage.Landscape = false;
var rectangleField = new RectangleF(10f, 20f, 200f, 10f);
var pdfGraphic = pdfPage.Graphics;
pdfGraphic.DrawString(
"Hello World! My car is 2008 Toyota LE Sedan 4 Camary. VIN is 4T1B346K88U780470",
new TextFormat()
{
FontName = "Courier New",
FontSize = 10.0f,
ForeColor = Color.Black,
Language = Language.English,
FontSizeInGraphicUnits = false,
FontStyle = FontStyle.Regular
},
rectangleField,
TextAlignment.Leading,
ParagraphAlignment.Near,
false
);
using (var pdfStream = new MemoryStream())
{
pdfDocument.Save(pdfStream);
pdfStream.Seek(0, SeekOrigin.Begin);
pdfBytes = pdfStream.ToArray();
}
Edited: Attached image as per commenter's request.
I have a section of my PDF in which I need to use one font for its unicode symbol and the rest of the paragraph should be a different font. (It is something like "1. a 2. b 3. c" where "1." is the unicode symbol/font and "a" is another font) I have followed the method Bruno describes here: iText 7: How to build a paragraph mixing different fonts? and it works fine to generate the PDF. The issue is that the file size of the PDF goes from around 20MB to around 100MB compared to using only one font and one Text element. This section is used repeatedly in the document thousands of times. I am wondering if there is a way to reduce the impact of switching fonts or to reduce the file size of the entire document in some way.
Style creation pseudocode:
Style style1 = new Style();
Style style2 = new Style();
PdfFont font1 = PdfFontFactory.createFont(FontProgramFactory.createFont(fontFile1), PdfEncodings.IDENTITY_H, true);
style1.setFont(font1).setFontSize(8f).setFontColor(Color.DARK_GRAY);
PdfFont font2 = PdfFontFactory.createFont(FontProgramFactory.createFont(fontFile2), "", false);
style2.setFont(font2).setFontSize(8f).setFontColor(Color.DARK_GRAY);
Writing text/paragraph pseudocode:
Div div = new Div().setPaddingLeft(3).setMarginBottom(0).setKeepTogether(true);
Paragraph paragraph = new Paragraph();
loop up to 25 times: {
Text unicodeText = new Text(unicodeSymbol + " ").addStyle(style1);
paragraph.add(unicodeText);
Text plainText = new Text(plainText + " ").addStyle(style2);
paragraph.add(plainText);
}
div.add(paragraph);
This writing of text/paragraph is done thousands of times and makes up most of the document. Basically the document consists of thousands of "buildings" that have corresponding codes and the codes have categories. I need to have the index for the category as the unicode symbol and then all of the corresponding codes within the paragraph for the building.
Here is reproducable code:
float offSet = 50;
Integer leading = 10;
DateFormat format = new SimpleDateFormat("yyyy_MM_dd_kkmmss");
String formattedDate = format.format(new Date());
String path = "/tmp/testing_pdf_"+formattedDate + ".pdf";
File targetPdfFile = new File(path);
PdfWriter writer = new PdfWriter(path, new WriterProperties().addXmpMetadata());
PdfDocument pdf = new PdfDocument(writer);
pdf.setTagged();
PageSize pageSize = PageSize.LETTER;
Document document = new Document(pdf, pageSize);
document.setMargins(offSet, offSet, offSet, offSet);
byte[] font1file = IOUtils.toByteArray(FileUtility.getInputStreamFromClassPath("fonts/Garamond-Premier-Pro-Regular.ttf"));
byte[] font2file = IOUtils.toByteArray(FileUtility.getInputStreamFromClassPath("fonts/Quivira.otf"));
PdfFont font1 = PdfFontFactory.createFont(FontProgramFactory.createFont(font1file), "", true);
PdfFont font2 = PdfFontFactory.createFont(FontProgramFactory.createFont(font2file), PdfEncodings.IDENTITY_H, true);
Style style1 = new Style().setFont(font1).setFontSize(8f).setFontColor(Color.DARK_GRAY);
Style style2 = new Style().setFont(font2).setFontSize(8f).setFontColor(Color.DARK_GRAY);
float columnGap = 5;
float columnWidth = (pageSize.getWidth() - offSet * 2 - columnGap * 2) / 3;
float columnHeight = pageSize.getHeight() - offSet * 2;
Rectangle[] columns = {
new Rectangle(offSet, offSet, columnWidth, columnHeight),
new Rectangle(offSet + columnWidth + columnGap, offSet, columnWidth, columnHeight),
new Rectangle(offSet + columnWidth * 2 + columnGap * 2, offSet, columnWidth, columnHeight)};
document.setRenderer(new ColumnDocumentRenderer(document, columns));
for (int j = 0; j < 5000; j++) {
Div div = new Div().setPaddingLeft(3).setMarginBottom(0).setKeepTogether(true);
Paragraph paragraph = new Paragraph().setFixedLeading(leading);
// StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 26; i++) {
paragraph.add(new Text("\u3255 ").addStyle(style2));
paragraph.add(new Text("test ").addStyle(style1));
// stringBuilder.append("\u3255 ").append(" test ");
}
// paragraph.add(stringBuilder.toString()).addStyle(style2);
div.add(paragraph);
document.add(div);
}
document.close();
In creating the reproducible code I have found this this is related to the document being tagged. If you remove the line that marks it as tagged it reduces the file size greatly.
You can also reduce the file size by using the commented out string builder with one font instead of two. (Comment out the two "paragraph.add"s in the for-loop) This mirrors the issue I have in my code.
The problem is not in fonts themselves. The issues comes from the fact that you are creating a tagged PDF. Tagged documents have a lot of PDF objects in them that need a lot of space in the file.
I wasn't able to reproduce your 20MB vs 100MB results. On my machine whether with one font or with two fonts, but with two Text elements, the resultant file size is ~44MB.
To compress file when creating large tagged documents, you should use full compression mode which compresses all PDF objects, not only streams.
To activate full compression mode, create a PdfWriter instance with WriterProperties:
PdfWriter writer = new PdfWriter(outFileName,
new WriterProperties().setFullCompressionMode(true));
This setting reduced the file size for me from >40MB to ~5MB.
Please note that you are using iText 7.0.x while 7.1.x line has already been released and is now the main line of iText, so I recommend that you update to the latest version.
I have written a program for adding small table into specific place in the pdf document. The program worked ok for a few years. But now after opening the pdf file modified by this program in Acrobat Reader (11.0.19) the following error appears:
An error exists on this page. Acrobat may not display the page
correctly. Please contact the person who created the PDF document to
correct the problem
We observed it after upgrade AR from lower version.
I searched answers on similar problem but I couldn't find the mentioned errors in my code. Can anyone help me, please?
PdfStamper rm_stamper = new PdfStamper(rm_reader, new FileOutputStream(UserCommonDetails.getPropertyValue("FILES_TO_SEND") + outputFilename));
rm_stamper.setEncryption(haslo.trim().getBytes(), "JACEK".getBytes(), PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_128);
PdfWriter rm_writer = rm_stamper.getWriter();
Rectangle rm_rect = rm_writer.getPageSize();
PdfPTable rm_table = new PdfPTable(widths);
rm_table.getDefaultCell().setFixedHeight(50);
rm_table.addCell(createLabelCell("NUMBER"));
rm_table.addCell(createDataCell(""+ number));
rm_table.addCell(createLabelCell("NAME"));
rm_table.addCell(createDataCell("" + dk_rm.getName() + " " + dk_rm.getName2()));
rm_table.addCell(createLabelCell("Strategiy"));
rm_table.addCell(createDataCell("" + str_descr));
rm_table.addCell(createLabelCell("Datee"));
rm_table.addCell(createDataCell("" + day));
rm_table.addCell(createLabelCell("Prepade"));
rm_table.addCell(createDataCell("" + get_Name(auhtor)));
rm_table.setTotalWidth(520);
PdfContentByte rm_cb = rm_stamper.getOverContent(RM_table_page);
rm_cb.saveState(); // q
rm_cb.beginText(); // BT
rm_cb.moveText(36, 100); // 36 806 Td
rm_table.writeSelectedRows(0,6,35f, rm_rect.getHeight()-320,rm_cb);
rm_cb.setFontAndSize(bf, 12); // /F1 12 Tf
rm_cb.endText(); // ET
rm_cb.restoreState(); // Q
rm_stamper.close();
indexList.add(new IndexData("EMW", dk_rm.getEmw().trim(), outputFilename, dk_rm.getPowitanie(), dk_rm.getRachunek(),""));
if (trace==1) log.info("Dodano dane do pliku index.dat.pdf dla rachunku: " + dk_rm.getRachunek());
I' m trying to convert a docx document to pdf and store the newly created pdf file as a new version.
This is the test code:
var document = search.findNode("workspace://SpacesStore/30f334f3-d357-4ea6-a09f-09eab2da7488");
var folder = document.parent
var pdf = document.transformDocument('application/pdf');
pdf.name = "tranformed-" + pdf.name;
pdf.save();
document.name = "new-" + document.name + ".pdf";
document.mimetype = "application/pdf";
document.content = pdf.content;
document.save();
The document ends up empty.
Is this type of conversion possible with javaScript?
This Code create new pdf from docx and created pdf stored as version 1.0
var document = search.findNode("workspace://SpacesStore/30f334f3-d357-4ea6-a09f-09eab2da7488");
var folder = document.parent
var pdf = document.transformDocument('application/pdf');
pdf.name = "tranformed-" + pdf.name;
pdf.save();
Thanks for your support.
The problem was assigning the pdf content.
The following code seems to work only with plain text content:
document.content = pdf.content;
Paradoxically, the following is needed when assigning pdf content to a document.
document.properties.content.write(pdf.properties.content);
Thanks.
We have generated a pdf in landscape mode with header and footer as part of the pdf. The header table and footer display fine in pdf using itextpdf5.1.1 jar. However when we update the jar to 5.5.3, the header table does not show only the footer shows. Below is the code snippet.
document = new Document(PageSize.A4.rotate(), 20, 20, 75, 20);
PdfCopy copy = new PdfCopy(document, new FileOutputStream(strPDFFile));
document.open();
PdfReader pdfReaderIntermediate =
new PdfReader(strIntermediatePDFFile);
numberOfPages = pdfReaderIntermediate.getNumberOfPages();
Font ffont = new Font(Font.FontFamily.UNDEFINED, 7, Font.NORMAL);
System.out.println("###### No. of Pages: " + numberOfPages);
for (int j = 0; j < numberOfPages; ) {
page = copy.getImportedPage(pdfReaderIntermediate, ++j);
stamp = copy.createPageStamp(page);
Phrase footer =
new Phrase(String.format("%d of %d", j, numberOfPages), ffont);
ColumnText.showTextAligned(stamp.getUnderContent(),
Element.ALIGN_CENTER, footer,
(document.right() - document.left()) /
2 + document.leftMargin(),
document.bottom() - 10, 0);
if (j != 1) {
headerTable = new PdfPTable(2);
headerTable.setTotalWidth(700);
headerTable.getDefaultCell().setFixedHeight(10);
headerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
headerTable.addCell(new Phrase(String.format(header1), ffont));
headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
headerTable.addCell(new Phrase(String.format(header2), ffont));
headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
headerTable.addCell(new Phrase(String.format(header3), ffont));
headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
headerTable.addCell(new Phrase(String.format(header5, j),
ffont));
headerTable.completeRow();
headerTable.writeSelectedRows(0, 5, 60.5f, 550,
stamp.getUnderContent());
}
stamp.alterContents();
copy.addPage(page);
}
document.close();
When we change the jar from 5.1.1 to 5.5.3 the header is lost. May be a change is needed in the way we call the header for the new jar.
Any inputs will be well appreciated.
Thanks.
You have cells with default padding (i.e. 2) and height 10, and you try to insert text at height 7. But 2 (top margin) + 7 (text height) + 2 (bottom margin) = 11, i.e. more than fits into your cell height 10. Thus, the text does not fit and is not displayed.
You can fix this by either
using a smaller font, e.g. 6, or
using a heigher cell, e.g. 11, or
using a smaller padding, e.g. 1:
headerTable.getDefaultCell().setPadding(1);
With any of these changes, your header shows.
I don't know in which way iText 5.1.1 handled this differently, but the behavior of current iText versions makes sense.