将字节数组数据加载到pdf文件中 [英] loading byte array data into pdf file

查看:159
本文介绍了将字节数组数据加载到pdf文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

pdf文件已转换为BYTE数组数据,并存储在数据库中.
数据已在CString对象(HexaDecimal数据)中检索,并转换为二进制.
我试图将这些数据保存在pdf文件中.
该文件未打开.出现错误无法打开或文档更正错误";
RTF文档正确执行了相同的步骤
是否有任何程序可以将pdf十六进制小数转换为二进制数据.
请对此提出建议.

我的转化代码是这样的.

Hi All

The pdf file was convert to BYTE array data and stored in database.
The data was retrived in CString object (HexaDecimal data) and converted to binary.
I tried to save this data in pdf file.
The file was not opening.It was giving error "Unable to Open or Doc correpted error";
The same procedure was happening correctly for RTF document
Is there any procedure to convert pdf Hexa Decimal to binary data.
Please suggest regarding this.

My Conversion code is like this.

void HexatoByte(CString DocData, CString Number)
{
	int size=0;
	char* array;
	std::string str= DocData;
	int length = str.length();
	CStdioFile file;
	CString FileName;
	
	// make sure the input string has an even digit numbers
	if(length%2 == 1)
	{
		str = "0" + str;
		length++;
	}
	
	// allocate memory for the output array
	array = new  char[length/2];
	size = length/2;
	
	std::stringstream sstr(str);
	for(int i=0; i < size; i++)
	{
		char ch1, ch2;
		sstr >> ch1 >> ch2;
		int dig1, dig2;
		if(isdigit(ch1))
			dig1 = ch1 - '0';
		else if(ch1>='A' && ch1<='F')
			dig1 = ch1 - 'A' + 10;
		else if(ch1>='a' && ch1<='f') 
			dig1 = ch1 - 'a' + 10;
		if(isdigit(ch2)) 
			dig2 = ch2 - '0';
		else if(ch2>='A' && ch2<='F')
			dig2 = ch2 - 'A' + 10;
		else if(ch2>='a' && ch2<='f') 
			dig2 = ch2 - 'a' + 10;

		
		array[i] = (dig1*16) + dig2;	
	}
	FileName.Format("%s.pdf",Number);
	
	file.Open(FileName,CFile::modeCreate|CFile::modeReadWrite);
	file.WriteString(array);
	file.Close();
	delete [] array;

}



如果需要任何更改,请检查此代码.
从数据库中仅以char *格式检索数据.
从vc ++开始,我们不会直接检索ByTe数组.



please check this code,if there are any changes required.
From the database data retriving in only char * format.
From vc++ we r not retriving directly ByTe array.

推荐答案

您似乎对实际的二进制数据感到困惑.如果您将PDF读取为字节数组并将其存储在数据库中,则必须以完全相同的方式来检索它(并将其写入任何新文件中).将其转换为字符串只会破坏结构,因此任何PDF处理器都无法读取它.
You seem to be confused about what binary data actually is. If you read a PDF as a byte array and store it in the database, then you must retrieve it in exactly the same way (and write it as such to any new file). Converting it into a string just destroys the structure so it becomes unreadable by any PDF processor.


您是一名程序员,因此创建自己的代码是您的工作.
这将适用于任何文件.尝试了解它并适应您自己的需求.
必须使用二进制文件进行读写. CStdioFile默认为文本.


You are a programmer so creating your own code is your job.
This will work for any file. Try to understand it and adapt to your own needs.
Must work with binary file for reading and writing. CStdioFile is text by default.


// reading a complete binary file
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;


int main () {

	ifstream::pos_type fSize = 0;
	char * memblock = NULL;

	 // Input file open as binary.
	ifstream fileIn ("c:\\Temp\\example.pdf", ios::in|ios::binary|ios::ate);

	if (fileIn.is_open())
	{
		fSize = fileIn.tellg();
		memblock = new char [fSize];		// Allocate memory for file contents.
		fileIn.seekg (0, ios::beg);
		fileIn.read (memblock, fSize);
		fileIn.close();

		cout << "The input file is in memory.\n";

		 // Output file open as binary.
		ofstream fileOut ("c:\\Temp\\example2.pdf", ios::out|ios::binary|ios::ate);

		if (fileOut.is_open())
		{
			fileOut.write(memblock, fSize);
			fileOut.close();
			cout << "The new file has been created.\n";
		}
		else cout << "Unable to open output file.\n";
	}
	else cout << "Unable to open input file.\n";

	_getch();

	if(memblock)
		delete[] memblock;

	return 0;
} 


这篇关于将字节数组数据加载到pdf文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆