use of org.apache.pdfbox.pdmodel.PDPageContentStream in project Gargoyle by callakrsos.
the class PDFUtil method toPdf.
public static void toPdf(File newPdfFile, File imageFile) throws IOException {
try (PDDocument doc = new PDDocument()) {
PDPage pdPage = new PDPage();
doc.addPage(pdPage);
PDImageXObject pdImage = PDImageXObject.createFromFile(imageFile.getAbsolutePath(), doc);
try (PDPageContentStream stream = new PDPageContentStream(doc, pdPage)) {
stream.drawImage(pdImage, 0, 420);
}
doc.save(newPdfFile);
}
}
use of org.apache.pdfbox.pdmodel.PDPageContentStream in project pdfbox by apache.
the class LayerUtility method appendFormAsLayer.
/**
* Places the given form over the existing content of the indicated page (like an overlay).
* The form is enveloped in a marked content section to indicate that it's part of an
* optional content group (OCG), here used as a layer. This optional group is returned and
* can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
* <p>
* You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
* sure that the graphics state is reset.
*
* @param targetPage the target page
* @param form the form to place
* @param transform the transformation matrix that controls the placement of your form. You'll
* need this if your page has a crop box different than the media box, or if these have negative
* coordinates, or if you want to scale or adjust your form.
* @param layerName the name for the layer/OCG to produce
* @return the optional content group that was generated for the form usage
* @throws IOException if an I/O error occurs
*/
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
PDOptionalContentProperties ocprops = catalog.getOCProperties();
if (ocprops == null) {
ocprops = new PDOptionalContentProperties();
catalog.setOCProperties(ocprops);
}
if (ocprops.hasGroup(layerName)) {
throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
}
PDRectangle cropBox = targetPage.getCropBox();
if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
// PDFBOX-4044
LOG.warn("Negative cropBox " + cropBox + " and identity transform may make your form invisible");
}
PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
ocprops.addGroup(layer);
try (PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG)) {
contentStream.beginMarkedContent(COSName.OC, layer);
contentStream.saveGraphicsState();
contentStream.transform(new Matrix(transform));
contentStream.drawForm(form);
contentStream.restoreGraphicsState();
contentStream.endMarkedContent();
}
return layer;
}
use of org.apache.pdfbox.pdmodel.PDPageContentStream in project pdfbox by apache.
the class PDFontTest method testPDFBox3826createDoc.
private byte[] testPDFBox3826createDoc(TrueTypeFont ttf) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PDDocument doc = new PDDocument()) {
PDPage page = new PDPage();
doc.addPage(page);
// type 0 subset embedding
PDFont font = PDType0Font.load(doc, ttf, true);
try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
cs.beginText();
cs.newLineAtOffset(10, 700);
cs.setFont(font, 10);
cs.showText("testMultipleFontFileReuse1");
cs.endText();
// type 0 full embedding
font = PDType0Font.load(doc, ttf, false);
cs.beginText();
cs.newLineAtOffset(10, 650);
cs.setFont(font, 10);
cs.showText("testMultipleFontFileReuse2");
cs.endText();
// tt full embedding but only WinAnsiEncoding
font = PDTrueTypeFont.load(doc, ttf, WinAnsiEncoding.INSTANCE);
cs.beginText();
cs.newLineAtOffset(10, 600);
cs.setFont(font, 10);
cs.showText("testMultipleFontFileReuse3");
cs.endText();
}
doc.save(baos);
}
return baos.toByteArray();
}
use of org.apache.pdfbox.pdmodel.PDPageContentStream in project pdfbox by apache.
the class PDFontTest method testPDFBOX4115.
/**
* PDFBOX-4115: Test ability to create PDF with german umlaut glyphs with a type 1 font.
* Test for everything that went wrong before this was fixed.
*
* @throws IOException
*/
@Test
public void testPDFBOX4115() throws IOException {
File fontFile = new File("target/fonts", "n019003l.pfb");
File outputFile = new File(OUT_DIR, "FontType1.pdf");
String text = "äöüÄÖÜ";
try (PDDocument doc = new PDDocument()) {
PDPage page = new PDPage();
try (PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
PDType1Font font = new PDType1Font(doc, new FileInputStream(fontFile), WinAnsiEncoding.INSTANCE);
contentStream.beginText();
contentStream.setFont(font, 10);
contentStream.newLineAtOffset(10, 700);
contentStream.showText(text);
contentStream.endText();
}
doc.addPage(page);
doc.save(outputFile);
}
try (PDDocument doc = PDDocument.load(outputFile)) {
PDType1Font font = (PDType1Font) doc.getPage(0).getResources().getFont(COSName.getPDFName("F1"));
Assert.assertEquals(font.getEncoding(), WinAnsiEncoding.INSTANCE);
for (char c : text.toCharArray()) {
String name = font.getEncoding().getName(c);
Assert.assertEquals("dieresis", name.substring(1));
Assert.assertFalse(font.getPath(name).getBounds2D().isEmpty());
}
PDFTextStripper stripper = new PDFTextStripper();
Assert.assertEquals(text, stripper.getText(doc).trim());
}
}
use of org.apache.pdfbox.pdmodel.PDPageContentStream in project pdfbox by apache.
the class TestFontEmbedding method testCIDFontType2VerticalSubsetMonospace.
/**
* Embed a monospace TTF as vertical CIDFontType2 with subsetting.
*
* @throws IOException
*/
public void testCIDFontType2VerticalSubsetMonospace() throws IOException {
String text = "「ABC」";
String expectedExtractedtext = "「\nA\nB\nC\n」";
File pdf = new File(OUT_DIR, "CIDFontType2VM.pdf");
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
File ipafont = new File("target/fonts/ipag00303", "ipag.ttf");
PDType0Font vfont = PDType0Font.loadVertical(document, ipafont);
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.beginText();
contentStream.setFont(vfont, 20);
contentStream.newLineAtOffset(50, 700);
contentStream.showText(text);
contentStream.endText();
}
// Check the font substitution
byte[] encode = vfont.encode(text);
int cid = ((encode[0] & 0xFF) << 8) + (encode[1] & 0xFF);
// it's 441 without substitution
assertEquals(7392, cid);
// Check the dictionaries
COSDictionary fontDict = vfont.getCOSObject();
assertEquals(COSName.IDENTITY_V, fontDict.getDictionaryObject(COSName.ENCODING));
document.save(pdf);
// Vertical metrics are fixed during subsetting, so do this after calling save()
COSDictionary descFontDict = vfont.getDescendantFont().getCOSObject();
COSArray dw2 = (COSArray) descFontDict.getDictionaryObject(COSName.DW2);
// This font uses default values for DW2
assertNull(dw2);
COSArray w2 = (COSArray) descFontDict.getDictionaryObject(COSName.W2);
// Monospaced font has no entries
assertEquals(0, w2.size());
}
// Check text extraction
String extracted = getUnicodeText(pdf);
assertEquals(expectedExtractedtext, extracted.replaceAll("\r", "").trim());
}
Aggregations