Can PDFIum edit PDF files?

1,276 views
Skip to first unread message

尚二蛋

unread,
Oct 25, 2021, 11:50:57 AM10/25/21
to pdfium
  • Why did I only find the load interface(FPDF_LoadDocument) and not the save interface????

Lei Zhang

unread,
Oct 25, 2021, 2:14:15 PM10/25/21
to 尚二蛋, pdfium
Please take a look in public/fpdf_save.h. I would also suggest looking
through the other headers in the same directory to see all the
available public APIs.

On Mon, Oct 25, 2021 at 8:50 AM 尚二蛋 <shan...@gmail.com> wrote:
>
> Why did I only find the load interface(FPDF_LoadDocument) and not the save interface????
>
> --
> You received this message because you are subscribed to the Google Groups "pdfium" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/3f146007-d811-4aa5-a503-7d90181061c1n%40googlegroups.com.

尚二蛋

unread,
Oct 26, 2021, 3:27:35 AM10/26/21
to pdfium

thanks for your reply.

It is now can  save, but file contents not the same as the source file.
show my code
-------------------------------my code begin------------------------------------

void SavePDF(
  const void* pData,
  unsigned long size) {
  char filename[256]{"E:\\hello_world_test.pdf"};

  FILE* fp = fopen(filename, "w");
  if (!fp) {
    fprintf(stderr, "Failed to open %s for output\n", filename);
    return;
  }

  if (fwrite(pData, size, 1, fp) != 1) {
    fprintf(stderr, "Failed to write to %s\n", filename);
    (void)fclose(fp);
    return;
  }

  (void)fclose(fp);
}

int FakeBlockWriter(FPDF_FILEWRITE* pThis,
  const void* pData,
  unsigned long size) {
  SavePDF(pData, size);
  return size;
}

test () {
  std::string filename = "E:\\src\\pdf\\test\\data\\helloworld.pdf";
  ScopedFPDFDocument doc;
  const char* password = nullptr;
  doc.reset(FPDF_LoadDocument(filename.c_str(), password));

  FPDF_FILEWRITE writer;
  writer.version = 1;
  writer.WriteBlock = FakeBlockWriter;

  FPDF_SaveAsCopy(doc.get(), &writer, 0);
}

----------------------------------------------my code end-------------------
soruce pdf file
%PDF-1.1
%忏嫌
1 0 obj 
<<
/Kids [2 0 R]
/Count 1
/Type /Pages
>>
endobj 
2 0 obj 
<<
/Parent 1 0 R
/Resources 3 0 R
/MediaBox [0 0 612 792]
/Contents [4 0 R]
/Type /Page
>>
endobj 
3 0 obj 
<<
/Font 
<<
/F0 
<<
/BaseFont /Times-Italic
/Subtype /Type1
/Type /Font
>>
>>
>>
endobj 
4 0 obj 
<<
/Length 65
>>
stream
1. 0. 0. 1. 50. 700. cm
BT
  /F0 36. Tf
  (Hello, World!) Tj
ET 

endstream 
endobj 
5 0 obj 
<<
/Pages 1 0 R
/Type /Catalog
>>
endobj xref
0 6
0000000000 65535 f 
0000000015 00000 n 
0000000074 00000 n 
0000000182 00000 n 
0000000281 00000 n 
0000000399 00000 n 
trailer

<<
/Root 5 0 R
/Size 6
>>
startxref
449
%%EOF
---------------------------------------------
save new file
%PDF-1.1

%〕抛

1 0 obj

<</Count 1/Kids[ 2 0 R ]/Type/Pages>>

endobj

2 0 obj

<</Contents[ 4 0 R ]/MediaBox[ 0 0 612 792]/Parent 1 0 R /Resources 3 0 R /Type/Page>>

endobj

3 0 obj

<</Font<</F0<</BaseFont/Times-Italic/Subtype/Type1/Type/Font>>>>>>

endobj

4 0 obj

<</Filter/FlateDecode/Length 65>>stream

x?許0 #C= S en $抯箿B? 糨 屚?B襽
徳湝| 咅?EM厫,.? . 琸 2

endstream

endobj

5 0 obj

<</Pages 1 0 R /Type/Catalog>>

endobj

xref

0 6

0000000000 65535 f

0000000017 00000 n

0000000073 00000 n

0000000178 00000 n

0000000263 00000 n

0000000399 00000 n

trailer

<</Root 5 0 R /Size 6/ID[<3077DC735BA531D26EAAA4D886B9D42B><3077DC735BA531D26EAAA4D886B9D42B>]>>

startxref

448

%%EOF

Lei Zhang

unread,
Oct 27, 2021, 11:25:50 AM10/27/21
to 尚二蛋, pdfium
Please send PDFs as email attachments and not inline, so they remain
intact. To be clear, are you saying the issue is FPDF_SaveAsCopy()'s
output is not exactly the same as the input, or that the output PDF
renders incorrectly?
> To view this discussion on the web visit https://groups.google.com/d/msgid/pdfium/617632f4-0ead-4a9a-b897-6d1c5f016d1fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages