Skip to content

Instantly share code, notes, and snippets.

@witwall
Created July 10, 2014 10:39
Show Gist options
  • Save witwall/5a869c7a63f5b001d5ba to your computer and use it in GitHub Desktop.
Save witwall/5a869c7a63f5b001d5ba to your computer and use it in GitHub Desktop.
Decrypt PDF(remove pdf password and limitations) with pdfium
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../fpdfsdk/include/fpdfview.h"
#include "../fpdfsdk/include/fpdfsave.h"
/*
author:Steven Lee
website:http://blog.rubypdf.com
email:rocsky(at)gmail(dot)com
*/
FILE * pFile;
int writeBlock(FPDF_FILEWRITE* pThis, const void* pData, unsigned long size)
{
int result= fwrite(pData, 1,size, pFile);
return result;
}
int main(int argc, char* argv[])
{
char * pwd=NULL;
if(argc==4)
{
pwd=argv[3];
}
else if(argc==3)
{
//do nothing
}
else
{
fprintf(stderr,"Usage:%s input.pdf outputpdf [password]\n");
fprintf(stderr,"password is not required \nonly when PDF is encrypted with the owner password, \n");
fprintf(stderr,"password can be the owner password or user passoword \n");
return -1;
}
FPDF_InitLibrary(NULL);
char * input=argv[1];//"d:\\PDFCreator_op.pdf"
char * output=argv[2];
FPDF_DOCUMENT doc=FPDF_LoadDocument(input,NULL);
int _errno =fopen_s (&pFile,output, "wb");
char buff[256];
if(_errno!=0)
{
//printf("Error opening file: %s.\n", strerror(_errno));
strerror_s(buff, 100, _errno);
fprintf(stderr,"Error opening file: %s.\n", buff);
return -1;
}
FPDF_FILEWRITE pFileWrite;
pFileWrite.version=1;
pFileWrite.WriteBlock=writeBlock;
FPDF_SaveAsCopy(doc,&pFileWrite,FPDF_REMOVE_SECURITY);
_errno=fclose (pFile);
if(_errno!=0)
{
strerror_s(buff, 100, _errno);
fprintf(stderr,"Error closing file: %s.\n", buff);
return -1;
}
FPDF_DestroyLibrary();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment