我试着用PDF为一个项目做一些事情。我正在尝试与Pdfium进行一些互操作:https://pdfium.googlesource.com/pdfium/。
我想将JPG图像转换为PDF(只是开始)。所以我在网上找到了一些资源,我来到了这个:
///// My main file
// Create the stream
using FileStream image = new FileStream(@"test.jpg", FileMode.Open, FileAccess.Read);
using FileStream pdf = new FileStream(@"test.pdf", FileMode.OpenOrCreate, FileAccess.Write);
// Init the library to use it
FPDF.InitLibrary();
// Create the PDF
IntPtr document = FPDF.CreateNewDocument();
IntPtr page = FPDFPage.New(document, 0, 3000, 4000);
IntPtr pageObject = FPDFPageObj.NewImageObj(document);
// Create a copy of the delegate
FPDF.m_GetBlockDelegate del = Delegates.m_GetBlockDelegate;
FPDF.FILEACCESS fileAccess = new FPDF.FILEACCESS
{
m_FileLen = (ulong)image.Length,
m_Param = image.SafeFileHandle.DangerousGetHandle(),
m_GetBlock = del,
};
// Create the FILEWRITE struct
byte[] buffer = null;
FPDF.FILEWRITE fileWrite = new FPDF.FILEWRITE
{
WriteBlock = delegate (ref FPDF.FILEWRITE pThis, byte[] pData, ulong size)
{
if (buffer == null || (ulong)buffer.Length < size)
buffer = new byte[size];
pData.CopyTo(buffer, 0);
pdf.Write(buffer, 0, (int)size);
return true;
}
};
// Load the image
FPDPImageObj.LoadJpegFile(IntPtr.Zero, 0, pageObject, ref fileAccess);
FPDFPage.InsertObject(page, pageObject);
// Save the PDF
FPDF.SaveAsCopy(document, ref fileWrite, 0);
// Destroy the library
FPDF.DestroyLibrary();
这个方法的问题是:结构体有一个委托作为回调函数来做一些事情(它在C中做一种流实现)。
下面是我的委托的代码,struct和委托的实现:
public struct FILEACCESS
{
public ulong m_FileLen { get; set; }
public m_GetBlockDelegate m_GetBlock { get; set; }
public IntPtr m_Param { get; set; }
}
public struct FILEWRITE
{
// Must be 1
public int version = 1;
public WriteBlockDelegate WriteBlock { get; set; }
}
public delegate int m_GetBlockDelegate(IntPtr param, ulong position, [In] byte[] pBuf, ulong size);
public delegate bool WriteBlockDelegate(ref FILEWRITE pThis, [In] byte[] pData, ulong size);
// The implementation of my delegate in another class
public static int m_GetBlockDelegate(IntPtr param, ulong position, [In] byte[] pBuf, ulong size)
{
// Original source code for the delegate
//FILE* fileHandle = (FILE*)param;
//fseek(imageFile, position, SEEK_SET);
//fread(pBuf, size, 1, fileHandle);
using FileStream fs = new FileStream(new SafeFileHandle(param, false), FileAccess.Read);
fs.Seek((long)position, SeekOrigin.Begin);
fs.Read(pBuf, 0, (int)size);
return (int)size;
}
目前的问题是pBuf应该是一个缓冲区,用于保存所有要读取的数据。所以它的大小必须与size
相同。在我的例子中,pBuf的大小为1。我已经测试了我的代码的C变体,它工作得很好(它读取所有数据)。代码如下:
#include <stdio.h>
#include <fpdfview.h>
#include <fpdf_edit.h>
#include <fpdf_save.h>
FILE* pdfFile = nullptr;
int m_GetBlockImpl(void* param, unsigned long position, unsigned char* pBuf, unsigned long size)
{
FILE* fileHandle = (FILE*)param;
fseek(fileHandle, position, SEEK_SET);
fread(pBuf, size, 1, fileHandle);
return size;
}
int WriteBlockImpl(FPDF_FILEWRITE* pThis, const void* pData, unsigned long size)
{
fwrite(pData, size, 1, pdfFile);
return TRUE;
}
int main()
{
FILE* jpgFile = fopen("C:\\Users\\tcroi\\Desktop\\test.jpg", "rb");
pdfFile = fopen("C:\\Users\\tcroi\\Desktop\\test.pdf", "wb");
if (jpgFile == nullptr || pdfFile == nullptr)
return 1;
FPDF_InitLibrary();
// Create the PDF
FPDF_DOCUMENT pdf_doc = FPDF_CreateNewDocument();
FPDF_PAGE pdf_page = FPDFPage_New(pdf_doc, 0, 3000, 4000);
// Seek the stream and get the size
fseek(jpgFile, 0, SEEK_END);
long fileSize = ftell(jpgFile);
fseek(jpgFile, 0, SEEK_SET);
// Create the FILEACCESS
FPDF_FILEACCESS fileReadObj;
fileReadObj.m_FileLen = fileSize;
fileReadObj.m_Param = jpgFile;
fileReadObj.m_GetBlock = m_GetBlockImpl;
// Load the image to the PDF
FPDF_PAGEOBJECT page_obj_image = FPDFPageObj_NewImageObj(pdf_doc);
FPDFImageObj_LoadJpegFile(nullptr, 0, page_obj_image, &fileReadObj);
FPDFPage_InsertObject(pdf_page, page_obj_image);
// Create the FILEWRITE
FPDF_FILEWRITE fileWriteObj;
fileWriteObj.version = 1;
fileWriteObj.WriteBlock = WriteBlockImpl;
FPDF_SaveAsCopy(pdf_doc, &fileWriteObj, 0);
FPDF_DestroyLibrary();
fclose(jpgFile);
fclose(pdfFile);
return 0;
}
这段代码运行良好,直到我想保存PDF,我只得到一个空白页,也许我错过了一些东西,但根据例子没有。下面是一个例子:https://groups.google.com/g/pdfium/c/P73DgvJtgTs
1条答案
按热度按时间zte4gxcn1#
在插入对象之后,您可能需要
FPDFPage_GenerateContent
页面,并且可能还需要FPDF_ClosePage
页面。