Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get string or image from "PDFDocument.context.indirectObjects" #962

Closed
wangxi83 opened this issue Aug 12, 2021 · 2 comments
Closed

Comments

@wangxi83
Copy link

I've load a pdf using

const existingPdfBytes = fs.readFileSync("C:/test/1.pdf");
const pdfDoc = await pdfLib.PDFDocument.load(existingPdfBytes);

and I can find many things in PDFDocument.context.indirectObjects

I've tried ways to get the content from indrectObjects. ex:

pdfDoc.context.indirectObjects.forEach((v,k)=>{
        const {decodePDFRawStream, PDFRawStream} =require('pdf-lib');
        if(v instanceof PDFRawStream){
            // console.log(dengXian.encodeText(v.contents.toString()));
            let stream = decodePDFRawStream(v);
            console.log(Utf8ArrayToStr(stream.getBytes()));
            // console.log(iconv.decode(Buffer.from(bytes), "cp936"));
        }
    });

but none of ways is the right way...

can anyone help me? Thanks!!

@nealdyrkacz
Copy link

nealdyrkacz commented Aug 12, 2021

@wangxi83

Take a look at this

I have been able to find the images and their sizes.... I am still trying to figure out if its possible to find what page the image is on and what the image coordinates are..... ( my issue )

Find images:

const enumeratedIndirectObjects = pdfDoc.context.enumerateIndirectObjects();
enumeratedIndirectObjects.forEach(x => {
const pdfRef = x[0];
const pdfObject = x[1];

  if (!(pdfObject instanceof PDFRawStream)) return;

  const { dict } = pdfObject;

  const subtype = dict.get(PDFName.of('Subtype'));
  const width = dict.get(PDFName.of('Width'));
  const height = dict.get(PDFName.of('Height'));
  const name = dict.get(PDFName.of('Name'));

  if (subtype == PDFName.of('Image')) {
   //FOUND IMAGE, NOW WHAT PAGE DOES IT EXIST ON
   //push image coordinates into an array for page
  }
});

@Hopding
Copy link
Owner

Hopding commented Sep 22, 2021

pdf-lib doesn't currently provide good support for processing content streams. It can be done, but you'll have to read the PDF spec to understand the inner structure of PDF files. See also #963 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants