writeOperator("Do"); restoreGraphicsState();
contents.transform(matrix); contents.drawForm(form); contents.restoreGraphicsState(); contents.transform(matrix); contents.drawForm(form); contents.restoreGraphicsState();
private void contentStreamRestoreState() throws IOException { if (saveCounter == 0) throw new IllegalStateException("Internal save/restore state error. Should never happen."); saveCounter--; contentStream.restoreGraphicsState(); }
contentStream.transform(new Matrix(transform)); contentStream.drawForm(form); contentStream.restoreGraphicsState(); contentStream.endMarkedContent();
contentStream.restoreGraphicsState(); contentStream.close();
public void restoreGraphics() { try { saveGraphicsCounter--; cs.restoreGraphicsState(); if (saveGraphicsCounter < 0) throw new IllegalStateException("Invalid save/restore pairing!"); } catch (IOException e) { logAndThrow("restoreGraphics", e); } }
public void restoreGraphics() { try { saveGraphicsCounter--; cs.restoreGraphicsState(); if (saveGraphicsCounter < 0) throw new IllegalStateException("Invalid save/restore pairing!"); } catch (IOException e) { logAndThrow("restoreGraphics", e); } }
PDImageXObject img = PDImageXObject.createFromFileByExtension(imageFile, doc); cs.drawImage(img, 0, 0); cs.restoreGraphicsState();
public void placeXForm(float x, float y, PDFormXObject xFormObject) { try { cs.saveGraphicsState(); cs.transform(new Matrix(AffineTransform.getTranslateInstance(x, y))); cs.drawForm(xFormObject); cs.restoreGraphicsState(); } catch (IOException e) { logAndThrow("placeXForm", e); } } }
public void placeXForm(float x, float y, PDFormXObject xFormObject) { try { cs.saveGraphicsState(); cs.transform(new Matrix(AffineTransform.getTranslateInstance(x, y))); cs.drawForm(xFormObject); cs.restoreGraphicsState(); } catch (IOException e) { logAndThrow("placeXForm", e); } } }
PDPage pdPage = doc.getPage(page); InputStream oldContentStream = pdPage.getContents(); byte[] ba = IOUtils.toByteArray(oldContentStream); oldContentStream.close(); // brings a warning because a content stream already exists PDPageContentStream newContentStream = new PDPageContentStream(doc, pdPage, false, true); // createFromFile is the easiest way with an image file // if you already have the image in a BufferedImage, // call LosslessFactory.createFromImage() instead PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc); newContentStream.saveGraphicsState(); newContentStream.drawImage(pdImage, 0, 0); newContentStream.restoreGraphicsState(); newContentStream.close(); // append the saved existing content stream PDPageContentStream newContentStream2 = new PDPageContentStream(doc, pdPage, true, true); newContentStream2.appendRawCommands(ba); // deprecated... needs to be rediscussed among devs newContentStream2.close();
/** * Draw an image at the x,y coordinates, with the given size. * * @param image The image to draw. * @param x The x-coordinate to draw the image. * @param y The y-coordinate to draw the image. * @param width The width to draw the image. * @param height The height to draw the image. * * @throws IOException If there is an error writing to the stream. * @throws IllegalStateException If the method was called within a text block. */ public void drawImage(PDImageXObject image, float x, float y, float width, float height) throws IOException { if (inTextMode) { throw new IllegalStateException("Error: drawImage is not allowed within a text block."); } saveGraphicsState(); AffineTransform transform = new AffineTransform(width, 0, 0, height, x, y); transform(new Matrix(transform)); writeOperand(resources.add(image)); writeOperator("Do"); restoreGraphicsState(); }
writeOperator("EI"); restoreGraphicsState();
writeOperator("Do"); restoreGraphicsState();
/** * Draw an image at the origin with the given transformation matrix. * * @param image The image to draw. * @param matrix The transformation matrix to apply to the image. * * @throws IOException If there is an error writing to the stream. * @throws IllegalStateException If the method was called within a text block. */ public void drawImage(PDImageXObject image, Matrix matrix) throws IOException { if (inTextMode) { throw new IllegalStateException("Error: drawImage is not allowed within a text block."); } saveGraphicsState(); AffineTransform transform = matrix.createAffineTransform(); transform(new Matrix(transform)); writeOperand(resources.add(image)); writeOperator("Do"); restoreGraphicsState(); }
writeOperator("Do"); restoreGraphicsState();
... PDPageContentStream contentStream = new PDPageContentStream( document, page, true, true, true); contentStream.saveGraphicsState(); contentStream.beginText(); // set font and font size contentStream.setFont(pdfFont, fontSize); // set text color to red contentStream.setNonStrokingColor(240, 240, 240); if (rotate) { // rotate the text according to the page rotation contentStream.setTextRotation(degree, x, y); } else { contentStream.setTextTranslation(centeredXPosition, centeredYPosition); } contentStream.drawString(text); contentStream.endText(); contentStream.restoreGraphicsState(); contentStream.close(); ...
contentStream.transform(new Matrix(transform)); contentStream.drawForm(form); contentStream.restoreGraphicsState(); contentStream.endMarkedContent();
contentStream.transform(new Matrix(transform)); contentStream.drawForm(form); contentStream.restoreGraphicsState(); contentStream.endMarkedContent(); contentStream.close();