Cocoa/iOS: How to add a file as attachment to a PDF document? - ios

PDF files support embedding arbitrary files as attachments (see here).
I would like to do just that in a Mac and an iPhone application using Objective-C:
Add a file as attachment to a PDF
Read the list of attached files from a PDF and extract one
Use case:
My app uses a custom document format that can only be opened by the app. I'd like to export the document as PDF and embed the original, custom document so that users who happen to have the app installed can modify the document. Everybody else can still open and print the PDF.

You can do this easily with the iOS version of the Debenu Quick PDF Library. Download the sample project and modify it with the sample code below:
How to embed a file
[DQPL LoadFromFile:path_of_my_PDF_file :#""]; //load your existing PDF file
[DQPL EmbedFile:#"Original_custom_document" :path_of_the_original_custom_document :#""]; //specify the name and the location of the attachment
[DQPL SaveToFile:new_path];
How to find the filenames of the attachments (skip if you have only one attached file)
[DQPL LoadFromFile:new_path :#""];
NSString *listItem;
int fileCount = [DQPL EmbeddedFileCount]; //returns the number of attached files
for( int count = 1; count <= fileCount; count++ ){
listItem = [DQPL GetEmbeddedFileStrProperty:count :1]; //returns the filename of the attached file
//show/read or save the returned filenames and the index of it...
}
How to extract an attached file
[DQPL LoadFromFile:new_path :#""];
[DQPL GetEmbeddedFileContentToFile:1 :path_and_filename_to_write_the_extracted_attachment]; //specify where do you want to save the extracted attachment (in this case the index of the attachment is 1)
Pal
(Debenu Team)

Related

Render Images in document.directory in a markdown file in swift(SwiftyMarkdown)

Am using SwiftyMarkdown framework to render my .md file to have dynamic formatted data in my VC. But i am not able to load images in my document directory referenced in .md file. I can load images if they are available in Bundle.
Any other option or framework to fix this issue? I want to dynamically load images from server and save them documents directory and refer them in .md file which is also saved in same documents directory/folder. So the url of image and .md file is same.
"MyFile.md" in documents directory contains below content
Continue your walk, joining up with your original perimeter path of the item. "
In my VC viewdidload, i access the url path and load nsattributed string to my textview using SwiftyMarkdown frmaework
let url = self.getDocumentsDirectory().appendingPathComponent("MyFile.md")
if let md = SwiftyMarkdown(url: url) {
textView.attributedText = md.attributedString()
}
The formatted text is loaded but no image as its not available in my Bundle. But its saved in the same document directory as that of "MyFile.md"
Hope its clear!

How to zip PDF and XML files which are in memorystreams

I'm working with VS2015 and ASP.Net on a webservice application which is installed in the AWS cloud.
In one of my methods i got two files, a PDF and a XML.
These files just exist as instances of type MemoryStream.
Now i have to compress these two "files" in a ZIP file before adding the zip as attachment to an E-mail (class MailMessage).
It seems that i have to save the memorystreams to files before adding them as entries to the zip.
Is ist true or do i have another possibility to add the streams as entries to the zip?
Thanks in advance!
The answer is no.
It is not necessary to save the files before adding them to the stream for the ZIP file.
I have found a solution with the Nuget package DotNetZip.
Here is a code example how to use it.
In that example there two files which only exist in MemoryStream objects, not on a local disc.
It is important to reset the Position property of the streams to zero before adding them to the ZIP stream.
At last i save the ZIP stream as a file in my local folder to control the results.
//DotNetZip from Nuget
//http://shahvaibhav.com/create-zip-file-in-memory-using-dotnetzip/
string zipFileName = System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".zip";
var zipMemStream = new MemoryStream();
zipMemStream.Position = 0;
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
textFileStream.Position = 0;
zip.AddEntry(System.IO.Path.GetFileNameWithoutExtension(xmlFileName) + ".txt", textFileStream);
xmlFileStream.Position = 0;
zip.AddEntry(xmlFileName, xmlFileStream);
zip.Save(zipMemStream);
// Try to save the ZIP-Stream as a ZIP file. And suddenly: It works!
var zipFs = new FileStream(zipFileName, FileMode.Create);
zipMemStream.Position = 0;
zipMemStream.CopyTo(zipFs);
zipMemStream.WriteTo(zipFs);
}

Format of converted Pdf is different from Word - Aspose.Words

I have a problem when saving Pdf document from Word document.
My code is as follows:
Aspose.Words.License licence = new Aspose.Words.License();
licence.SetLicense("Aspose.Total.lic");
Aspose.Words.Document doc = new Aspose.Words.Document(#"C:\Users\martink\Desktop\slTest\slvesnik.docx");
doc.Save(#"C:\Users\martink\Desktop\slTest\slvesnik.pdf", SaveFormat.Pdf);
The problem is that the source and the destination documents are not the same.
The last lines of text from page 1 in Word are on page 2 in Pdf etc.
I'm giving you the source and the destination document in the attachment.
Thank you
source document,
destination document

Email a .txt file located in application storage directory Air iOS AS3

I would like to find out, how can I email a .txt file that is located in the application storage directory.
Thanks
Jared
Use applicationStorageDirectory and navigateToURL with mailto like so,
(Note: I am not going in depth with reading text file. I can help though if you need help. Putting it only to give some idea here.)
//Find the target file
var fileToFind:File = File.applicationStorageDirectory.resolvePath("filename.txt");
//Extract content from file
var fileStream:FileStream = new FileStream();
fileStream.openAsync(fileToFind, FileMode.READ);
//fileStream.addEventListener(Event.COMPLETE, showContent);
//fileStream.addEventListener(IOErrorEvent.IO_ERROR, alertError);
//To mail
navigateToURL(newURLRequest("mailto:someEmail#gmail.com"+"?subject=Subject"+"&body="+ textfileContents + "));
--
If you want to send attachment or HTML formatted email, see this link,
SMTPMailer
.
Best luck.

Using printer name Adobe PDF

I have looked everywhere for this solution. The code below allows me to print to the printer, Adobe PDF, but what I want to do is automate the file name save as screen with a generic name and in a specific folder. For example, the file would be saved to C:\temp\tmpResize.pdf and I am having problems there.
var params = this.getPrintParams();
params.interactive=params.constants.interactionLevel.silent;
params.pageHandling=params.constants.handling.none;
params.fileName = "/c/temp/tmpResize.pdf";
params.printerName="Adobe PDF"
this.print(params);
Thanks for your help.

Resources