Lotus Notes: Displaying Image attachment on a document - lotus-notes

I have a field (Rich Text), that holds the value of an image attachment, but it only display the image path and filename, not as an image display. Am I using the wrong field or there's a problem in my line of code to attach the image? The code to attach is right below:
chqRSIDoc.photodoc = workspace.Openfiledialog(True, "Select a file to attach as photo: ", "", "c:\")
Appreciate all the help.
Thanks!

The openFileDialog returns just a string array. see http://www.ibm.com/support/knowledgecenter/SSVRGU_9.0.0/com.ibm.designer.domino.main.doc/H_OPENFILEDIALOG_METHOD_5310_ABOUT.html
I assume thatyour chqRSIDoc is of NotesDocument. If you want it as an attachment you'll have to use the NotesRichTextItem.EmbedObject function.

Here's an example in Java
Stream stream = this.session.createStream();
MIMEEntity body = doc.createMIMEEntity("dummy");
MIMEHeader header = body.createHeader("Content-type");
header.setHeaderVal("multipart/mixed");
MIMEEntity child = body.createChildEntity();
if (stream.open(filePath))
{
child.setContentFromBytes(stream, "image/jpeg", 1730);
stream.close();
doc.save(true, false);
if (doc.hasItem("Body"))
{
doc.removeItem("Body");
}
RichTextItem rt1 = doc.createRichTextItem("Body");
RichTextItem rt2 = (RichTextItem) doc.getFirstItem("dummy");
rt1.appendRTItem(rt2);
rt2.remove();
doc.save(true, false);
recycle(rt2, rt1, child, header, body);
}

Related

Mail attachment sent by SAP to Google Sheets

Briefly, from an attachment, I am looking to retrieve the data and import it into a Google Sheets spreadsheet.
Context:
An email is sent from SAP once an hour containing an attachment. This attachment can be in ".TXT" format but also in ".XLS" format but the data remains the same (no encoding parameter is available)
A script retrieves the content of the attachment.
If the attachment is in TXT format, then the data is imported into a Google Sheets.
If the attachment is in XLS format, then the attachment is converted and the data is imported into a Google Sheets.
The email containing the attachment is deleted.
Problem:
If the attachment is in TXT format and I display the data as a string,
I get this in the console:
After convert in Spreadsheet :
But for XLS format and I display the data as a string
I get this in the console:
I presume that we can observe those "?" chars due to encoding issues ?
After convert in Spreadsheet :
Why some columns appeared and how to fix it ?
Note:
If I download the XLS file on my computer and then send it back by email and convert it, I don't get the same result as if I converted it directly from the email box. Why is this?
After Convert In Spreadsheet
My scripts :
function main(){
var mailXLS;
var mailTXT;
var mailXLSDownloaded;
var attachmentXLS;
var attachmentTXT
var attachmentXLSDownloaded;
var dataXLS;
var dataTXT;
var dataXLSDownloaded;
mailXLS = GmailApp.getMessageById( "182b5d38f39bf636" ); // XLS mail.
mailTXT = GmailApp.getMessageById( "182b63be8c849b97" ); // TXT mail.
mailXLSDownloaded = GmailApp.getMessageById( "182b6510aa9153d6" ); // XLS Downloaded mail.
attachmentXLS = mailXLS.getAttachments()[0]; // Retrieve the attachment. (XLS file)
attachmentTXT = mailTXT.getAttachments()[0]; // Retrieve the attachment. (TXT file)
attachmentXLSDownloaded = mailXLSDownloaded.getAttachments()[0]; // Retrieve the attachment. (XLS Downloaded file)
dataXLS = attachmentXLS.getDataAsString(); // Data from XLS file.
dataTXT = attachmentTXT.getDataAsString(); // Data from TXT file.
dataXLSDownloaded = attachmentXLSDownloaded.getDataAsString(); // Data from XLS Downloaded file.
Logger.log( dataXLS );
Logger.log( dataTXT );
Logger.log( dataXLSDownloaded );
}
IF attachment is TXT:
// IF attachment is TXT :
var delimiter;
var body;
var spreadsheet;
delimiter = "|";
body = Utilities.parseCsv( dataTXT, delimiter ); // 2-D Array.
spreadsheet = SpreadsheetApp.create(); // new Spreadsheet.
spreadsheet.getSheets()[0].getRange( 1, 1, body.length, body[0].length ).setValues( body ); // Retrieve data.
IF attachment is XLS:
// IF attachment is XLS :
var params;
params = {
"title" : name,
"parents" : [{ "id" : "1234"}]
};
Drive.Files.insert( params, attachment, { "convert" : true} );
Looking for someone to help me in order to get the same Data format for TXT & XLS
Thanks in advance for reading !
dataXLS = attachmentXLS.getDataAsString("UTF-16"); // Data from XLS file.
var body = Utilities.parseCsv(dataXLS,"\t");
var spreadsheet = SpreadsheetApp.create("test"); // new Spreadsheet.
spreadsheet.getSheets()[0].getRange( 1, 1, body.length, body[0].length ).setValues( body ); // Retrieve data.
The issue was the encoding of the XLS file, Apps Script needed accurate encoding format in the instruction blob.getDataAsString() default is UTF-8 and it must be UTF-16.
You just need to add "UTF-16" like blob.getDataAsString("UTF-16")

Document ExportImage command creates image file with different extensions

There is a strange behavior when I try create an image from view in Revit via API. For some reason the target file sometimes is "png", sometimes "jpg" (for different View3D). As a workaround I check file existence and replace the extension, but I think it's not a good solution. The idea was taken from
https://thebuildingcoder.typepad.com/blog/2013/08/setting-a-default-3d-view-orientation.html
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
var tempFileName = Path.ChangeExtension(Path.GetRandomFileName(), "png");
string tempImageFile;
try
{
tempImageFile = Path.Combine(Path.GetTempPath(), tempFileName);
}
catch (IOException)
{
return Result.Failed;
}
var opt = new ImageExportOptions
{
ZoomType = ZoomFitType.Zoom,
FilePath = tempImageFile,
FitDirection = FitDirectionType.Horizontal,
HLRandWFViewsFileType = ImageFileType.PNG,
ImageResolution = ImageResolution.DPI_300,
};
doc.ExportImage(opt);
Debug.WriteLine(File.Exists(tempImageFile) ? "File exists." : "File does not exist.");
return Result.Succeeded;
}
}
Steps to reproduce:
Create external command (implement IExternalCommand interface)
Use provided above implementation of Execute method
Open Revit 2020 sample model (rac_basic_sample_project.rvt)
Select 3D Views->Selection Perspective (or Kitchen)
Execute external command
Check the result of doc.ExportImage(opt) command
AR: Result file has "jpg" extension instead of "png"
ER: File should be "png".
PS. If you select 3D Views->{3D} file has extension "png"
Look at screens

Search an object, a image or rectangle and add image on it in java with iText

I'm using Itext to add an image to a PDF File. I added the image but I don't want it in an absolute position because I have lots of different reports. I think to put an image in the pdf and change it but it didn't work for me.
That's my code now with absolute position:
ByteSource pdfVersion = remoteAccess.loadMultimedia(signature.getIdMultimedia());
// Imagen
Files.createDirectories(Paths.get("src/main/resources/signFolder"));
File img = new File("src/main/resources/signFolder/" + fileName);
img.deleteOnExit();
com.google.common.io.Files.write(file.read(), img);
// Pdf
File pdf = new File("src/main/resources/signFolder/" + signature.getIdMultimedia());
com.google.common.io.Files.write(pdfVersion.read(), pdf);
PdfReader reader = new PdfReader(pdf.getPath());
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("src/main/resources/signFolder/with_image.pdf"));
Image image = Image.getInstance(img.getPath());
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 270);
image.scalePercent(50);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
I thought to search a rectangle in the PDF file. Do you know how? Thanks in advance.

NotesRichTextItem.getMIMEEntity() always returns null

I have a notes form with a rich text field on it, called "Body". I've set the "Storage" property of the field to "Store contents as HTML and MIME".
Now, I am creating a new document with that form in the Notes Client.
However, if I try to access the rich text field's value in SSJS with NotesRichTextItem.getMIMEEntity(), it always returns null.
Am I missing something?
Thank you for your help in advance.
Update 2: 02/12/2015
I did some more testing and I found the cause, why it won't recognize the rich text field as MIME Type, but rather always returns it as RICH TEXT:
The cause is me accessing the database with "sessionAsSigner" rather than just using "database".
If I remove "sessionAsSigner" and use "database" instead, making the XPage unavailable to public access users, so, I am forced to log in, the code recognizes it as MIME Type and I can get a handle on NotesMIMEEntity.
Unfortunately, the XPage has to be available to public access users and I have to use sessionAsSigner.
When I open the document properties and I look at the rich text field, I can see that the "Field Flags" are "SIGN SEAL". My guess is, that's why sessionAsSigner doesn't work, but it is just a guess.
Any ideas?
Update 1: 02/12/2015
Here is the code I am using in my SSJS:
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oVWPWResetRecipient:NotesView = oDBCurrent.getView('$vwPWPMLookupPWResetNotificationProfiles');
var oDocPWResetRecipient:NotesDocument = null;
var oDocMailProfile:NotesDocument = null;
var oDocMail:NotesDocument = null;
var sServer = session.getServerName();
oDocPWResetRecipient = oVWPWResetRecipient.getDocumentByKey(sServer, true);
oDocMailProfile = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);
oDocMail = oDBCurrent.createDocument();
//Set default fields
oDocMail.replaceItemValue('Form', 'Memo');
oDocMail.replaceItemValue('Subject', oDocMailProfile.getItemValueString('iTxtSubject'));
oDocMail.replaceItemValue('SendTo', oDocPWResetRecipient.getItemValue('iNmesRecipients'))
//Get body text
var oItem:NotesItem = oDocMailProfile.getFirstItem("Body");
var entity:NotesMIMEEntity = oItem.getMIMEEntity();
//Create email body
var tmp = entity.getContentAsText();
//Replace <part2> with part 2 of the password
tmp = #ReplaceSubstring(tmp, "<part2>", sPWPart2);
//Set content of Body field as MIME type
var body = oDocMail.createMIMEEntity();
var stream = session.createStream();
stream.writeText(tmp);
body.setContentFromText(stream, "text/html; charset=iso-8859-1", 0);
//Send email
oDocMail.send();
As I mentioned before, I've also tried:
var oDBCurrent:NotesDatabase = sessionAsSigner.getDatabase(session.getServerName(), session.getCurrentDatabase().getFilePath());
var oVWMailProfiles:NotesView = oDBCurrent.getView('$vwSYSLookupEmailProfiles');
var oVWPWResetRecipient:NotesView = oDBCurrent.getView('$vwPWPMLookupPWResetNotificationProfiles');
var oDocPWResetRecipient:NotesDocument = null;
var oDocMailProfile:NotesDocument = null;
var oDocMail:NotesDocument = null;
var sServer = session.getServerName();
oDocPWResetRecipient = oVWPWResetRecipient.getDocumentByKey(sServer, true);
oDocMailProfile = oVWMailProfiles.getDocumentByKey('.MailTemplate', true);
oDocMail = oDBCurrent.createDocument();
//Set default fields
oDocMail.replaceItemValue('Form', 'Memo');
oDocMail.replaceItemValue('Subject', oDocMailProfile.getItemValueString('iTxtSubject'));
oDocMail.replaceItemValue('SendTo', oDocPWResetRecipient.getItemValue('iNmesRecipients'))
//Get body text
var entity:NotesMIMEEntity = oDocMailProfile.getMIMEEntity('Body');
//Create email body
var tmp = entity.getContentAsText();
//Replace <part2> with part 2 of the password
tmp = #ReplaceSubstring(tmp, "<part2>", sPWPart2);
//Set content of Body field as MIME type
var body = oDocMail.createMIMEEntity();
var stream = session.createStream();
stream.writeText(tmp);
body.setContentFromText(stream, "text/html; charset=iso-8859-1", 0);
//Send email
oDocMail.send();
Try calling sessionAsSigner.setConvertMime(false)
You get the MIMEEntity from the document, not from the Richtext item. See an example here (starting at line 103): https://github.com/zeromancer1972/OSnippets/blob/master/CustomControls/ccSnippets.xsp
You should set the session to not convert MIME to RichText.
Add this at the start of your code.
session.setConvertMime(false);

upload photo to facebook returning "expects upload file"

I am using the following code to upload photo to facebook
hconnect = WinHttpConnect(hsession,L"graph.facebook.com",INTERNET_DEFAULT_HTTPS_PORT,0);`hrequest = WinHttpOpenRequest( hconnect, L"POST", reqUrl,NULL, WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,WINHTTP_FLAG_SECURE);
CString temp = L"Source:";
temp += objDlg.fileName;
bResult = WinHttpAddRequestHeaders(hrequest,temp,(ULONG)-1L,WINHTTP_ADDREQ_FLAG_ADD );
CString contentType(L"Content-Type:multipart/form-data");
bResult = WinHttpAddRequestHeaders(hrequest,contentType,(ULONG)-1L,WINHTTP_ADDREQ_FLAG_ADD );bResult = WinHttpSendRequest( hrequest,WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0,objDlg.fileLength, NULL);
if(bResult)
{
DWORD dwBytesWritten = 0;
bResult = WinHttpWriteData( hrequest,objDlg.memblock, objDlg.fileLength,&dwBytesWritten);}`
but its returning "Expects Upload file as reponse.
Can anyone help me out.
Thanks in advance
Faceebook expects the post data in a "multipart/form-data" format. Form the data accordingly and send the data along with Winhttpsendrequest. The photo gets uploaded and the corresponding ID will be returned.

Resources