java 如何使用PDFBox调整XObjects中的运算符和操作数

imzjd6km  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(284)

使用PDFContentStreamEditor.class可以操作主PDF页面内容的运算符和操作数。
似乎XObject不是编辑器编辑的,我的问题是是否有例子也可以调整XObject的操作符和操作数?

  1. PDResources pdResources = page.getResources();
  2. pdResources.getXObjectNames().forEach(propertyName -> {
  3. PDXObject xObject = pdResources.getXObject(propertyName);
  4. if(xObject instanceof PDFormXObject pdformxobject) {
  5. //Is this the right place
  6. }
  7. }

字符串
我在PDFContentStreamEditor.class中尝试了其他方法-processControlrencyGroup方法。我认为我离解决方案越来越近了,但在下面的行中,我只在XObject中添加了一个Contents元素,并且不更改根对象操作符和操作数:

  1. group.getCOSObject().setItem(COSName.CONTENTS, stream);


这里是完整的方法:

  1. @Override
  2. protected void processTransparencyGroup(PDTransparencyGroup group){
  3. PDStream stream = new PDStream(document, group.getContents());
  4. replacementForm = new ContentStreamWriter(replacementFormStream = stream.createOutputStream(COSName.FLATE_DECODE));// stream.createOutputStream(COSName.FLATE_DECODE));
  5. super.processTransparencyGroup(group);
  6. replacementFormStream.close();
  7. group.getCOSObject().setItem(COSName.CONTENTS, stream);
  8. replacementForm = null;
  9. replacementFormStream = null;
  10. }

laximzn5

laximzn51#

您可以通过将此方法添加到this old answerPdfContentStreamEditor类来编辑表单XObjects内容流:

  1. public void processFormXObject(PDFormXObject formXObject, PDPage page) throws IOException {
  2. PDStream stream = new PDStream(document);
  3. replacement = new ContentStreamWriter(replacementStream = stream.createOutputStream(COSName.FLATE_DECODE));
  4. super.processChildStream(formXObject, page);
  5. replacementStream.close();
  6. try (OutputStream outputStream = formXObject.getCOSObject().createOutputStream()) {
  7. stream.createInputStream().transferTo(outputStream);
  8. } finally {
  9. replacement = null;
  10. replacementStream = null;
  11. }
  12. }

字符串
(PdfContentStreamEditor方法)
(Form XObject的内容流并不在Contents子条目中,它们本身就是内容流。因此,替换流内容必须以不同的方式存储。)
你可以这样使用它:

  1. PDDocument document = ...;
  2. for (PDPage page : document.getDocumentCatalog().getPages()) {
  3. PdfContentStreamEditor editor = new PdfContentStreamEditor(document, page) {
  4. @Override
  5. protected void write(ContentStreamWriter contentStreamWriter, Operator operator, List<COSBase> operands) throws IOException {
  6. String operatorString = operator.getName();
  7. if (RGB_FILL_COLOR_OPERATORS.contains(operatorString))
  8. {
  9. for (int i = 0; i < operands.size(); i++) {
  10. COSBase number = operands.get(i);
  11. if (number instanceof COSNumber) {
  12. operands.set(i, new COSFloat(1.0f - ((COSNumber)number).floatValue()));
  13. }
  14. }
  15. }
  16. super.write(contentStreamWriter, operator, operands);
  17. }
  18. final List<String> RGB_FILL_COLOR_OPERATORS = Arrays.asList("rg", "sc", "scn");
  19. };
  20. PDResources resources = page.getResources();
  21. for (COSName name : resources.getXObjectNames()) {
  22. PDXObject xObject = resources.getXObject(name);
  23. if (xObject instanceof PDFormXObject) {
  24. System.out.printf("Editing form XObject %s.\n", name.toString());
  25. editor.processFormXObject((PDFormXObject) xObject, page);
  26. }
  27. }
  28. }

  • (EditFormXObjectContent测试testInvertColorsHighPioneerFallNewsletterADApdf_2)*

此示例反转文档的直接表单XObject页面资源中的某些填充颜色。

展开查看全部

相关问题