use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.
the class PDDocument method save.
/**
* This will save the document to an output stream.
*
* @param output The stream to write to. It will be closed when done. It is recommended to wrap
* it in a {@link java.io.BufferedOutputStream}, unless it is already buffered.
*
* @throws IOException if the output could not be written
*/
public void save(OutputStream output) throws IOException {
if (document.isClosed()) {
throw new IOException("Cannot save a document which has been closed");
}
// subset designated fonts
for (PDFont font : fontsToSubset) {
font.subset();
}
fontsToSubset.clear();
// save PDF
COSWriter writer = new COSWriter(output);
try {
writer.write(this);
} finally {
writer.close();
}
}
use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testAcroFormDefaultFonts.
/**
* PDFBOX-3732, PDFBOX-4303, PDFBOX-4393: Test whether /Helv and /ZaDb get added, but only if
* they don't exist.
*/
@Test
public void testAcroFormDefaultFonts() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDAcroForm acroForm2 = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm2);
PDResources defaultResources = acroForm2.getDefaultResources();
assertNull(defaultResources);
defaultResources = new PDResources();
acroForm2.setDefaultResources(defaultResources);
assertNull(defaultResources.getFont(COSName.HELV));
assertNull(defaultResources.getFont(COSName.ZA_DB));
// getting AcroForm sets the two fonts
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
assertNotNull(defaultResources.getFont(COSName.HELV));
assertNotNull(defaultResources.getFont(COSName.ZA_DB));
// repeat with a new AcroForm (to delete AcroForm cache) and thus missing /DR
doc.getDocumentCatalog().setAcroForm(new PDAcroForm(doc));
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
PDFont helv = defaultResources.getFont(COSName.HELV);
PDFont zadb = defaultResources.getFont(COSName.ZA_DB);
assertNotNull(helv);
assertNotNull(zadb);
doc.save(baos);
doc.close();
doc = PDDocument.load(baos.toByteArray());
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
helv = defaultResources.getFont(COSName.HELV);
zadb = defaultResources.getFont(COSName.ZA_DB);
assertNotNull(helv);
assertNotNull(zadb);
// make sure that font wasn't overwritten
assertNotEquals(PDType1Font.HELVETICA, helv);
assertNotEquals(PDType1Font.ZAPF_DINGBATS, zadb);
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.
the class TestOptionalContentGroupsInstrumentationTest method testOCGGenerationSameNameCanHaveSameVisibilityOff.
/**
* PDFBOX-4496: setGroupEnabled(String, boolean) must catch all OCGs of a name even when several
* names are identical.
*
* @throws IOException
*/
@Test
public void testOCGGenerationSameNameCanHaveSameVisibilityOff() throws IOException {
Bitmap expectedImage;
Bitmap actualImage;
PDDocument doc = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
// Prepare OCG functionality
PDOptionalContentProperties ocprops = new PDOptionalContentProperties();
doc.getDocumentCatalog().setOCProperties(ocprops);
// ocprops.setBaseState(BaseState.ON); //ON=default
// Create OCG for background
PDOptionalContentGroup background = new PDOptionalContentGroup("background");
ocprops.addGroup(background);
assertTrue(ocprops.isGroupEnabled("background"));
// Create OCG for enabled
PDOptionalContentGroup enabled = new PDOptionalContentGroup("science");
ocprops.addGroup(enabled);
assertFalse(ocprops.setGroupEnabled("science", true));
assertTrue(ocprops.isGroupEnabled("science"));
// Create OCG for disabled1
PDOptionalContentGroup disabled1 = new PDOptionalContentGroup("alternative");
ocprops.addGroup(disabled1);
// Create OCG for disabled2 with same name as disabled1
PDOptionalContentGroup disabled2 = new PDOptionalContentGroup("alternative");
ocprops.addGroup(disabled2);
assertFalse(ocprops.setGroupEnabled("alternative", false));
assertFalse(ocprops.isGroupEnabled("alternative"));
// Setup page content stream and paint background/title
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginMarkedContent(COSName.OC, background);
contentStream.beginText();
contentStream.setFont(font, 14);
contentStream.newLineAtOffset(80, 700);
contentStream.showText("PDF 1.5: Optional Content Groups");
contentStream.endText();
contentStream.endMarkedContent();
font = PDType1Font.HELVETICA;
// Paint enabled layer
contentStream.beginMarkedContent(COSName.OC, enabled);
contentStream.setNonStrokingColor(AWTColor.GREEN);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 600);
contentStream.showText("The earth is a sphere");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer1
contentStream.beginMarkedContent(COSName.OC, disabled1);
contentStream.setNonStrokingColor(AWTColor.RED);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 500);
contentStream.showText("Alternative 1: The earth is a flat circle");
contentStream.endText();
contentStream.endMarkedContent();
// Paint disabled layer2
contentStream.beginMarkedContent(COSName.OC, disabled2);
contentStream.setNonStrokingColor(AWTColor.BLUE);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 450);
contentStream.showText("Alternative 2: The earth is a flat parallelogram");
contentStream.endText();
contentStream.endMarkedContent();
contentStream.close();
doc.getDocumentCatalog().setPageMode(PageMode.USE_OPTIONAL_CONTENT);
File targetFile = new File(testResultsDir, "ocg-generation-same-name-off.pdf");
doc.save(targetFile.getAbsolutePath());
doc.close();
// render PDF with science disabled and alternatives with same name enabled
doc = PDDocument.load(new File(testResultsDir, "ocg-generation-same-name-off.pdf"));
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("background", false);
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("science", false);
doc.getDocumentCatalog().getOCProperties().setGroupEnabled("alternative", true);
actualImage = new PDFRenderer(doc).renderImage(0, 2);
actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-actual.png")));
} finally {
doc.close();
}
// create PDF without OCGs to created expected rendering
PDDocument doc2 = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc2.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
PDPageContentStream contentStream = new PDPageContentStream(doc2, page, AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA;
contentStream.setNonStrokingColor(AWTColor.RED);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 500);
contentStream.showText("Alternative 1: The earth is a flat circle");
contentStream.endText();
contentStream.setNonStrokingColor(AWTColor.BLUE);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(80, 450);
contentStream.showText("Alternative 2: The earth is a flat parallelogram");
contentStream.endText();
contentStream.close();
expectedImage = new PDFRenderer(doc2).renderImage(0, 2);
actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-expected.png")));
} finally {
doc2.close();
}
// compare images
int height = expectedImage.getHeight();
int width = expectedImage.getWidth();
int[] expectedImagePixels = new int[width * height];
expectedImage.getPixels(expectedImagePixels, 0, width, 0, 0, width, height);
int[] actualImagePixels = new int[width * height];
actualImage.getPixels(actualImagePixels, 0, width, 0, 0, width, height);
Assert.assertArrayEquals(expectedImagePixels, actualImagePixels);
}
use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.
the class PDFStreamEngine method showText.
/**
* Process text from the PDF Stream. You should override this method if you want to
* perform an action when encoded text is being processed.
*
* @param string the encoded text
* @throws IOException if there is an error processing the string
*/
protected void showText(byte[] string) throws IOException {
PDGraphicsState state = getGraphicsState();
PDTextState textState = state.getTextState();
// get the current font
PDFont font = textState.getFont();
if (font == null) {
Log.w("PdfBox-Android", "No current font, will use default");
font = PDType1Font.HELVETICA;
}
float fontSize = textState.getFontSize();
float horizontalScaling = textState.getHorizontalScaling() / 100f;
float charSpacing = textState.getCharacterSpacing();
// put the text state parameters into matrix form
Matrix parameters = new Matrix(// 0
fontSize * horizontalScaling, // 0
0, // 0
0, // 0
fontSize, 0, // 1
textState.getRise());
// read the stream until it is empty
InputStream in = new ByteArrayInputStream(string);
while (in.available() > 0) {
// decode a character
int before = in.available();
int code = font.readCode(in);
int codeLength = before - in.available();
String unicode = font.toUnicode(code);
// Word spacing shall be applied to every occurrence of the single-byte character code
// 32 in a string when using a simple font or a composite font that defines code 32 as
// a single-byte code.
float wordSpacing = 0;
if (codeLength == 1 && code == 32) {
wordSpacing += textState.getWordSpacing();
}
// text rendering matrix (text space -> device space)
Matrix ctm = state.getCurrentTransformationMatrix();
Matrix textRenderingMatrix = parameters.multiply(textMatrix).multiply(ctm);
// changes to vertical text should be tested with PDFBOX-2294 and PDFBOX-1422
if (font.isVertical()) {
// position vector, in text space
Vector v = font.getPositionVector(code);
// apply the position vector to the horizontal origin to get the vertical origin
textRenderingMatrix.translate(v);
}
// get glyph's horizontal and vertical displacements, in text space
Vector w = font.getDisplacement(code);
// process the decoded glyph
saveGraphicsState();
Matrix textMatrixOld = textMatrix;
Matrix textLineMatrixOld = textLineMatrix;
showGlyph(textRenderingMatrix, font, code, unicode, w);
textMatrix = textMatrixOld;
textLineMatrix = textLineMatrixOld;
restoreGraphicsState();
// calculate the combined displacements
float tx;
float ty;
if (font.isVertical()) {
tx = 0;
ty = w.getY() * fontSize + charSpacing + wordSpacing;
} else {
tx = (w.getX() * fontSize + charSpacing + wordSpacing) * horizontalScaling;
ty = 0;
}
// update the text matrix
textMatrix.concatenate(Matrix.getTranslateInstance(tx, ty));
}
}
use of com.tom_roush.pdfbox.pdmodel.font.PDFont in project PdfBox-Android by TomRoush.
the class PDFontSetting method getFont.
/**
* This will get the font for this font setting.
*
* @return The font for this setting of null if one was not found.
*
* @throws IOException If there is an error getting the font.
*/
public PDFont getFont() throws IOException {
PDFont retval = null;
COSBase font = fontSetting.getObject(0);
if (font instanceof COSDictionary) {
retval = PDFontFactory.createFont((COSDictionary) font);
}
return retval;
}
Aggregations